This repository has been archived by the owner on Sep 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcs_cog.py
489 lines (446 loc) · 24.1 KB
/
cs_cog.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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
import discord, db_handler as dbh, copy
from asyncio import Lock
from discord.ext import commands
from typing import Union
import functions
from converters import ChannelElseInt
class Settings(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command(
name='prefix', aliases=['p'], brief='Set prefix',
description='Set prefix for server'
)
@commands.guild_only()
@commands.has_permissions(manage_messages=True)
async def set_prefix(self, ctx, prefix, space: bool = False):
if prefix == '':
await ctx.send("That cannot be the prefix")
return
new_prefix = f"{prefix}{' ' if space else ''}"
if ctx.guild.id not in dbh.database.locks:
dbh.database.locks[ctx.guild.id] = Lock()
async with dbh.database.locks[ctx.guild.id]:
dbh.database.db['guilds'][ctx.guild.id]['prefix'] = new_prefix
await ctx.send(f"Set prefix to `{new_prefix}`")
@commands.group(
name='defaults', aliases=['d'], description='Set the default settings when starboards are added',
brief='Manage default starboard settings'
)
@commands.guild_only()
async def defaults(self, ctx):
if ctx.invoked_subcommand == None:
settings = dbh.database.db['guilds'][ctx.guild.id]['default_settings']
embed = discord.Embed(title='Starboard Defaults', color=0xFCFF00)
msg = ''
msg += f"**requiredStars:** {settings['required_stars']}\n"
msg += f"**requiredToLose:** {settings['required_to_lose']}\n"
msg += f"**selfStar:** {settings['self_star']}\n"
msg += f"**linkEdits:** {settings['link_edits']}\n"
msg += f"**linkDeletes:** {settings['link_deletes']}\n"
embed.description = msg
await ctx.send(embed=embed)
@defaults.command(
name='selfstar', aliases=['ss'], description='Set the default for allowing self-stars',
brief='Set default for self-star'
)
@commands.guild_only()
@commands.has_permissions(manage_channels=True)
async def toggle_self_star(self, ctx, self_star: bool):
if ctx.guild.id not in dbh.database.locks:
dbh.database.locks[ctx.guild.id] = Lock()
async with dbh.database.locks[ctx.guild.id]:
dbh.database.db['guilds'][ctx.guild.id]['default_settings']['self_star'] = self_star
await ctx.send(f"Set selfStar to {self_star}.")
@defaults.command(
name='requiredstars', aliases=['rs'], description='Set the default for minimum stars for message',
brief='Set default for required-stars'
)
@commands.guild_only()
@commands.has_permissions(manage_channels=True)
async def required_stars(self, ctx, count: int):
if count <= dbh.database.db['guilds'][ctx.message.guild.id]['default_settings']['required_to_lose']:
await ctx.send("requiredStars cannot be less than or equal to requiredToLose")
return
if ctx.guild.id not in dbh.database.locks:
dbh.database.locks[ctx.guild.id] = Lock()
async with dbh.database.locks[ctx.guild.id]:
dbh.database.db['guilds'][ctx.guild.id]['default_settings']['required_stars'] = count
await ctx.send(f"The default for requiredStars has been set to {count}")
@defaults.command(
name='requiredtolose', aliases=['rtl'], defaults='Set the default for maximum stars before message is removed',
brief='Set default for required-to-lose'
)
@commands.guild_only()
@commands.has_permissions(manage_channels=True)
async def required_to_lose(self, ctx, count: int):
if count >= dbh.database.db['guilds'][ctx.message.guild.id]['default_settings']['required_stars']:
await ctx.send("requiredToLose cannot be greater than or equal to requiredStars.")
return
if ctx.guild.id not in dbh.database.locks:
dbh.database.locks[ctx.guild.id] = Lock()
async with dbh.database.locks[ctx.guild.id]:
dbh.database.db['guilds'][ctx.message.guild.id]['default_settings']['required_to_lose'] = count
await ctx.send(f"The default for requiredToLose has been set to {count}.")
@defaults.command(
name='linkedits', aliases=['le'], description='Set the default for linking message edits',
brief='Set default for link-deletes'
)
@commands.guild_only()
@commands.has_permissions(manage_channels=True)
async def toggle_link_edits(self, ctx, link_edits: bool):
if ctx.guild.id not in dbh.database.locks:
dbh.database.locks[ctx.guild.id] = Lock()
async with dbh.database.locks[ctx.guild.id]:
dbh.database.db['guilds'][ctx.message.guild.id]['default_settings']['link_edits'] = link_edits
await ctx.send(f"The default for linkEdits has been set to {link_edits}")
@defaults.command(
name='linkdeletes', aliases=['ld'], description='Set the default for linking message deletes',
brief='Set default for link-edits'
)
@commands.guild_only()
@commands.has_permissions(manage_channels=True)
async def toggle_link_deletes(self, ctx, link_deletes: bool):
if ctx.guild.id not in dbh.database.locks:
dbh.database.locks[ctx.guild.id] = Lock()
async with dbh.database.locks[ctx.guild.id]:
dbh.database.db['guilds'][ctx.message.guild.id]['default_settings']['link_deletes'] = link_deletes
await ctx.send(f"The default for linkDeletes has been set to {link_deletes}")
@commands.group(
name='starboard', aliases=['s'], invoke_without_command=True, description='Managed starboards',
brief='Manage starboards'
)
@commands.guild_only()
async def channel(self, ctx, channel:discord.TextChannel=None):
if ctx.invoked_subcommand == None:
msg = ''
if channel is None:
settings = dbh.database.db['guilds'][ctx.guild.id]['channels']
if ctx.guild.id not in dbh.database.locks:
dbh.database.locks[ctx.guild.id] = Lock()
async with dbh.database.locks[ctx.guild.id]:
for channel_id in dbh.database.db['guilds'][ctx.guild.id]['channels']:
channel_object = discord.utils.get(ctx.guild.channels, id=channel_id)
if channel_object is None:
msg += f"Deleted Channel; ID: {channel_id}\n"
continue
emoji_str = await functions.get_emoji_str(ctx.guild, settings[channel_id]['emojis'])
msg += f"{channel_object.mention}: {emoji_str}\n"
else:
settings = dbh.database.db['guilds'][ctx.guild.id]['channels']
channel_object = discord.utils.get(ctx.guild.channels, id=channel.id)
emoji_str = await functions.get_emoji_str(ctx.guild, settings[channel.id]['emojis'])
msg += f"{channel_object.mention}: {emoji_str}\n"
msg += f"**----requiredStars:** {settings[channel.id]['required_stars']}\n"
msg += f"**----requiredToLose:** {settings[channel.id]['required_to_lose']}\n"
msg += f"**----selfStar:** {settings[channel.id]['self_star']}\n"
msg += f"**----linkEdits:** {settings[channel.id]['link_edits']}\n"
msg += f"**----linkDeletes:** {settings[channel.id]['link_deletes']}\n"
if msg == '':
msg = 'No starboards have been set. Use `<prefix> starboard add <channel>` to add one.'
embed = discord.Embed(title='Starboards', description=msg, color=0xFCFF00)
await ctx.send(embed=embed)
@channel.command(
name='selfstar', aliases=['ss'], description='Set self-star for specific starboard',
brief='Set self-star for starboard'
)
@commands.guild_only()
@commands.has_permissions(manage_channels=True)
async def channel_self_star(self, ctx, channel: discord.TextChannel, allow: bool):
if channel == None:
await ctx.send("I could not find any channel with that name or id")
return
if ctx.guild.id not in dbh.database.locks:
dbh.database.locks[ctx.guild.id] = Lock()
async with dbh.database.locks[ctx.guild.id]:
dbh.database.db['guilds'][ctx.guild.id]['channels'][channel.id]['self_star'] = allow
await ctx.send(f"Set selfStar to {allow} for {channel.mention}")
@channel.command(
name='linkedits', aliases=['le'], description='Set link-edits for specific starboard',
brief='Set link-edits for starboard'
)
@commands.guild_only()
@commands.has_permissions(manage_channels=True)
async def channel_link_edits(self, ctx, channel: discord.TextChannel, link_edits: bool):
if ctx.guild.id not in dbh.database.locks:
dbh.database.locks[ctx.guild.id] = Lock()
async with dbh.database.locks[ctx.guild.id]:
dbh.database.db['guilds'][ctx.guild.id]['channels'][channel.id]['link_edits'] = link_edits
await ctx.send(f"Set linkEdits to {link_edits} for {channel.mention}")
@channel.command(
name='linkdeletes', aliases=['ld'], description='Set link-deletes for specific starboard',
brief='Set link-deletes for starboard'
)
@commands.guild_only()
@commands.has_permissions(manage_channels=True)
async def channel_link_deletes(self, ctx, channel: discord.TextChannel, link_deletes: bool):
if ctx.guild.id not in dbh.database.locks:
dbh.database.locks[ctx.guild.id] = Lock()
async with dbh.database.locks[ctx.guild.id]:
dbh.database.db['guilds'][ctx.guild.id]['channels'][channel.id]['link_deletes'] = link_deletes
await ctx.send(f"Set linkDeletes to {link_deletes} for {channel.mention}")
@channel.command(
name='add', aliases=['+', 'a'], description='Add a new starboard',
brief='Add starboard'
)
@commands.guild_only()
@commands.has_permissions(manage_channels=True)
async def add_channel(self, ctx, channel: discord.TextChannel):
if channel == None:
await ctx.send("I could not find any channel with that name or id")
return
try:
dbh.database.db['guilds'][ctx.guild.id]['channels'][channel.id]
except KeyError:
if ctx.guild.id not in dbh.database.locks:
dbh.database.locks[ctx.guild.id] = Lock()
async with dbh.database.locks[ctx.guild.id]:
dbh.database.db['guilds'][ctx.guild.id]['channels'][channel.id] = copy.deepcopy(dbh.database.db['guilds'][ctx.guild.id]['default_settings'])
dbh.database.db['guilds'][ctx.guild.id]['channels'][channel.id]['emojis'] = []
dbh.database.db['guilds'][ctx.guild.id]['channels'][channel.id]['messages'] = {}
await ctx.send(f"Added starboard {channel.mention}")
return
await ctx.send(f"{channel.mention} is already a starboard")
@channel.command(
name='remove', aliases=['-', 'delete', 'd', 'r'], description='Remove a starboard',
brief='Remove starboard'
)
@commands.guild_only()
@commands.has_permissions(manage_channels=True)
async def remove_channel(self, ctx, channel: ChannelElseInt):
if channel == None:
await ctx.send("I could not find any channel with that name or id")
return
try:
channel_id = channel if isinstance(channel, int) else channel.id
if ctx.guild.id not in dbh.database.locks:
dbh.database.locks[ctx.guild.id] = Lock()
async with dbh.database.locks[ctx.guild.id]:
del dbh.database.db['guilds'][ctx.guild.id]['channels'][channel_id]
await ctx.send(f"{channel_id if isinstance(channel, int) else channel.mention} is no longer a starboard")
except KeyError:
await ctx.send("That channel is not a starboard")
@channel.command(
name='addemoji', aliases=['ae'], description='Add emoji for specific starboard',
brief='Add emoji'
)
@commands.guild_only()
@commands.has_permissions(manage_channels=True)
async def starboard_add_emoji(self, ctx, channel: discord.TextChannel, in_emoji):
emoji = await functions.get_emoji(ctx.guild, in_emoji)
if type(emoji) is discord.Emoji:
emoji = emoji.id
try:
if ctx.guild.id not in dbh.database.locks:
dbh.database.locks[ctx.guild.id] = Lock()
async with dbh.database.locks[ctx.guild.id]:
dbh.database.db['guilds'][ctx.guild.id]['channels'][channel.id]['emojis'].append(emoji)
await ctx.send(f"Added {in_emoji} to {channel.mention}")
except KeyError:
await ctx.send(f"{channel.mention} is not a starboard.")
@channel.command(
name='removeemoji', aliases=['re'], description='Removed emoji for specific starboard',
brief='Remove emoji'
)
@commands.guild_only()
@commands.has_permissions(manage_channels=True)
async def starboard_remove_emoji(self, ctx, channel: discord.TextChannel, in_emoji):
emoji = await functions.get_emoji(ctx.guild, in_emoji)
if type(emoji) is discord.Emoji:
emoji = emoji.id
try:
if ctx.guild.id not in dbh.database.locks:
dbh.database.locks[ctx.guild.id] = Lock()
async with dbh.database.locks[ctx.guild.id]:
found = False
if emoji in dbh.database.db['guilds'][ctx.guild.id]['channels'][channel.id]['emojis']:
dbh.database.db['guilds'][ctx.guild.id]['channels'][channel.id]['emojis'].remove(emoji)
found = True
else:
try:
if int(emoji) in dbh.database.db['guilds'][ctx.guild.id]['channels'][channel.id]['emojis']:
dbh.database.db['guilds'][ctx.guild.id]['channels'][channel.id]['emojis'].remove(int(emoji))
found = True
except Exception as e:
print(e)
if found:
await ctx.send(f"Removed {in_emoji} from {channel.mention}")
else:
await ctx.send(f"Could not find that emoji in {channel.mention}")
except KeyError:
await ctx.send(f"Either {in_emoji} is not linked to {channel.mention} or {channel.mention} is not a starboard.")
@channel.command(
name='requiredstars', aliases=['rs'], description='Set minimum stars for message to appear on starboard for specific starboard',
brief='Set required-stars for starboard'
)
@commands.guild_only()
@commands.has_permissions(manage_channels=True)
async def channel_required_stars(self, ctx, channel: discord.TextChannel, count: int):
if channel == None:
await ctx.send("I could not find any channel with that name or id")
return
if ctx.guild.id not in dbh.database.locks:
dbh.database.locks[ctx.guild.id] = Lock()
async with dbh.database.locks[ctx.guild.id]:
if count <= dbh.database.db['guilds'][ctx.message.guild.id]['channels'][channel.id]['required_to_lose']:
await ctx.send("requiredStars cannot be less than or equal to requiredToLose")
return
dbh.database.db['guilds'][ctx.message.guild.id]['channels'][channel.id]['required_stars'] = count
await ctx.send(f"Set requiredStars to {count}")
@channel.command(
name='requiredtolose', aliases=['rtl'], description='Set minimum stars before message is removed for specific starboard',
brief='Set required-to-lose for starboard'
)
@commands.guild_only()
@commands.has_permissions(manage_channels=True)
async def channel_required_to_lose(self, ctx, channel: discord.TextChannel, count: int):
if channel == None:
await ctx.send("I could not find any channel with that name or id")
return
if ctx.guild.id not in dbh.database.locks:
dbh.database.locks[ctx.guild.id] = Lock()
async with dbh.database.locks[ctx.guild.id]:
if count >= dbh.database.db['guilds'][ctx.message.guild.id]['channels'][channel.id]['required_stars']:
await ctx.send("requiredToLose cannot be greater than or equal to requiredStars")
return
dbh.database.db['guilds'][ctx.message.guild.id]['channels'][channel.id]['required_to_lose'] = count
await ctx.send(f"requiredToLose has been set to {count}")
@commands.group(
name='mediachannel', aliases=['mc'], description='Manage media channels, where the bot automatically reacts to messages with an emoji.',
brief='Manage media channels', invoke_without_command=True
)
@commands.guild_only()
async def media_channels(self, ctx, channel: discord.TextChannel=None):
string = ''
if channel is None:
if ctx.guild.id not in dbh.database.locks:
dbh.database.locks[ctx.guild.id] = Lock()
async with dbh.database.locks[ctx.guild.id]:
for media_channel_id in dbh.database.db['guilds'][ctx.guild.id]['media_channels']:
media_channel = discord.utils.get(ctx.guild.channels, id=media_channel_id)
if media_channel is not None:
emojis = dbh.database.db['guilds'][ctx.guild.id]['media_channels'][media_channel_id]['emojis']
emoji_str = await functions.get_emoji_str(ctx.guild, emojis)
string += f"{media_channel.mention}: {emoji_str}\n"
else:
string += f"Deleted Channel; ID: {media_channel_id}"
if string == '':
string = "You have no media channels set. Use `<prefix> mediachannel add <channel>` to add one."
else:
settings = dbh.database.db['guilds'][ctx.guild.id]['media_channels'][channel.id]
emojis = settings['emojis']
emoji_str = await functions.get_emoji_str(ctx.guild, emojis)
string = f"{channel.mention}: {emoji_str}\n**----mediaOnly:** {settings['media_only']}"
embed = discord.Embed(title='Media Channels', description=string, color=0xFCFF00)
await ctx.send(embed=embed)
@media_channels.command(
name='add', aliases=['a', '+'], brief='Add a media channel', description='Add a media channel'
)
@commands.guild_only()
@commands.has_permissions(manage_channels=True)
async def add_media_channel(self, ctx, channel: discord.TextChannel):
if channel is None:
await ctx.send("I could not find a channel with that name or id")
return
if channel.id in dbh.database.db['guilds'][ctx.guild.id]['media_channels']:
await ctx.send("That is already a media channel")
return
if ctx.guild.id not in dbh.database.locks:
dbh.database.locks[ctx.guild.id] = Lock()
async with dbh.database.locks[ctx.guild.id]:
dbh.database.db['guilds'][ctx.guild.id]['media_channels'][channel.id] = {
'emojis': [],
'media_only': False
}
await ctx.send("Added media channel")
@media_channels.command(
name='remove', aliases=['r', '-'], brief='Remove media channel', description='Remove media channel'
)
@commands.guild_only()
@commands.has_permissions(manage_channels=True)
async def remove_media_channel(self, ctx, channel: ChannelElseInt):
if channel is None:
await ctx.send("I could not find a channel with that name or id")
return
channel_id = channel if isinstance(channel, int) else channel.id
if ctx.guild.id not in dbh.database.locks:
dbh.database.locks[ctx.guild.id] = Lock()
async with dbh.database.locks[ctx.guild.id]:
if channel_id not in dbh.database.db['guilds'][ctx.guild.id]['media_channels']:
await ctx.send("That does not appear to be a media channel")
return
del dbh.database.db['guilds'][ctx.guild.id]['media_channels'][channel_id]
await ctx.send(f"{channel if isinstance(channel, int) else channel.mention} is no longer a media channel")
@media_channels.command(
name='addemoji', aliases=['ae'], brief='Add an emoji to media channel',
description='Adds an emoji for the bot to automatically react to all messages sent in the media channel'
)
@commands.guild_only()
@commands.has_permissions(manage_channels=True)
async def media_channel_add_emoji(self, ctx, channel: discord.TextChannel, in_emoji):
emoji = await functions.get_emoji(ctx.guild, in_emoji)
if type(emoji) is discord.Emoji:
emoji = emoji.id
if channel is None:
await ctx.send("I could not find a channel with that name or id")
return
if ctx.guild.id not in dbh.database.locks:
dbh.database.locks[ctx.guild.id] = Lock()
async with dbh.database.locks[ctx.guild.id]:
if channel.id not in dbh.database.db['guilds'][ctx.guild.id]['media_channels']:
await ctx.send("That is not a media channel")
return
dbh.database.db['guilds'][ctx.guild.id]['media_channels'][channel.id]['emojis'].append(emoji)
await ctx.send(f"Added {in_emoji} to {channel.mention}")
@media_channels.command(
name='removeemoji', aliases=['re'], brief='Remove an emoji from media channel',
description='Remove an emoji from media channel'
)
@commands.guild_only()
@commands.has_permissions(manage_channels=True)
async def media_channel_remove_emoji(self, ctx, channel: discord.TextChannel, in_emoji):
emoji = await functions.get_emoji(ctx.guild, in_emoji)
if type(emoji) is discord.Emoji:
emoji = emoji.id
if channel is None:
await ctx.send("I could not find a channel with that name or id")
return
if ctx.guild.id not in dbh.database.locks:
dbh.database.locks[ctx.guild.id] = Lock()
async with dbh.database.locks[ctx.guild.id]:
if channel.id not in dbh.database.db['guilds'][ctx.guild.id]['media_channels']:
await ctx.send("That is not a media channel")
return
found = False
if emoji in dbh.database.db['guilds'][ctx.guild.id]['media_channels'][channel.id]['emojis']:
dbh.database.db['guilds'][ctx.guild.id]['media_channels'][channel.id]['emojis'].remove(emoji)
found = True
else:
try:
if int(emoji) in dbh.database.db['guilds'][ctx.guild.id]['media_channels'][channel.id]['emojis']:
dbh.database.db['guilds'][ctx.guild.id]['media_channels'][channel.id]['emojis'].remove(int(emoji))
found = True
except Exception as e:
print(e)
if found:
await ctx.send(f"Removed {in_emoji} from {channel.mention}")
else:
await ctx.send(f"Could not find that emoji in {channel.mention}")
@media_channels.command(
name='mediaonly', aliases=['mo', 'imagesonly'], brief='Set mediaOnly for media channel',
description='Set wether or not a media channel allows messages without images'
)
@commands.guild_only()
@commands.has_permissions(manage_channels=True, manage_messages=True)
async def media_channel_media_only(self, ctx, channel: discord.TextChannel, allow: bool):
if channel is None:
await ctx.send("I could not find a channel with that name or id")
return
if ctx.guild.id not in dbh.database.locks:
dbh.database.locks[ctx.guild.id] = Lock()
async with dbh.database.locks[ctx.guild.id]:
if channel.id not in dbh.database.db['guilds'][ctx.guild.id]['media_channels']:
await ctx.send("That is not a media channel")
return
dbh.database.db['guilds'][ctx.guild.id]['media_channels'][channel.id]['media_only'] = allow
await ctx.send(f"Set mediaOnly to {allow} for {channel.mention}")