-
Notifications
You must be signed in to change notification settings - Fork 0
/
discord_bot.py
executable file
·162 lines (124 loc) · 5.07 KB
/
discord_bot.py
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
#!/usr/bin/python3
# pragma pylint: disable=missing-docstring
from typing import Dict, List
import datetime
import discord # type: ignore
from discord_game import DiscordGame
import seat_commands as commands
from seat_typing import SeatException, SeatChannel
# TODO: police nickname changes
class DiscordBotException(SeatException):
pass
class DiscordBot(discord.Client): # type: ignore
def __init__(self) -> None:
super().__init__()
self.games: Dict[SeatChannel, DiscordGame] = {}
# self.players: Dict[discord.user, DiscordGame] = {}
self.command_list: List[commands.CommandType] = []
self.command_dict: Dict[str, List[commands.CommandType]] = {}
self._initialize_commands()
def _initialize_commands(self) -> None:
self.command_list += [
# General info
commands.Help(self.command_dict),
commands.Rules(),
commands.Commands(self.command_list),
commands.Source(),
# Game management
commands.Create(self.games),
commands.Recreate(self.games),
commands.Join(self.games),
commands.CreateJoin(self.games),
commands.RecreateJoin(self.games),
commands.Leave(self.games),
commands.AddBot(self.games),
commands.RemoveBot(self.games),
commands.Ready(self.games),
commands.Unready(self.games),
# Options
commands.StreakLength(self.games),
commands.XCount(self.games),
commands.RoundLength(self.games),
commands.RevealLongestStreak(self.games),
# game info
commands.PrintProposals(self.games),
commands.PrintBotSwaps(self.games),
commands.PrintPlayers(self.games),
commands.PrintGarnets(self.games),
commands.PrintSeating(self.games),
commands.AssignNumber(self.games),
commands.UnassignNumber(self.games),
# gameplay
commands.ProposeSeatSwap(self.games),
commands.AcceptSeatSwap(self.games),
commands.CancelSeatSwap(self.games),
commands.CreateBotSwap(self.games),
commands.CancelBotSwap(self.games),
commands.DonateGarnets(self.games),
# real life game
commands.CreateRealLifeGame(self.games),
commands.Reveal(self.games),
commands.Swap(self.games),
commands.RealLifeSeating(self.games),
# admin
commands.Shutdown(self),
commands.ForceStart(self.games),
commands.ForceStop(self.games),
commands.ForceSwap(self.games),
commands.ForceNewRound(self.games),
commands.ForceSeatNumbers(self.games),
]
for command in self.command_list:
for command_name in command.command_name_list:
if command_name not in self.command_dict:
self.command_dict[command_name] = [command]
else:
self.command_dict[command_name].append(command)
async def on_ready(self) -> None:
print('Logged in as {0.user} at {1}'.format(
self, datetime.datetime.now()))
# for guild in self.guilds:
# for channel in guild.channels:
# if channel.name == 'testing':
# await channel.send('Seat Exchange Bot v0.1')
async def on_message(self, message: discord.message) -> None:
if message.author == self.user:
return
if not message.content.startswith('!'):
return
command = message.content.split(' ')[0][1:]
channel = SeatChannel(message.channel)
# parameters = message.content.split(' ')[1:]
if command in self.command_dict:
command_message = commands.CommandMessage(message, channel)
errors = []
for matching_command in self.command_dict[command]:
try:
await matching_command.execute(command_message)
return
except SeatException as error:
errors.append(error)
print(errors)
await message.channel.send('\n'.join(str(x) for x in errors))
async def on_reaction_add(self,
reaction: discord.Reaction,
user: discord.User) -> None: # TODO
print('reaction added')
message = reaction.message
channel = SeatChannel(message.channel)
if channel not in self.games:
print('no channel')
return
game = self.games[channel]
if message.id not in game.reactable_messages:
print(message, '\n', game.reactable_messages)
return
await game.reactable_messages[message.id].on_react(
reaction, user)
def main() -> None:
with open('discord_token') as f: # pylint: disable=invalid-name
token = f.read().strip()
bot = DiscordBot()
bot.run(token)
if __name__ == '__main__':
main()