From 3772f04b5c10169f10664688bbc4cb82d1214059 Mon Sep 17 00:00:00 2001 From: davfsa Date: Sun, 27 May 2018 11:13:25 +0200 Subject: [PATCH 1/2] Added tag.py --- cogs.txt | 1 + src/cogs/tag.py | 97 +++++++++++++++++++++++++++++++++++++++++ src/resources/tags.json | 1 + 3 files changed, 99 insertions(+) create mode 100644 src/cogs/tag.py create mode 100644 src/resources/tags.json 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 From daf320a0764178d2a98e6a48a60fd9b1b8772cde Mon Sep 17 00:00:00 2001 From: davfsa Date: Sun, 27 May 2018 11:27:24 +0200 Subject: [PATCH 2/2] Updated tag.py --- src/cogs/tag.py | 6 +++--- src/{resources => shared_libs}/tags.json | 0 2 files changed, 3 insertions(+), 3 deletions(-) rename src/{resources => shared_libs}/tags.json (100%) diff --git a/src/cogs/tag.py b/src/cogs/tag.py index 84c6f2a..3bae890 100644 --- a/src/cogs/tag.py +++ b/src/cogs/tag.py @@ -7,7 +7,7 @@ import asyncio class Tag: def __init__(self, bot): self.bot = bot - with open("src/resources/tags.json", "r") as fp: + with open("src/shared_libs/tags.json", "r") as fp: json_data = fp.read() global tags tags = json.loads(json_data) @@ -59,7 +59,7 @@ class Tag: if not exists: tags.update({tag_name : tag_info}) - async with aiofiles.open("src/resources/tags.json", "w") as fp: + async with aiofiles.open("src/shared_libs/tags.json", "w") as fp: json_data = json.dumps(tags) await fp.write(json_data) @@ -84,7 +84,7 @@ class Tag: if found is not None: del tags[found] - async with aiofiles.open("src/resources/tags.json", "w") as fp: + async with aiofiles.open("src/shared_libs/tags.json", "w") as fp: json_data = json.dumps(tags) await fp.write(json_data) diff --git a/src/resources/tags.json b/src/shared_libs/tags.json similarity index 100% rename from src/resources/tags.json rename to src/shared_libs/tags.json