-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot_choice.py
More file actions
370 lines (302 loc) · 8.43 KB
/
bot_choice.py
File metadata and controls
370 lines (302 loc) · 8.43 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#Discord_bot.py reaction_choice module
import logging
logger = logging.getLogger('helium_logger')
import discord
class Reaction_Choice:
"""Reaction choice: choice"""
def initialize(self):
txt_cmds = {
self.choice_create: ['choice_create', 'create_choice'],
self.choice_modify: ['choice_modify', 'modify_choice'],
self.choice_add_reaction: ['choice_reaction', 'choice_add_reaction'],
self.choice_remove_reaction: ['choice_del_reaction', 'choice_remove_reaction', 'choice_delete_reaction'],
self.choice_delete: ['choice_delete', 'delete_choice']
}
events = {
'on_reaction_add': self.check_reaction_add,
'on_reaction_remove': self.check_reaction_remove
}
if not hasattr(self, 'choices_data'):
self.choices_data = {}
return txt_cmds, events
@discord.option(
"title",
description="The title"
)
@discord.option(
"description",
description="The description"
)
@discord.option(
"color",
description="The color, in hex"
)
async def choice_create(self,
ctx,
title : str,
desc : str,
col : str = None
):
"Create a new reaction choice"
if not self.is_admin(ctx.author):
await ctx.respond('This command is for admins only!')
return
if title in (data['title'] for data in self.choices_data.values()):
await ctx.respond(f"A reaction choice with title '{title}' already exists! Please delete the previous one or use a different title.")
return
if col is None:
col = 0x0000FF
else:
try:
col = int(col, 16)
except ValueError:
await ctx.respond(f'{col} is an invalid hex color!')
return
data = {
'guild': ctx.guild.id,
'channel': ctx.channel.id,
'title': title,
'description': desc,
'color': col,
'reactions': []
}
emb, emojis = self.format_reaction(data)
msg = await ctx.send(embed=emb)
m_id = msg.id
data['id'] = m_id
self.choices_data[m_id] = data
await ctx.hidden('Done', delete_after=5*60) # 5 mins
@discord.option(
"title",
description="The title of the choice to modify"
)
@discord.option(
"description",
description="The new description"
)
@discord.option(
"color",
description="The new color, in hex"
)
async def choice_modify(self,
ctx,
title : str,
desc : str = None,
col : str = None
):
"Modify a reaction choice"
if not self.is_admin(ctx.author):
await ctx.respond('This command is for admins only!')
return
found = False
for id, data in self.choices_data.items():
if title == data['title']:
found = True
break
if not found:
await ctx.respond(f"Reaction choice '{title}' doesn't exists yet!")
return
if desc is not None:
data['description'] = desc
if col is not None:
try:
col = int(col, 16)
except ValueError:
await ctx.respond(f'{col} is an invalid hex color!')
return
data['color'] = col
emb, emojis = self.format_reaction(data)
try:
msg = await self.get_message_from_data(data)
await msg.edit(embed=emb)
except discord.NotFound:
await ctx.respond('Choice was not found!')
logger.warning(f'Msg id {data["id"]} was not found!')
self.choices_data[data['id']] = data
await ctx.hidden('Done', delete_after=5*60) # 5 mins
@discord.option(
"title",
description="The title of the choice to modify"
)
@discord.option(
"emoji",
description="The emoji reaction"
)
@discord.option(
"role",
description="The role associated"
)
@discord.option(
"desc",
description="The description for that choice"
)
async def choice_add_reaction(self,
ctx,
title : str,
emoji : str,
role : discord.Role,
desc : str = None,
):
"Add a reaction to a reaction choice"
if not self.is_admin(ctx.author):
await ctx.respond('This command is for admins only!')
return
found = False
for id, data in self.choices_data.items():
if title == data['title']:
found = True
break
if not found:
await ctx.respond(f"Reaction choice '{title}' doesn't exists yet!")
return
if desc is None:
desc = role.name
if role.id in (react['role'] for react in data['reactions']):
await ctx.respond(f"There is already a reaction with that role!")
return
reaction = {
'emoji': emoji,
'description': desc,
'role': role.id
}
# Fetch message object
try:
msg = await self.get_message_from_data(data)
except discord.NotFound:
await ctx.respond(f'The choice was not found!')
logger.warning(f'Msg id {data["id"]} was not found!')
return
# Update embed content
emb, emojis = self.format_reaction(data)
try:
await msg.edit(embed=emb)
except discord.Forbidden:
await ctx.respond('I don\'t have the permissions to edit the message!')
# Add the emoji
try:
await msg.add_reaction(emoji)
except discord.Forbidden:
await ctx.respond('I don\'t have the permissions to add an emoji!')
return
except discord.InvalidArgument:
await ctx.respond(f'{emoji} is an invalid emoji!')
return
data['reactions'].append(reaction)
self.choices_data[data['id']] = data
await ctx.hidden('Done', delete_after=5*60) # 5 mins
@discord.option(
"title",
description="The title of the choice to modify"
)
@discord.option(
"role",
description="The role associated"
)
async def choice_remove_reaction(self,
ctx,
title : str,
role : discord.Role
):
"Remove a reaction to a reaction choice"
if not self.is_admin(ctx.author):
await ctx.respond('This command is for admins only!')
return
found = False
for id, data in self.choices_data.items():
if title == data['title']:
found = True
break
if not found:
await ctx.respond(f"Reaction choice '{title}' doesn't exists yet!")
return
found = False
for react in data['reactions']:
if role.id == react['role']:
found = True
data['reactions'].remove(react)
self.choices_data[data['id']] = data
break
if not found:
await ctx.respond(f"Role '{role.name}' hasn't been added on reaction with title '{title}'!")
return
# TODO
await ctx.hidden('Done', delete_after=5*60) # 5 mins
@discord.option(
"title",
description="The title of the choice to delete"
)
async def choice_delete(self,
ctx,
title : str
):
"Delete a reaction choice"
if not self.is_admin(ctx.author):
await ctx.respond('This command is for admins only!')
return
found = False
for id, data in self.choices_data.items():
if title == data['title']:
found = True
break
if not found:
await ctx.respond(f"Reaction choice '{title}' doesn't exists yet!")
return
m_id = data['id']
# msg = find msg from id
try:
msg = await self.get_message_from_data(data)
await msg.delete() # TODO - Errors handling
except discord.NotFound:
await ctx.respond('The choice was not found!')
logger.warning(f'Msg id {id} raised NotFound exception on choice_delete')
del self.choices_data[m_id]
await ctx.hidden('Done', delete_after=5*60) # 5 mins
async def check_reaction(self, add, reaction, user):
if reaction.message.author.bot: # Bot shouldn't get perms
return
m_id = reaction.message.id
if m_id in self.choices_data:
data = self.choices_data[m_id]
emoji = reaction.emoji
for react_data in data['reactions']:
if emoji == react_data['emoji']:
role_id = react_data['role']
if add is True:
await user.add_roles(discord.Object(id=role_id))
else:
await user.remove_roles(discord.Object(id=role_id))
return
# Unknown emoji
await reaction.remove()
async def check_reaction_add(self, reaction, user):
return await self.check_reaction(True, reaction, user)
async def check_reaction_remove(self, reaction, user):
return await self.check_reaction(False, reaction, user)
def format_reaction(self, reaction):
desc = reaction['description']
emojis = []
for role_data in reaction['reactions']:
desc += f"\n{role_data['emoji']}: {role_data['description']}"
emojis.append(role_data['emoji'])
emb = {
'type': 'rich',
'title': reaction['title'],
'description': desc,
'color': reaction['color']
}
emb = discord.Embed.from_dict(emb)
return emb, emojis
async def get_message_from_data(self, data):
msg = self.get_message(data['id']) # Cache
if msg is not None:
return msg
else:
guild = self.get_guild(data['guild']) # Cache
if guild is not None:
channel = guild.get_channel(data['channel']) # Cache
if channel is None:
# No need to get guild first
channel = await guild.fetch_channel(data['channel']) # API
msg = await channel.fetch_message(data['id']) # API
return msg
module_class = Reaction_Choice