From 74f4c11f28a983f619c2fe37ac2c4b099f0c6f45 Mon Sep 17 00:00:00 2001 From: Ave Ozkal Date: Sun, 23 Dec 2018 16:13:39 +0300 Subject: [PATCH] Switch to .py config --- .gitignore | 4 +++- README.md | 7 +++++-- Robocop.py | 36 ++++++++---------------------------- cogs/admin.py | 25 ++++++++++++++++--------- cogs/basic.py | 17 ++++++++--------- config.py.template | 13 +++++++++++++ 6 files changed, 53 insertions(+), 49 deletions(-) create mode 100644 config.py.template diff --git a/.gitignore b/.gitignore index c4bdd71..349aaf6 100755 --- a/.gitignore +++ b/.gitignore @@ -97,4 +97,6 @@ files/* .idea *.ttf -priv-* \ No newline at end of file +priv-* +config.py + diff --git a/README.md b/README.md index ba77045..993b2f9 100755 --- a/README.md +++ b/README.md @@ -9,10 +9,13 @@ Based on https://gitlab.com/ao/dpybotbase ## TODO -- [ ] .py configs +- [x] .py configs - [ ] Verification - [ ] Logging joins, leaves, role changes, deletes, bans, kicks -- [ ] Moderation commands +- [ ] Moderation commands (ban, warn etc) - [ ] Meme commands (honestly the easiest part) - [ ] .serr and .err +- [x] source command +- [ ] robocop command +- [ ] eval and sh might need to be removed diff --git a/Robocop.py b/Robocop.py index 80f27ca..b26aebe 100755 --- a/Robocop.py +++ b/Robocop.py @@ -3,9 +3,9 @@ import sys import logging import logging.handlers import traceback -import configparser from pathlib import Path import aiohttp +import config import discord from discord.ext import commands @@ -31,12 +31,9 @@ log.setLevel(logging.INFO) log.addHandler(file_handler) log.addHandler(stdout_handler) -config = configparser.ConfigParser() -config.read(f"{script_name}.ini") - def get_prefix(bot, message): - prefixes = [config['base']['prefix']] + prefixes = config.prefixes return commands.when_mentioned_or(*prefixes)(bot, message) @@ -46,7 +43,7 @@ initial_extensions = ['cogs.common', 'cogs.basic'] bot = commands.Bot(command_prefix=get_prefix, - description=config['base']['description'], pm_help=None) + description=config.bot_description, pm_help=None) bot.log = log bot.config = config @@ -69,7 +66,7 @@ async def on_ready(): log.info(f'\nLogged in as: {bot.user.name} - ' f'{bot.user.id}\ndpy version: {discord.__version__}\n') - game_name = f"{config['base']['prefix']}help" + game_name = f"{config.prefixes[0]}help" await bot.change_presence(activity=discord.Game(name=game_name)) @@ -129,32 +126,15 @@ async def on_command_error(ctx, error): f"arguments. {help_text}") -@bot.event -async def on_guild_join(guild): - bot.log.info(f"Joined guild \"{guild.name}\" ({guild.id}).") - await guild.owner.send(f"Hello and welcome to {script_name}!\n" - "If you don't know why you're getting this message" - f", it's because someone added {script_name} to your" - " server\nDue to Discord API ToS, I am required to " - "inform you that **I log command usages and " - "errors**.\n**I don't log *anything* else**." - "\n\nIf you do not agree to be logged, stop" - f" using {script_name} and remove it from your " - "server as soon as possible.") - - @bot.event async def on_message(message): if message.author.bot: return + if message.guild.id not in config.guild_whitelist: + return + ctx = await bot.get_context(message) await bot.invoke(ctx) -if not Path(f"{script_name}.ini").is_file(): - log.warning( - f"No config file ({script_name}.ini) found, " - f"please create one from {script_name}.ini.example file.") - exit(3) - -bot.run(config['base']['token'], bot=True, reconnect=True) +bot.run(config.token, bot=True, reconnect=True) diff --git a/cogs/admin.py b/cogs/admin.py index c4400a8..3a744aa 100644 --- a/cogs/admin.py +++ b/cogs/admin.py @@ -3,6 +3,7 @@ from discord.ext import commands import traceback import inspect import re +import config class AdminCog: @@ -11,20 +12,26 @@ class AdminCog: self.last_eval_result = None self.previous_eval_code = None - @commands.is_owner() + def check_if_staff(ctx): + return any(r.id in config.staff_role_ids for r in ctx.author.roles) + + def check_if_bot_manager(ctx): + return any(r.id in config.bot_manager_role_id for r in ctx.author.roles) + + @commands.check(check_if_staff) @commands.command(aliases=['echo'], hidden=True) async def say(self, ctx, *, the_text: str): """Repeats a given text.""" await ctx.send(the_text) - @commands.is_owner() + @commands.check(check_if_bot_manager) @commands.command(name='exit', hidden=True) async def _exit(self, ctx): """Shuts down the bot, owner only.""" await ctx.send(":wave: Exiting bot, goodbye!") await self.bot.logout() - @commands.is_owner() + @commands.check(check_if_bot_manager) @commands.command(hidden=True) async def load(self, ctx, ext: str): """Loads a cog, owner only.""" @@ -37,14 +44,14 @@ class AdminCog: self.bot.log.info(f'Loaded ext {ext}') await ctx.send(f':white_check_mark: `{ext}` successfully loaded.') - @commands.is_owner() + @commands.check(check_if_bot_manager) @commands.command(hidden=True) async def fetchlog(self, ctx): """Returns log""" await ctx.send(file=discord.File(f"{self.bot.script_name}.log"), content="Here's the current log file:") - @commands.is_owner() + @commands.check(check_if_bot_manager) @commands.command(name='eval', hidden=True) async def _eval(self, ctx, *, code: str): """Evaluates some code (Owner only)""" @@ -97,7 +104,7 @@ class AdminCog: for msg in sliced_message: await ctx.send(msg) - @commands.is_owner() + @commands.check(check_if_bot_manager) @commands.command(hidden=True) async def pull(self, ctx, auto=False): """Does a git pull (Owner only).""" @@ -118,7 +125,7 @@ class AdminCog: f'```\n{traceback.format_exc()}\n```') return - @commands.is_owner() + @commands.check(check_if_bot_manager) @commands.command(hidden=True) async def sh(self, ctx, *, command: str): """Runs a command on shell.""" @@ -138,7 +145,7 @@ class AdminCog: for msg in sliced_message: await ctx.send(msg) - @commands.is_owner() + @commands.check(check_if_bot_manager) @commands.command(hidden=True) async def unload(self, ctx, ext: str): """Unloads a cog, owner only.""" @@ -146,7 +153,7 @@ class AdminCog: self.bot.log.info(f'Unloaded ext {ext}') await ctx.send(f':white_check_mark: `{ext}` successfully unloaded.') - @commands.is_owner() + @commands.check(check_if_bot_manager) @commands.command(hidden=True) async def reload(self, ctx, ext="_"): """Reloads a cog, owner only.""" diff --git a/cogs/basic.py b/cogs/basic.py index 24f64c6..4fde7de 100644 --- a/cogs/basic.py +++ b/cogs/basic.py @@ -1,4 +1,5 @@ import time +import config from discord.ext import commands @@ -7,20 +8,18 @@ class Basic: def __init__(self, bot): self.bot = bot - @commands.command() - async def invite(self, ctx): - """Sends an invite to add the bot""" - await ctx.send(f"{ctx.author.mention}: You can use " - " " - "to add {self.bot.user.name} to your guild.") - @commands.command() async def hello(self, ctx): """Says hello. Duh.""" await ctx.send(f"Hello {ctx.author.mention}!") + @commands.command() + async def source(self, ctx): + """Gives link to source code.""" + await ctx.send("You can find my source at " + + config.source_url + + ". Serious PRs and issues welcome!") + @commands.command(aliases=['p']) async def ping(self, ctx): """Shows ping values to discord. diff --git a/config.py.template b/config.py.template new file mode 100644 index 0000000..17e4cac --- /dev/null +++ b/config.py.template @@ -0,0 +1,13 @@ +# Basic bot config +prefixes = [".", "!"] +token = "token-goes-here" +bot_description = "An attempt to rewrite the bot used in ReSwitched" +guild_whitelist = [ + 526372255052201993, # NotSwitched discord + 269333940928512010 # ReSwitched discord +] + +source_url = "https://github.com/aveao/robocop-ng" + +bot_manager_role_id = 1 +staff_role_ids = [1, 2]