async def restart(ctx): if str(ctx.message.author.id) in Shared.Vars.devs: Shared.Vars.Stopping = True Output("Restarting") await bot.change_presence(activity=discord.Activity(name="bot restarting...", type=discord.ActivityType.playing), status=discord.Status.do_not_disturb, afk=False) Output("Unloading all cogs.") for Cog in Shared.API.GatherCogs(): try: Output(f"unloading cog \"{Cog}\".") bot.unload_extension(Cog) except: Output(f"Failed to unload cog \"{Cog}\"") try: await ctx.message.delete() except: Output(Type="Error", Msg="Unable to delete restart command.") if Shared.Vars.IsLinux: os.system("python3 bot.py") else: os.system("bot.py") exit(0) else: msg = await ctx.send(":x: You do not have the required permissions to run this command.") await asyncio.sleep(5) await msg.delete()
async def Cog_Reload(ctx, cog): if str(ctx.message.author.id) in Shared.Vars.devs: if cog == "*": Output("Reloading all cogs.") for Cog in Shared.API.GatherCogs(): try: Output(f"reloading cog \"{Cog}\".") bot.reload_extension(Cog) Output(f"reloaded \"{Cog}\"") except: Output(Type="Error", Msg=f"Failed to reload cog \"{Cog}\"") await ctx.send("Reloaded all cogs!") Output("Reload complete!") else: try: Output(f"reloading cog \"{cog}\".") bot.reload_extension(cog) Output(f"reloaded cog \"{cog}\".") await ctx.message.delete() msg = await ctx.send("Reloaded cog successfully!") await asyncio.sleep(5) await msg.delete() except: await ctx.message.delete() msg = await ctx.send("There was an error Reloading that cog, please make sure the location and file is correct (eg: \"Commands.Help\")") Output(Type="Error", Msg=f"Failed to reload cog \"{cog}\".") await asyncio.sleep(5) await msg.delete()
async def purge_error(self, ctx, error): if isinstance(error, commands.MissingRequiredArgument): await ctx.send(f"Missing argument (`{error}`).") elif isinstance(error, commands.BadArgument): await ctx.send(f"Bad argument (`{error}`).") else: Output(Type="Error", Msg=f"Purge error, {str(error)}")
async def update(ctx, *, args: str="None"): if str(ctx.message.author.id) in Shared.Vars.devs: Shared.Vars.Stopping = True Output("Attempting to perform update procedure.") await bot.change_presence(activity=discord.Activity(name="bot updating...", type=discord.ActivityType.playing), status=discord.Status.do_not_disturb, afk=False) try: await ctx.message.delete() except: Output(Type="Error", Msg="Unable to delete update command.") UpOut = os.popen("git pull").read() if "already up to date." in UpOut.lower(): Output(Type="Error", Msg="Aborting update, bot is already up to date.") await bot.change_presence(activity=discord.Activity(name=f"for {Shared.Vars.prefix}", type=discord.ActivityType.watching), status=discord.Status.online, afk=False) Shared.Vars.Stopping = False elif "please commit your changes or stash them before you merge" in UpOut.lower(): Output(Type="Error", Msg="Bot update faliled: bot has uncomitted changes.") await bot.change_presence(activity=discord.Activity(name=f"for {Shared.Vars.prefix}", type=discord.ActivityType.watching), status=discord.Status.online, afk=False) Shared.Vars.Stopping = False elif "aborting" in UpOut.lower(): Output(Type="Error", Msg="Bot update failed: unknown error.") await bot.change_presence(activity=discord.Activity(name=f"for {Shared.Vars.prefix}", type=discord.ActivityType.watching), status=discord.Status.online, afk=False) Shared.Vars.Stopping = False else: if "--soft" in str(args).lower(): Output("Update complete.") msg = await ctx.send("Downloaded update from git. Due to `--soft`, a cog reload or a bot restart may be needed to load / enable updated code. To do this, `//Cog_Reload *` will reload all cogs, `//restart` will restart the bot.") await bot.change_presence(activity=discord.Activity(name=f"for {Shared.Vars.prefix}", type=discord.ActivityType.watching), status=discord.Status.online, afk=False) Shared.Vars.Stopping = False await asyncio.sleep(10) await msg.delete() else: Output("Update complete, restarting.") Output("Unloading all cogs.") for Cog in Shared.API.GatherCogs(): try: Output(f"unloading cog \"{Cog}\".") bot.unload_extension(Cog) except: Output(f"Failed to unload cog \"{Cog}\"") os.system("bot.py") exit(0) else: msg = await ctx.send(":x: You do not have the required permissions to run this command.") await asyncio.sleep(5) await msg.delete()
async def config(ctx, module: str = None, property: str = None, value: str = None, file: str = "Settings.json"): if str(ctx.message.author.id) in Shared.Vars.devs: BotSettings = Shared.Vars.Settings['Bot_Settings'] if module == "*": Keys = "" for key in dict(Shared.Vars.Settings['Bot_Settings']).keys(): skey = str(key) + "\n" Keys = Keys + str(skey) await ctx.send("```-----Module list-----\n" + Keys + "```") elif module == None: await ctx.send("<@" + str(ctx.message.author.id) + "> :regional_indicator_x: Missing value `module` (arg1).") elif module in BotSettings.keys(): BSettings = Shared.Vars.Settings['Bot_Settings'] Module = BSettings[module] if property == None: Keys = "" for key, value in Module.items(): skey = str(key) + " = " + str(value) + "\n" Keys = Keys + str(skey) await ctx.send("```-----" + module.capitalize() + " variable list-----\n" + Keys + "```") else: if property in Module.keys(): if value == None: await ctx.send("Property `" + property + "` is currently set to `" + str(Module[property]) + "`.") else: if type(Module[property]) != type(value): try: value = int(value) except ValueError: try: value = float(value) except: if "true" or "false" in value.lower(): if "false" in value.lower(): value = False else: value = True else: pass if type(Module[property]) == type(value): Module[property] = value BSettings[module] = Module Shared.Vars.Settings['Bot_Settings'] = BSettings with open("Settings.json", "w") as f: json.dump(Shared.Vars.Settings, f, indent=4) await ctx.send("Property updated to \"" + str(value) + "\".") Output(f"Property {property} in {module} updated to \"{str(value)}\".") else: await ctx.send(":x: Invalid type: `" + str(type(value)) + "`, should be: `" + str(type(Module[property])) + "`.") else: await ctx.send("Invalid property.") else: await ctx.send("<@" + str(ctx.message.author.id) + "> :regional_indicator_x: Invalid module.") else: await ctx.send(":warning:Warning: <@" + str(ctx.message.author.id) + "> has insuficiant permissions to run this command.")
async def Cog_Load(ctx, cog): if str(ctx.message.author.id) in Shared.Vars.devs: if cog == "*": Output("Loading all cogs.") for Cog in CogL: try: bot.load_extension(Cog) except Exception as e: Output(Premsg="\n", Type="Error", Msg=f"Failed to load cog \"{Cog}\", {e}") else: try: Output(f"loading cog \"{cog}\".") bot.load_extension(cog) Output(f"loaded cog \"{cog}\".") await ctx.message.delete() msg = await ctx.send("Loaded cog successfully!") await asyncio.sleep(5) await msg.delete() except: Output(Type="Error", Msg=f"Failed to load cog \"Commands.{cog[:-3]}\"") await ctx.message.delete() msg = await ctx.send("There was an error loading that cog, please make sure the location and file is correct (eg: \"Commands.Help\")") await asyncio.sleep(5) await msg.delete()
async def on_command_error(ctx, error): if hasattr(ctx.command, "on_error"): return error = getattr(error, 'original', error) Sverity = "Undetermined" Class = "Unknown" if isinstance(error, commands.MissingRequiredArgument): Sverity = "Unimportant"; Class = "User invoked" if isinstance(error, commands.BadArgument): Sverity = "Unimportant"; Class = "User invoked" if isinstance(error, commands.CommandNotFound): Sverity = "Unimportant"; Class = "User invoked" if isinstance(error, commands.CommandInvokeError): Sverity = "Medium"; Class = "Code error" if isinstance(error, AttributeError): Severity = "Medium"; Class = "Code error / Var error" if isinstance(error, UnboundLocalError): Severity = "Critical"; Class = "Code error / Var error" Output(Type="Error", Msg=f"An error has occured: \n\n--ST3-MODERATOR ERROR HANDLER--\n Command: {ctx.command}\n Error: {str(error)}\n Error type: {str(type(error))[8:-2]}\n Error class: {Class}\n Sverity: {Sverity}\n-------------------------------\n")
async def on_ready(): Output(Premsg="\r", Msg="Bot loaded [100%]", End="\n") await bot.change_presence(activity=discord.Activity(name=f"for {bot.command_prefix}", type=discord.ActivityType.watching), status=discord.Status.online, afk=False) Output("Bot ready.")
if not path.isfile("Guilds.json"): Gfile = open("Guilds.json","w+") Gfile.write("{}") Gfile.close() while True: try: #Clear the screen, this is to reduce console clutter. if Shared.Vars.IsLinux: os.system("clear") else: os.system("cls") Output("ST3-MODERATOR by \"ST3VI3 RICHI3#5015\"") Output("\n\n ST3-MODERATOR Copyright (C) 2020 ST3VI3 RICHI3\n\n This program comes with ABSOLUTELY NO WARRANTY.\n This is free software, and you are welcome to redistribute it\n under certain conditions.\n", Type="Copyright") Output("Checking for updates.") UpOut = os.popen("git pull").read() if "already up to date." in UpOut.lower(): Output("No updates found.") else: Output("Update found, restarting.") os.system("bot.py") exit(0) Output("Bot loading [0%]") #--Init--# Shared.__init__()