diff --git a/cogs.txt b/cogs.txt index 9529ab0..3fd3b1f 100644 --- a/cogs.txt +++ b/cogs.txt @@ -1,5 +1,6 @@ example contributors +tag code git fun \ No newline at end of file diff --git a/src/cogs/tag.py b/src/cogs/tag.py new file mode 100644 index 0000000..84c6f2a --- /dev/null +++ b/src/cogs/tag.py @@ -0,0 +1,97 @@ +import discord +from discord.ext import commands +import json +import aiofiles +import asyncio + +class Tag: + def __init__(self, bot): + self.bot = bot + with open("src/resources/tags.json", "r") as fp: + json_data = fp.read() + global tags + tags = json.loads(json_data) + + @commands.group(case_insensitive=True, invoke_without_command=True) + async def tag(self, ctx, tag=None): + """Gets a tag""" + await ctx.trigger_typing() + if tag is None: + return await ctx.send('Please provide a argument. Do `help tag` for more info') + + found = tags.get(tag, None) + + if found is None: + return await ctx.send('Tag not found') + + await ctx.send(found) + + @tag.command(case_insensitive=True) + async def list(self, ctx): + """Lists available tags""" + await ctx.trigger_typing() + desc = "" + for i in tags: + desc = desc + i + "\n" + + if desc == "": + desc = "None" + + em = discord.Embed(title='Available tags:', description=desc ,colour=discord.Colour(0x00FFFF)) + + await ctx.send(embed=em) + + @tag.command(case_insensitive=True) + async def add(self, ctx, tag_name=None, *, tag_info=None): + """Adds a new tag""" + await ctx.trigger_typing() + if not ctx.author.guild_permissions.manage_guild: + return await ctx.send("You are not allowed to do this") + + if tag_name is None or tag_info is None: + return await ctx.send("Please provide a tag name and the tag info. Do `help tag` for more info") + + exists = False + for i in tags: + if i == tag_name: + exists = True + + if not exists: + tags.update({tag_name : tag_info}) + + async with aiofiles.open("src/resources/tags.json", "w") as fp: + json_data = json.dumps(tags) + await fp.write(json_data) + + return await ctx.send("The tag has been added") + + await ctx.send("The tag already exists") + + @tag.command(case_insensitive=True) + async def remove(self, ctx, tag=None): + """Remove a existing tag""" + await ctx.trigger_typing() + if not ctx.author.guild_permissions.manage_guild: + return await ctx.send("You are not allowed to do this") + + if tag is None: + return await ctx.send("Please provide a tag name and the tag info. Do `help tag` for more info") + + found = None + for i in tags: + if i == tag: + found = i + + if found is not None: + del tags[found] + async with aiofiles.open("src/resources/tags.json", "w") as fp: + json_data = json.dumps(tags) + await fp.write(json_data) + + return await ctx.send("The tag has been removed") + + await ctx.send("The tag has not been found") + + +def setup(bot): + bot.add_cog(Tag(bot)) diff --git a/src/resources/tags.json b/src/resources/tags.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/src/resources/tags.json @@ -0,0 +1 @@ +{} \ No newline at end of file