From 9b472f1af089439e0a8cac67635392214213cea1 Mon Sep 17 00:00:00 2001 From: NatrixAeria Date: Mon, 19 Oct 2020 23:45:44 +0200 Subject: Disallow actions to plebs --- bot.py | 23 ++++++++++++++++++----- commands.py | 13 +++++++++++++ config.py | 9 ++++++++- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/bot.py b/bot.py index 1ef8bec..72753dd 100644 --- a/bot.py +++ b/bot.py @@ -12,7 +12,7 @@ from collections import defaultdict as dd class Cupido(commands.Bot): - def __init__(self, *args, **kwargs): + def __init__(self, admin_channel, *args, **kwargs): super().__init__(*args, **kwargs) self.meta_channel = None self.lobby_channel = None @@ -23,6 +23,7 @@ class Cupido(commands.Bot): self.read_file() self.score_map = dd(lambda: 0) self.oldgroups = set() + self.admin_channel = admin_channel def read_file(self): try: @@ -60,10 +61,18 @@ class Cupido(commands.Bot): await answer(ctx, f'info: category created "{category}" ({category.id})') return category + async def create_voice_channel(self, ctx, name, category=None): + channel = await ctx.guild.create_voice_channel( + config.LOBBY_CHANNEL_NAME, + category=self.meta_channel, + ) + await channel.set_permissions(ctx.guild.default_role, **config.CHANNEL_PERMISSIONS) + return channel + async def create_lobby(self, ctx): - lobby = await ctx.guild.create_voice_channel( + lobby = await self.create_voice_channel( + ctx, config.LOBBY_CHANNEL_NAME, - topic=config.LOBBY_CHANNEL_TOPIC, category=self.meta_channel, ) await answer(ctx, f'info: voice channel created "{lobby}" ({lobby.id})') @@ -142,7 +151,7 @@ class Cupido(commands.Bot): await self.destroy_pair_channels(ctx, meta_channel) futures = [] for i in range(1, n + 1): - futures.append(ctx.guild.create_voice_channel(str(i), category=meta_channel)) + futures.append(self.create_voice_channel(ctx, str(i), category=meta_channel)) return await await_n(futures) async def get_channels(self, ctx): @@ -224,7 +233,11 @@ def main(): if token is None: print('error: no token was given') sys.exit(1) - bot = Cupido(activity=discord.Game(name=config.GAME_STATUS), command_prefix=config.COMMAND_PREFIX, case_insensitive=True) + admin_channel = getenv(config.ADMIN_CHANNEL_ENV_VAR) + if admin_channel is None: + print('error: no admin channel was given') + sys.exit(1) + bot = Cupido(int(admin_channel), activity=discord.Game(name=config.GAME_STATUS), command_prefix=config.COMMAND_PREFIX, case_insensitive=True) bot.remove_command('help') for cmd in bot_commands: bot.add_command(cmd) diff --git a/commands.py b/commands.py index a422e26..a804469 100644 --- a/commands.py +++ b/commands.py @@ -20,6 +20,11 @@ async def answer(ctx: commands.Context, msg: str, *args, **kwargs): await ctx.send(f'{ctx.author.mention} {msg}', *args, **kwargs) +def is_admin_channel(ctx: commands.Context): + print(ctx.message.channel,ctx.bot.admin_channel, ctx.message.channel == ctx.bot.admin_channel) + return ctx.message.channel.id == ctx.bot.admin_channel + + @commands.command(help='display this help message', aliases=('hepl', 'h', '?')) async def help(ctx: commands.Context): command_doc = '\n'.join( @@ -31,12 +36,14 @@ async def help(ctx: commands.Context): @commands.command(help='create a new lobby', aliases=('create', 'inti', 'craete', 'cretae', 'c', 'i', '+')) async def init(ctx: commands.Context): + if not is_admin_channel(ctx): return ctx.bot.meta_channel = ctx.bot.get_meta_channel(ctx) or await ctx.bot.create_category(ctx) ctx.bot.lobby_channel = ctx.bot.get_lobby_channel(ctx, ctx.bot.meta_channel) or await ctx.bot.create_lobby(ctx) @commands.command(help=f'destruct all {config.NAME} channels', aliases=('kill', 'desctruction', 'genocide', '-')) async def destroy(ctx: commands.Context): + if not is_admin_channel(ctx): return await stop(ctx) futures = [] meta_channel = ctx.bot.get_meta_channel(ctx) @@ -56,6 +63,7 @@ def round_up_div(a, b): @commands.command(help='start shuffling', aliases=('start', 'run', 'strat', 'rnu')) async def shuffle(ctx: commands.Context, channel_size=2): + if not is_admin_channel(ctx): return if channel_size < 1: await answer(ctx, 'error: channel size must be at least 1 (you jerk)') return False @@ -120,6 +128,7 @@ async def shuffle(ctx: commands.Context, channel_size=2): @commands.command(help='move everyone back to lobby', aliases=('quit', 'exit', 'abort', 'back', 'return')) async def stop(ctx: commands.Context, cancel_loop=True): + if not is_admin_channel(ctx): return channels = await ctx.bot.get_channels(ctx) if not channels: return @@ -149,6 +158,7 @@ async def loop_cycle(ctx, t): f' (default: {config.DEFAULT_LOOP_TIME}) seconds', aliases=('repeat', 'shuffleloop')) async def loop(ctx: commands.Context, n=config.DEFAULT_LOOP_COUNT, t=config.DEFAULT_LOOP_TIME): + if not is_admin_channel(ctx): return try: n, t = int(n), int(t) except ValueError: @@ -171,6 +181,7 @@ async def loop(ctx: commands.Context, n=config.DEFAULT_LOOP_COUNT, t=config.DEFA aliases=('tutor', 'hello') ) async def add(ctx: commands.Context, id=None): + if not is_admin_channel(ctx): return if id is None: return await answer(ctx, 'error: expecting user id') try: @@ -185,6 +196,7 @@ async def add(ctx: commands.Context, id=None): aliases=('rm', 'del', 'delete', 'byebye', 'bb') ) async def remove(ctx: commands.Context, id=None): + if not is_admin_channel(ctx): return if id is None: return await answer(ctx, 'error: expecting user id') try: @@ -200,6 +212,7 @@ async def remove(ctx: commands.Context, id=None): aliases=('tutors',) ) async def list(ctx: commands.Context): + if not is_admin_channel(ctx): return if ctx.bot.tutors: users = [] for tutor in ctx.bot.tutors: diff --git a/config.py b/config.py index 84ba673..4703448 100644 --- a/config.py +++ b/config.py @@ -1,5 +1,6 @@ NAME = 'shuffle bot' TOKEN_ENV_VAR = 'DISCORD_TOKEN' +ADMIN_CHANNEL_ENV_VAR = 'ADMIN_CHANNEL' TUTOR_FILE_PATH = 'tutors' COMMAND_PREFIX = 's!' @@ -13,7 +14,6 @@ This software is open-source ! CATEGORY_CHANNEL_NAME = 'KENNENLERNEN' LOBBY_CHANNEL_NAME = 'Lobby' -LOBBY_CHANNEL_TOPIC = 'Wer auch schon immer so richtig durchgeshuffelt werden wollte ist hier genau richtig!' # time that one loop cycle needs (in seconds) DEFAULT_LOOP_TIME = 3 * 60 @@ -46,3 +46,10 @@ Vielleicht ja beim nächsten Mal 😎 SCOREBOARD_TITLE = 'Scoreboard' SCOREBOARD_TEXT = 'Hier siehst du, wer wie viele Punkte hat:' + +CHANNEL_PERMISSIONS = { + 'priority_speaker': False, + 'stream': False, + 'move_members': False, + 'mute_members': False, +} -- cgit v1.2.3