summaryrefslogtreecommitdiff
path: root/bot.py
blob: 88153cc22d3542cf228d9bed2f58936036409aad (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import asyncio
import discord
import config
from command_utils import command, CommandClient
from random import shuffle, choice
from time import sleep


async def await_n(lst):
    lst = list(asyncio.create_task(task) for task in lst)
    if lst:
        done, _ = await asyncio.wait(lst)
        return list(task.result() for task in done)


class Client(CommandClient):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.meta_channel = None
        self.lobby_channel = None
        self.pair_channels = []

    async def on_ready(self):
        print(f'the bot {config.NAME} is logged in as "{self.user}"')

    @command(
        names = ('help', 'hepl', 'h', '?'),
        description = 'display this help message',
        is_help = True
    )
    async def help(self, ctx):
        command_doc = '\n'.join(
                f' * {config.COMMAND_PREFIX.strip()} {c.names[0]:15} - {c.description}'
                for c in self.get_commands())
        await ctx.answer(f'''```
{config.HELP_TEXT}\nThese are all available commands:\n{command_doc}```''')

    async def create_category(self, ctx):
        category = await ctx.guild.create_category(config.CATEGORY_CHANNEL_NAME)
        await ctx.answer(f'info: category created "{category}" ({category.id})')
        return category

    async def create_lobby(self, ctx):
        lobby = await ctx.guild.create_voice_channel(config.LOBBY_CHANNEL_NAME, topic=config.LOBBY_CHANNEL_TOPIC, category=self.meta_channel)
        await ctx.answer(f'info: voice channel created "{lobby}" ({lobby.id})')
        return lobby

    def get_meta_channel(self, ctx):
        return (self.meta_channel
                or discord.utils.get(ctx.guild.categories,
                                     name=config.CATEGORY_CHANNEL_NAME))

    def get_lobby_channel(self, ctx, meta_channel):
        return (self.lobby_channel
                or discord.utils.get(ctx.guild.voice_channels,
                                     name=config.LOBBY_CHANNEL_NAME,
                                     category=meta_channel))

    def get_pair_channels(self, ctx, meta_channel):
        return (self.pair_channels
                or sorted((channel for channel in ctx.guild.voice_channels
                           if channel.category == meta_channel
                           and channel.name.isdigit()),
                          key=lambda c: c.name))

    async def destroy_pair_channels(self, ctx, meta_channel):
        await await_n(channel.delete() for channel in self.get_pair_channels(ctx, meta_channel))
        self.pair_channels = []

    async def create_pair_channels(self, ctx, meta_channel, n):
        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))
        return await await_n(futures)

    @command(
        names = ('init', 'create', 'inti', 'craete', 'cretae', 'c', 'i', '+'),
        description = 'create a new lobby'
    )
    async def init(self, ctx):
        self.meta_channel = (
            self.get_meta_channel(ctx)
            or await self.create_category(ctx)
        )
        self.lobby_channel = (
            self.get_lobby_channel(ctx, self.meta_channel)
            or await self.create_lobby(ctx)
        )

    @command(
        names = ('destroy', 'kill', 'desctruction', 'genocide', '-'),
        description = f'destruct all {config.NAME} channels'
    )
    async def destroy(self, ctx):
        futures = []
        meta_channel = self.get_meta_channel(ctx)
        for channel in (self.get_lobby_channel(ctx, meta_channel), meta_channel):
            if channel:
                futures.append(channel.delete())
        await await_n(futures)
        self.lobby_channel = None
        self.meta_channel = None
        self.pair_channels = []

    async def get_channels(self, ctx):
        meta_channel = self.get_meta_channel(ctx)
        lobby_channel = self.get_lobby_channel(ctx, meta_channel)
        if meta_channel is None or lobby_channel is None:
            await ctx.answer('error: cannot start shuffling, you need to initialize channels')
            await self.help(ctx)
            return None
        return meta_channel, lobby_channel

    @command(
        names = ('shuffle', 'start', 'run', 'strat', 'rnu'),
        description = 'start shuffling'
    )
    async def shuffle(self, ctx):
        channels = await self.get_channels(ctx)
        if not channels: return
        meta_channel, lobby_channel = channels
        members = lobby_channel.members[:]
        slots = len(members) >> 1
        self.pair_channels = await self.create_pair_channels(ctx, meta_channel, slots)
        slots = []
        for i, _ in enumerate(self.pair_channels):
            slots.append(i)
            slots.append(i)
        shuffle(slots)
        futures = []
        for slot in slots:
            member = members.pop()
            if member is None: break
            futures.append(member.move_to(self.pair_channels[slot]))
        if members:
            futures.append(members.pop().move_to(choice(self.pair_channels)))
        await await_n(futures)

    @command(
        names = ('stop', 'quit', 'exit', 'abort', 'back', 'return'),
        description = 'move everyone back to lobby'
    )
    async def stop(self, ctx):
        channels = await self.get_channels(ctx)
        if not channels: return
        meta_channel, lobby_channel = channels
        pair_channels = self.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 self.destroy_pair_channels(ctx, meta_channel)

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


if __name__ == '__main__':
    from os import getenv
    token = getenv(config.TOKEN_ENV_VAR)
    if token is None:
        print('error: no token was given')
        exit(1)
    bot = Client(activity=discord.Game(name=config.GAME_STATUS))
    bot.run(token)