summaryrefslogtreecommitdiff
path: root/commands.py
blob: e463b9872ef52208f6f0bc626db0c4364da8dbc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import asyncio
from typing import Any, List
import random
from time import sleep

import config

from discord.ext import commands


async def await_n(it) -> List[Any]:
    lst = [asyncio.create_task(task) for task in it]
    if not lst:
        return []
    done, _ = await asyncio.wait(lst)
    return [task.result() for task in done]


async def answer(ctx: commands.Context, msg: str):
    await ctx.send(f'{ctx.author.mention} {msg}')


@commands.command(help='display this help message', aliases=('hepl', 'h', '?'))
async def help(ctx: commands.Context):
    command_doc = '\n'.join(
        f' * {config.COMMAND_PREFIX.strip()} {c.name:15} - {c.help}'
        for c in ctx.bot.commands)
    await answer(ctx, f'''```
{config.HELP_TEXT}\nThese are all available commands:\n{command_doc}```''')


@commands.command(help='create a new lobby', aliases=('create', 'inti', 'craete', 'cretae', 'c', 'i', '+'))
async def init(ctx: commands.Context):
    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):
    futures = []
    meta_channel = ctx.bot.get_meta_channel(ctx)
    for channel in (ctx.bot.get_lobby_channel(ctx, meta_channel), meta_channel):
        if channel:
            futures.append(channel.delete())
    await await_n(futures)
    ctx.bot.lobby_channel = None
    ctx.bot.meta_channel = None
    ctx.bot.pair_channels = []


@commands.command(help='start shuffling', aliases=('start', 'run', 'strat', 'rnu'))
async def shuffle(ctx: commands.Context):
    channels = await ctx.bot.get_channels(ctx)
    if not channels:
        return
    meta_channel, lobby_channel = channels
    members = lobby_channel.members[:]
    slots = len(members) >> 1
    ctx.bot.pair_channels = await ctx.bot.create_pair_channels(ctx, meta_channel, slots)
    slots = []
    for i, _ in enumerate(ctx.bot.pair_channels):
        slots.append(i)
        slots.append(i)
    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]))
    if members:
        futures.append(members.pop().move_to(random.choice(ctx.bot.pair_channels)))
    await await_n(futures)


@commands.command(help='move everyone back to lobby', aliases=('quit', 'exit', 'abort', 'back', 'return'))
async def stop(ctx: commands.Context):
    channels = await ctx.bot.get_channels(ctx)
    if not channels:
        return
    meta_channel, lobby_channel = channels
    pair_channels = ctx.bot.get_pair_channels(ctx, meta_channel)
    futures = []
    for channel in pair_channels:
        for member in channel.members:
            futures.append(member.move_to(lobby_channel))
    await await_n(futures)
    await ctx.bot.destroy_pair_channels(ctx, meta_channel)


@commands.command(help='repeat "shuffle" and "stop" <n> (default: 3) times and <t> (default: 120) seconds')
async def loop(ctx: commands.Context, *args):
    if len(args) >= 1 and args[0].isdigit():
        n = int(args[0])
    else:
        n = 3
    if len(args) >= 2 and args[1].isdigit():
        t = int(ctx.args[1])
    else:
        t = 120
    await answer(ctx, f'repeat shuffling {n} times and each {t} seconds')
    for _ in range(n):
        await shuffle(ctx)
        sleep(t)
        await stop(ctx)


bot_commands = [cmd for cmd in locals().values() if isinstance(cmd, commands.Command)]