From 750e021833c1105948c5dde8948be2ab71871650 Mon Sep 17 00:00:00 2001 From: Akumatic Date: Wed, 20 Jun 2018 20:43:07 +0200 Subject: [PATCH] Own Settings for every Server the Bot joined --- akuma.py | 42 +++++++++++++++++++-------- extensions/moderation.py | 62 ++++++++++++++++++++-------------------- extensions/user.py | 8 +++--- server.json | 2 ++ settings.json | 6 +--- 5 files changed, 68 insertions(+), 52 deletions(-) create mode 100644 server.json diff --git a/akuma.py b/akuma.py index d39dd32..2aa78aa 100644 --- a/akuma.py +++ b/akuma.py @@ -3,14 +3,18 @@ import discord from discord.ext import commands #config file -c = json.load(open("settings.json", "r")) +c = json.load(open("settings.json", "r")) +s = json.load(open("server.json", "r")) + #The Bot itself bot = commands.Bot(description=c["description"], command_prefix=c["prefix"]) #Function to write changed config to JSON file -def writeJSON(data): - with open("settings.json", "w") as file: - json.dump(data, file, indent=4) +def writeConfig(data): + json.dump(data, open("settings.json", "w"), indent=4) + +def writeServer(data): + json.dump(data, open("server.json", "w"), indent=4) @bot.event async def on_ready(): @@ -18,13 +22,27 @@ async def on_ready(): game = (c["prefix"] + "help" if (c["game"] == "") else c["prefix"] + "help | " + c["game"]) return await bot.change_presence(status=discord.Status.online,activity=discord.Game(name=game)) +@bot.event +async def on_guild_join(guild): + s[str(guild.id)] = {"adminRole": "", "modRole": "", "joinMessage" : "", "suggestionChannel": 0, "modChannel": 0} + writeServer(s) + +@bot.event +async def on_guild_remove(guild): + del s[str(guild.id)] + writeServer(s) + +@bot.command() +async def invite(ctx): + await ctx.send("https://discordapp.com/oauth2/authorize?client_id={}&scope=bot&permissions=8".format(bot.user.id)) + @bot.command(hidden=True) async def printExt(ctx): """Prints out every loaded extension""" - s = [] + string = [] for ext in bot.extensions: - s.append(ext.split(".")[1]) - await ctx.send("Loaded extensions: " + ", ".join(s)) + string.append(ext.split(".")[1]) + await ctx.send("Loaded extensions: " + ", ".join(string)) @bot.command(hidden=True) async def load(ctx, ext : str = None, json : bool = False): @@ -32,7 +50,7 @@ async def load(ctx, ext : str = None, json : bool = False): First argument is the name of python file without .py extension. (Optional) If second argument is True, it will be autoloaded""" - if(ctx.author.id != c["owner"]): + if(ctx.author.id != ctx.guild.owner.id): return if(ext == None): return await ctx.send("No extension specified") @@ -41,14 +59,14 @@ async def load(ctx, ext : str = None, json : bool = False): await ctx.send("Loaded " + ext) if(json): c["extensions"].append(ext) - writeJSON(c) + writeConfig(c) except Exception as e: await ctx.send("Failed to load extension \"{}\": {}".format(ext, "{} ({})".format(type(e).__name__, e))) @bot.command(hidden=True) async def reload(ctx, ext : str = None): """Reloads an extension""" - if(ctx.author.id != c["owner"]): + if(ctx.author.id != ctx.guild.owner.id): return if(ext == None): return await ctx.send("No extension specified") @@ -69,7 +87,7 @@ async def unload(ctx, ext : str = None, json : bool = False): First argument is the name of the extension. (Optional) If second argument is True, it will be removed from autoload""" - if(ctx.author.id != c["owner"]): + if(ctx.author.id != ctx.guild.owner.id): return if(ext == None): return await ctx.send("No extension specified") @@ -78,7 +96,7 @@ async def unload(ctx, ext : str = None, json : bool = False): await ctx.send("Unloaded " + ext) if(json): c["extensions"].remove(ext) - writeJSON(c) + writeConfig(c) else: await ctx.send("Extension " + ext + " not loaded") diff --git a/extensions/moderation.py b/extensions/moderation.py index 2892f4d..386c69a 100644 --- a/extensions/moderation.py +++ b/extensions/moderation.py @@ -1,6 +1,6 @@ -import discord, json +import discord from discord.ext import commands -from akuma import c, writeJSON +from akuma import s, c, writeServer modCommands = """```Possible Commands: mod setJoinMessage @@ -24,7 +24,7 @@ class Moderation(): @commands.group() async def mod(self, ctx): """Commands usable a Mod""" - if (ctx.author.id not in c["mods"]): + if (s[str(ctx.guild.id)]["modRole"] not in [r.name for r in ctx.author.roles]): return if ctx.invoked_subcommand is None: await ctx.send(modCommands) @@ -32,7 +32,7 @@ class Moderation(): @commands.group() async def admin(self, ctx): """Commands usable by an Admin""" - if(ctx.author.id not in c["admins"]): + if(s[str(ctx.guild.id)]["adminRole"] not in [r.name for r in ctx.author.roles]): return if ctx.invoked_subcommand is None: await ctx.send(adminCommands) @@ -48,8 +48,8 @@ class Moderation(): ### Mod Commands ### @mod.command() async def setJoinMessage(self, ctx, *, msg : str): - c["joinMessage"] = msg - writeJSON(c) + s[str(ctx.guild.id)]["joinMessage"] = msg + writeServer(s) await ctx.send("Join Message sucessfully changed to: " + msg) ### Admin Commands ### @@ -61,11 +61,11 @@ class Moderation(): if user is None: return await ctx.send("User not Found") #Add Mod Role to User - if(c["modRole"] not in [r.name for r in user.roles]): - await user.add_roles(discord.utils.get(ctx.guild.roles, name=c["modRole"])) - await ctx.send("User " + user.name + " was added to " + c["modRole"]) + if(s[str(ctx.guild.id)]["modRole"] not in [r.name for r in user.roles]): + await user.add_roles(discord.utils.get(ctx.guild.roles, name=s[str(ctx.guild.id)]["modRole"])) + await ctx.send("User " + user.name + " was added to " + s[str(ctx.guild.id)]["modRole"]) else: - return await ctx.send("User " + user.name + " is already in " + c["modRole"]) + return await ctx.send("User " + user.name + " is already in " + s[str(ctx.guild.id)]["modRole"]) @admin.command() async def rmMod(self, ctx, id : int = None): @@ -76,29 +76,29 @@ class Moderation(): return await ctx.send("User not Found") if (user.id == ctx.author.id): return await ctx.send("You can't remove yourself from Mods") - if(c["adminRole"] in [r.name for r in user.roles] and ctx.author.id != ctx.guild.owner.id): + if(s[str(ctx.guild.id)]["adminRole"] in [r.name for r in user.roles] and ctx.author.id != ctx.guild.owner.id): return await ctx.send("You can't remove this ID") - if(c["modRole"] in [r.name for r in user.roles]): - await user.remove_roles(discord.utils.get(ctx.guild.roles, name=c["modRole"])) - await ctx.send("User " + user.name + " was removed from " + c["modRole"]) + if(s[str(ctx.guild.id)]["modRole"] in [r.name for r in user.roles]): + await user.remove_roles(discord.utils.get(ctx.guild.roles, name=s[str(ctx.guild.id)]["modRole"])) + await ctx.send("User " + user.name + " was removed from " + s[str(ctx.guild.id)]["modRole"]) else: - return await ctx.send("User " + user.name + " wasn't in " + c["modRole"]) + return await ctx.send("User " + user.name + " wasn't in " + s[str(ctx.guild.id)]["modRole"]) ### Owner Commands ### @owner.command() async def setModRole(self, ctx, msg : str): if(msg not in [r.name for r in ctx.guild.roles]): return await ctx.send("Role " + msg + " does not exist") - c["modRole"] = msg - writeJSON(c) + s[str(ctx.guild.id)]["modRole"] = msg + writeServer(s) await ctx.send("Mod role set") @owner.command() async def setAdminRole(self, ctx, msg : str): if(msg not in [r.name for r in ctx.guild.roles]): return await ctx.send("Role " + msg + " does not exist") - c["adminRole"] = msg - writeJSON(c) + s[str(ctx.guild.id)]["adminRole"] = msg + writeServer(s) await ctx.send("Admin role set") @owner.command() @@ -109,17 +109,17 @@ class Moderation(): if user is None: return await ctx.send("User not Found") #Add Admin Role to User - if(c["adminRole"] not in [r.name for r in user.roles]): - await user.add_roles(discord.utils.get(ctx.guild.roles, name=c["adminRole"])) - await ctx.send("User " + user.name + " was added to " + c["adminRole"]) + if(s[str(ctx.guild.id)]["adminRole"] not in [r.name for r in user.roles]): + await user.add_roles(discord.utils.get(ctx.guild.roles, name=s[str(ctx.guild.id)]["adminRole"])) + await ctx.send("User " + user.name + " was added to " + s[str(ctx.guild.id)]["adminRole"]) else: - return await ctx.send("User " + user.name + " is already in " + c["adminRole"]) + return await ctx.send("User " + user.name + " is already in " + s[str(ctx.guild.id)]["adminRole"]) #Add Mod Role to User - if(c["modRole"] not in [r.name for r in user.roles]): - await user.add_roles(discord.utils.get(ctx.guild.roles, name=c["modRole"])) - await ctx.send("User " + user.name + " was added to " + c["modRole"]) + if(s[str(ctx.guild.id)]["modRole"] not in [r.name for r in user.roles]): + await user.add_roles(discord.utils.get(ctx.guild.roles, name=s[str(ctx.guild.id)]["modRole"])) + await ctx.send("User " + user.name + " was added to " + s[str(ctx.guild.id)]["modRole"]) else: - return await ctx.send("User " + user.name + " is already in " + c["modRole"]) + return await ctx.send("User " + user.name + " is already in " + s[str(ctx.guild.id)]["modRole"]) @owner.command() async def rmAdmin(self, ctx, id : int = None): @@ -130,11 +130,11 @@ class Moderation(): return await ctx.send("User not Found") if (user.id == ctx.author.id): return await ctx.send("You can't remove yourself from Admins") - if(c["adminRole"] in [r.name for r in user.roles]): - await user.remove_roles(discord.utils.get(ctx.guild.roles, name=c["adminRole"])) - await ctx.send("User " + user.name + " was removed from " + c["adminRole"]) + if(s[str(ctx.guild.id)]["adminRole"] in [r.name for r in user.roles]): + await user.remove_roles(discord.utils.get(ctx.guild.roles, name=s[str(ctx.guild.id)]["adminRole"])) + await ctx.send("User " + user.name + " was removed from " + s[str(ctx.guild.id)]["adminRole"]) else: - return await ctx.send("User " + user.name + " wasn't in " + c["adminRole"]) + return await ctx.send("User " + user.name + " wasn't in " + s[str(ctx.guild.id)]["adminRole"]) #Setup def setup(bot): diff --git a/extensions/user.py b/extensions/user.py index b3254f4..1428aa3 100644 --- a/extensions/user.py +++ b/extensions/user.py @@ -1,5 +1,5 @@ from discord.ext import commands -from akuma import c +from akuma import s import discord class User(): @@ -9,7 +9,7 @@ class User(): @commands.command() async def greetMe(self, ctx): """Prints the greeting text a user receives by joining the server""" - await ctx.send(c["joinMessage"]) + await ctx.send(s[str(ctx.guild.id)]["joinMessage"]) @commands.command() async def suggest(self, ctx, *, msg : str): @@ -20,8 +20,8 @@ class User(): e = discord.Embed(color=0x6428c8) e.set_author(name=ctx.author, icon_url=ctx.author.avatar_url) e.add_field(name="Suggestion", value=msg) - if(c["suggestionChannel"] != 0): - chan = self.bot.get_channel(c["suggestionChannel"]) + if(s[str(ctx.guild.id)]["suggestionChannel"] != 0): + chan = self.bot.get_channel(s[str(ctx.guild.id)]["suggestionChannel"]) await chan.send(embed=e) else: await ctx.send(e) diff --git a/server.json b/server.json new file mode 100644 index 0000000..e02aef2 --- /dev/null +++ b/server.json @@ -0,0 +1,2 @@ +{ +} \ No newline at end of file diff --git a/settings.json b/settings.json index f136406..3bd0d47 100644 --- a/settings.json +++ b/settings.json @@ -3,13 +3,9 @@ "prefix": "~", "description": "A Discord Bot written by Akuma#7346", "game": "", - "adminRole": "", - "modRole": "", "extensions": [ "fun", "moderation", "user" - ], - "joinMessage": "", - "suggestionChannel": 0 + ] } \ No newline at end of file