diff options
author | NatrixAeria <upezu@student.kit.edu> | 2020-10-15 19:04:27 +0200 |
---|---|---|
committer | NatrixAeria <upezu@student.kit.edu> | 2020-10-15 19:04:27 +0200 |
commit | 47562293ebb8cabc862509c9ba1d62d982bee3c1 (patch) | |
tree | 41f69ef6f89dacd2b277473de4abd56dccd65815 | |
parent | 8883d72fedbb438b0fa14b1dae0405d6317f7e07 (diff) |
Add channel size property to the shuffle command
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | bot.py | 5 | ||||
-rw-r--r-- | commands.py | 35 |
3 files changed, 22 insertions, 19 deletions
@@ -1,2 +1,3 @@ .env .idea/ +__pycache__ @@ -77,6 +77,9 @@ class Cupido(commands.Bot): except discord.errors.NotFound: return False + async def send_panel(self, ctx, user): + pass + async def destroy_pair_channels(self, ctx, meta_channel): await await_n(map(self.try_delete_channel, self.get_pair_channels_no_cache(ctx, meta_channel))) self.pair_channels = [] @@ -102,7 +105,7 @@ def main(): if token is None: print('error: no token was given') exit(1) - bot = Cupido(activity=discord.Game(name=config.GAME_STATUS), command_prefix=config.COMMAND_PREFIX) + bot = Cupido(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 4e34bce..916b4ad 100644 --- a/commands.py +++ b/commands.py @@ -48,32 +48,31 @@ async def destroy(ctx: commands.Context): @commands.command(help='start shuffling', aliases=('start', 'run', 'strat', 'rnu')) -async def shuffle(ctx: commands.Context): +async def shuffle(ctx: commands.Context, channel_size=2): + if channel_size < 1: + await answer(ctx, 'error: channel size must be at least 1 (you jerk)') + return False channels = await ctx.bot.get_channels(ctx) if not channels: return False meta_channel, lobby_channel = channels members = lobby_channel.members[:] - if len(members) == 0: - await answer(ctx, 'nobody wants to get shuffeled :(') + if not members: + await answer(ctx, 'error: nobody wants to get shuffeled :(') return False - elif len(members) == 1: - await answer(ctx, f'{members[0].mention} you are a looser and you don\'t have any friends, get a life') + elif len(members) < channel_size: + await answer(ctx, f'error: you are too few people ({len(members)}). Group size is {channel_size}') return False - slots = len(members) >> 1 - ctx.bot.pair_channels = await ctx.bot.create_pair_channels(ctx, meta_channel, slots) + # round up the quotient between the member count and the channel size + channel_count = (len(members) + channel_size - 1) // channel_size + ctx.bot.pair_channels = await ctx.bot.create_pair_channels(ctx, meta_channel, channel_count) slots = [] for i, _ in enumerate(ctx.bot.pair_channels): - slots.append(i) - slots.append(i) + slots.extend([i] * channel_size) random.shuffle(slots) - futures = [] - for slot in slots: - member = members.pop() - if member is None: break - futures.append(member.move_to(ctx.bot.pair_channels[slot])) + futures = [members.pop().move_to(ctx.bot.pair_channels[slot]) for slot in slots] if members: - futures.append(members.pop().move_to(random.choice(ctx.bot.pair_channels))) + await answer(ctx, 'warning: not all members got shuffeled') await await_n(futures) return True @@ -111,9 +110,9 @@ async def loop(ctx: commands.Context, n=config.DEFAULT_LOOP_COUNT, t=config.DEFA try: n, t = int(n), int(t) except ValueError: - await answer(ctx, f'expecting positive integer arguments for <n> and <t> (e.g. "loop 5 60" to repeat 5 times)') + await answer(ctx, f'error: expecting positive integer arguments for <n> and <t> (e.g. "loop 5 60" to repeat 5 times)') return - await answer(ctx, f'repeat shuffling {n} times and each {t} seconds') + await answer(ctx, f'error: repeat shuffling {n} times and each {t} seconds') for _ in range(n): result = await ctx.bot.await_coroutine(loop_cycle(ctx, t)) message = { @@ -121,7 +120,7 @@ async def loop(ctx: commands.Context, n=config.DEFAULT_LOOP_COUNT, t=config.DEFA 'already running': 'cannot start loop, another task is running...' }.get(result, None) if message is not None: - return await answer(ctx, message) + return await answer(ctx, f'error: {message}') if not result: break |