forked from TrustyJAID/Trusty-cogs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstarboard.py
930 lines (871 loc) · 37.4 KB
/
starboard.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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
import asyncio
from datetime import timedelta
from typing import Dict, Optional, Union
import discord
from red_commons.logging import getLogger
from redbot.core import Config, checks, commands
from redbot.core.i18n import Translator, cog_i18n
from redbot.core.utils.chat_formatting import humanize_list, humanize_timedelta, pagify
from redbot.core.utils.views import SimpleMenu
from .converters import RealEmoji, StarboardExists
from .events import StarboardEvents
from .starboard_entry import FakePayload, StarboardEntry
_ = Translator("Starboard", __file__)
log = getLogger("red.trusty-cogs.Starboard")
TimeConverter = commands.converter.TimedeltaConverter(
minimum=timedelta(days=7), allowed_units=["days", "weeks"], default_unit="days"
)
@cog_i18n(_)
class Starboard(StarboardEvents, commands.Cog):
"""
Create a starboard to *pin* those special comments indefinitely
"""
__version__ = "2.6.0"
__author__ = "TrustyJAID"
def __init__(self, bot):
self.bot = bot
self.config = Config.get_conf(self, 356488795)
self.config.register_global(purge_time=None)
self.config.register_guild(starboards={})
self.starboards: Dict[int, Dict[str, StarboardEntry]] = {}
self.ready = asyncio.Event()
self.cleanup_loop: Optional[asyncio.Task] = None
async def cog_load(self) -> None:
log.debug("Started building starboards cache from config.")
for guild_id in await self.config.all_guilds():
self.starboards[guild_id] = {}
all_data = await self.config.guild_from_id(int(guild_id)).starboards()
for name, data in all_data.items():
try:
starboard = await StarboardEntry.from_json(data, guild_id)
except Exception:
log.exception("error converting starboard")
self.starboards[guild_id][name] = starboard
self.cleanup_loop = asyncio.create_task(self.cleanup_old_messages())
self.ready.set()
log.debug("Done building starboards cache from config.")
async def cog_unload(self) -> None:
self.ready.clear()
if self.cleanup_loop:
self.cleanup_loop.cancel()
async def cog_check(self, ctx: commands.Context) -> bool:
return self.ready.is_set()
def format_help_for_context(self, ctx: commands.Context) -> str:
"""
Thanks Sinbad!
"""
pre_processed = super().format_help_for_context(ctx)
return f"{pre_processed}\n\nCog Version: {self.__version__}"
@commands.group()
@checks.admin_or_permissions(manage_channels=True)
@commands.guild_only()
async def starboard(self, ctx: commands.Context) -> None:
"""
Commands for managing the starboard
"""
@starboard.command(name="purge")
@commands.is_owner()
async def purge_threshold(
self, ctx: commands.Context, *, time: TimeConverter = timedelta(seconds=0)
) -> None:
"""
Define how long to keep message ID's for every starboard
`<time>` is the number of days or weeks you want to keep starboard messages for.
e.g. `[p]starboard purge 2 weeks`
"""
if time.total_seconds() < 1:
await self.config.purge_time.clear()
await ctx.send(_("I will now keep message ID's indefinitely."))
return
await self.config.purge_time.set(int(time.total_seconds()))
await ctx.send(
_(
"I will now prun messages that are {time} "
"old or more every 24 hours.\n"
"This will take effect after the next reload."
).format(time=humanize_timedelta(timedelta=time))
)
async def format_starboard(
self, ctx: commands.Context, starboard: StarboardEntry
) -> discord.Embed:
guild = ctx.guild
embed = discord.Embed(colour=await ctx.bot.get_embed_colour(ctx.channel))
embed.title = _("Starboard settings for {guild}").format(guild=guild.name)
channel = guild.get_channel(starboard.channel)
s_channel = channel.mention if channel else "deleted_channel"
msg = _(
"Name: **{name}**\nEnabled: **{enabled}**\nEmoji: {emoji}\n"
"Channel: {channel}\nThreshold: **{threshold}**\n"
"{emoji} Messages: **{starred_messages}**\n"
"{emoji} Added: **{stars_added}**\nSelfstar: **{selfstar}**\n"
"Inherit from parent channel: **{inherit}**\n"
).format(
name=starboard.name,
enabled=starboard.enabled,
emoji=starboard.emoji,
channel=s_channel,
threshold=starboard.threshold,
starred_messages=starboard.starred_messages,
stars_added=starboard.stars_added,
selfstar=starboard.selfstar,
inherit=starboard.inherit,
)
if starboard.blacklist:
channels = [guild.get_channel(c) for c in starboard.blacklist]
roles = [guild.get_role(r) for r in starboard.blacklist]
chans = humanize_list([c.mention for c in channels if c is not None])
roles_str = humanize_list([r.mention for r in roles if r is not None])
if chans:
msg += _("Blocked Channels: {chans}\n").format(chans=chans)
if roles_str:
msg += _("Blocked roles: {roles}\n").format(roles=roles_str)
if starboard.whitelist:
channels = [guild.get_channel(c) for c in starboard.whitelist]
roles = [guild.get_role(r) for r in starboard.whitelist]
chans = humanize_list([c.mention for c in channels if c is not None])
roles_str = humanize_list([r.mention for r in roles if r is not None])
if chans:
msg += _("Allowed Channels: {chans}\n").format(chans=chans)
if roles_str:
msg += _("Allowed roles: {roles}\n").format(roles=roles_str)
count = 0
embed.description = ""
for page in pagify(msg, page_length=1024):
if count <= 1:
embed.description += msg
else:
embed.add_field(name=_("Starboard info continued"), value=page)
count += 1
return embed
@starboard.command(name="info", aliases=["list"])
@commands.bot_has_permissions(read_message_history=True, embed_links=True)
async def starboard_info(self, ctx: commands.Context) -> None:
"""
Display info on starboards setup on the server.
"""
guild = ctx.guild
await ctx.typing()
if guild.id in self.starboards:
pages = [
{"embed": await self.format_starboard(ctx, starboard)}
for starboard in self.starboards[guild.id].values()
]
await SimpleMenu(pages).start(ctx)
else:
await ctx.send(_("No Starboards exist on this server."))
@starboard.command(name="create", aliases=["add"])
async def setup_starboard(
self,
ctx: commands.Context,
name: str,
channel: Optional[discord.TextChannel] = None,
emoji: RealEmoji = "⭐",
) -> None:
"""
Create a starboard on this server
`<name>` is the name for the starboard and will be lowercase only
`[channel]` is the channel where posts will be made defaults to current channel
`[emoji=⭐]` is the emoji that will be used to add to the starboard defaults to ⭐
"""
guild = ctx.message.guild
name = name.lower()
if channel is None:
channel = ctx.message.channel
if type(emoji) == discord.Emoji:
if emoji not in guild.emojis:
await ctx.send(_("That emoji is not on this guild!"))
return
if not channel.permissions_for(guild.me).send_messages:
send_perms = _("I don't have permission to post in ")
await ctx.send(send_perms + channel.mention)
return
if not channel.permissions_for(guild.me).embed_links:
embed_perms = _("I don't have permission to embed links in ")
await ctx.send(embed_perms + channel.mention)
return
if len(self.starboards.get(guild.id, [])) < 1:
self.starboards[guild.id] = {}
starboards = self.starboards[guild.id]
if name in starboards:
await ctx.send(_("{name} starboard name is already being used").format(name=name))
return
starboard = StarboardEntry(name=name, channel=channel.id, emoji=str(emoji), guild=guild.id)
self.starboards[guild.id][name] = starboard
await self._save_starboards(guild)
msg = _("Starboard set to {channel} with emoji {emoji}").format(
channel=channel.mention, emoji=emoji
)
await ctx.send(msg)
@starboard.command(name="cleanup")
async def cleanup(self, ctx: commands.Context) -> None:
"""
Cleanup stored deleted channels or roles in the blocklist/allowlist
"""
guild = ctx.guild
if len(self.starboards.get(guild.id, [])) < 1:
await ctx.send(_("There are no Starboards setup on this server."))
return
channels = 0
boards = 0
for name, starboard in self.starboards[guild.id].items():
channel = guild.get_channel(starboard.channel)
if channel is None:
del self.starboards[guild.id][name]
boards += 1
continue
if starboard.blacklist:
for c in starboard.blacklist:
channel = guild.get_channel(c)
role = guild.get_role(c)
if channel is None and role is None:
self.starboards[guild.id][name].blacklist.remove(c)
channels += 1
if starboard.whitelist:
for c in starboard.whitelist:
channel = guild.get_channel(c)
role = guild.get_role(c)
if channel is None and role is None:
self.starboards[guild.id][name].whitelist.remove(c)
channels += 1
await self._save_starboards(guild)
msg = _(
"Removed {channels} channels and roles, and {boards} boards " "that no longer exist"
).format(channels=channels, boards=boards)
await ctx.send(msg)
@starboard.command(name="remove", aliases=["delete", "del"])
async def remove_starboard(
self, ctx: commands.Context, starboard: Optional[StarboardExists]
) -> None:
"""
Remove a starboard from the server
`<name>` is the name for the starboard and will be lowercase only
"""
guild = ctx.guild
if not starboard:
if len(self.starboards.get(guild.id, [])) < 1:
await ctx.send(_("There are no starboards setup on this server!"))
return
if len(self.starboards[guild.id]) > 1:
await ctx.send(
_(
"There's more than one starboard setup in this server. "
"Please provide a name for the starboard you wish to use."
)
)
return
starboard = list(self.starboards[guild.id].values())[0]
async with self.config.guild(guild).starboards() as starboards:
try:
del self.starboards[ctx.guild.id][starboard.name]
del starboards[starboard.name]
except Exception:
log.exception("Error removing starboard")
await ctx.send("Deleting the starboard failed.")
return
await ctx.send(_("Deleted starboard {name}").format(name=starboard.name))
@commands.command()
@commands.guild_only()
async def star(
self,
ctx: commands.Context,
starboard: Optional[StarboardExists],
message: discord.Message,
) -> None:
"""
Manually star a message
`<name>` is the name of the starboard you would like to add the message to
`<message>` is the message ID, `channel_id-message_id`, or a message link
of the message you want to star
"""
guild = ctx.guild
if not starboard:
if len(self.starboards.get(guild.id, [])) < 1:
await ctx.send(_("There are no starboards setup on this server!"))
return
if len(self.starboards[guild.id]) > 1:
await ctx.send(
_(
"There's more than one starboard setup in this server. "
"Please provide a name for the starboard you wish to use."
)
)
return
starboard = list(self.starboards[guild.id].values())[0]
if message.guild and message.guild.id != guild.id:
await ctx.send(_("I cannot star messages from another server."))
return
if not starboard.enabled:
error_msg = _("Starboard {name} isn't enabled.").format(name=starboard.name)
await ctx.send(error_msg)
return
if not starboard.check_roles(ctx.message.author):
error_msg = _(
"One of your roles is blocked on {starboard} "
"or you don't have a role that is allowed."
).format(starboard=starboard.name)
await ctx.send(error_msg)
return
if not starboard.check_channel(self.bot, message.channel):
error_msg = _(
"That messages channel is either blocked, not "
"in the allowlist, or designated NSFW while the "
"{starboard} channel is not designated as NSFW."
).format(starboard=starboard.name)
await ctx.send(error_msg)
return
fake_payload = FakePayload(
guild_id=guild.id,
message_id=message.id,
channel_id=message.channel.id,
user_id=ctx.author.id,
emoji=starboard.emoji,
event_type="REACTION_ADD",
)
await self._update_stars(fake_payload)
@commands.command()
@commands.guild_only()
async def unstar(
self,
ctx: commands.Context,
starboard: Optional[StarboardExists],
message: discord.Message,
) -> None:
"""
Manually unstar a message
`<name>` is the name of the starboard you would like to add the message to
`<message>` is the message ID, `channe_id-message_id`, or a message link
of the message you want to unstar
"""
guild = ctx.guild
if not starboard:
if len(self.starboards.get(guild.id, [])) < 1:
await ctx.send(_("There are no starboards setup on this server!"))
return
if len(self.starboards[guild.id]) > 1:
await ctx.send(
_(
"There's more than one starboard setup in this server. "
"Please provide a name for the starboard you wish to use."
)
)
return
starboard = list(self.starboards[guild.id].values())[0]
if message.guild and message.guild.id != guild.id:
await ctx.send(_("I cannot star messages from another server."))
return
if not starboard.enabled:
error_msg = _("Starboard {name} isn't enabled.").format(name=starboard.name)
await ctx.send(error_msg)
return
if not starboard.check_roles(ctx.message.author):
error_msg = _(
"One of your roles is blocked on {starboard} "
"or you don't have a role that is allowed."
).format(starboard=starboard.name)
await ctx.send(error_msg)
return
if not starboard.check_channel(self.bot, message.channel):
error_msg = _(
"That messages channel is either blocked, not "
"in the allowlist, or designated NSFW while the "
"{starboard} channel is not designated as NSFW."
).format(starboard=starboard.name)
await ctx.send(error_msg)
return
fake_payload = FakePayload(
guild_id=guild.id,
message_id=message.id,
channel_id=message.channel.id,
user_id=ctx.author.id,
emoji=starboard.emoji,
event_type="REACTION_REMOVE",
)
await self._update_stars(fake_payload)
@starboard.group(name="allowlist", aliases=["whitelist"])
async def whitelist(self, ctx: commands.Context) -> None:
"""Add/Remove channels/roles from the allowlist"""
pass
@starboard.group(name="blocklist", aliases=["blacklist"])
async def blacklist(self, ctx: commands.Context) -> None:
"""Add/Remove channels/roles from the blocklist"""
pass
@blacklist.command(name="add")
async def blacklist_add(
self,
ctx: commands.Context,
starboard: Optional[StarboardExists],
channel_or_role: Union[
discord.TextChannel,
discord.CategoryChannel,
discord.Thread,
discord.ForumChannel,
discord.VoiceChannel,
discord.Role,
],
) -> None:
"""
Add a channel to the starboard blocklist
`<name>` is the name of the starboard to adjust
`<channel_or_role>` is the channel or role you would like to add to the blocklist
"""
guild = ctx.guild
if not starboard:
if len(self.starboards.get(guild.id, [])) < 1:
await ctx.send(_("There are no starboards setup on this server!"))
return
if len(self.starboards[guild.id]) > 1:
await ctx.send(
_(
"There's more than one starboard setup in this server. "
"Please provide a name for the starboard you wish to use."
)
)
return
starboard = list(self.starboards[guild.id].values())[0]
if channel_or_role.id in starboard.blacklist:
msg = _("{channel_or_role} is already blocked for starboard {name}").format(
channel_or_role=channel_or_role.mention, name=starboard.name
)
else:
self.starboards[ctx.guild.id][starboard.name].blacklist.append(channel_or_role.id)
await self._save_starboards(guild)
msg = _("{channel_or_role} blocked on starboard {name}").format(
channel_or_role=channel_or_role.mention, name=starboard.name
)
await ctx.send(msg, allowed_mentions=discord.AllowedMentions(roles=False))
@blacklist.command(name="remove")
async def blacklist_remove(
self,
ctx: commands.Context,
starboard: Optional[StarboardExists],
channel_or_role: Union[
discord.TextChannel,
discord.CategoryChannel,
discord.Thread,
discord.ForumChannel,
discord.VoiceChannel,
discord.Role,
],
) -> None:
"""
Remove a channel to the starboard blocklist
`<name>` is the name of the starboard to adjust
`<channel_or_role>` is the channel or role you would like to remove from the blocklist
"""
guild = ctx.guild
if not starboard:
if len(self.starboards.get(guild.id, [])) < 1:
await ctx.send(_("There are no starboards setup on this server!"))
return
if len(self.starboards[guild.id]) > 1:
await ctx.send(
_(
"There's more than one starboard setup in this server. "
"Please provide a name for the starboard you wish to use."
)
)
return
starboard = list(self.starboards[guild.id].values())[0]
if channel_or_role.id not in starboard.blacklist:
msg = _("{channel_or_role} is not on the blocklist for starboard {name}").format(
channel_or_role=channel_or_role.mention, name=starboard.name
)
await ctx.send(msg, allowed_mentions=discord.AllowedMentions(roles=False))
return
else:
self.starboards[ctx.guild.id][starboard.name].blacklist.remove(channel_or_role.id)
await self._save_starboards(guild)
msg = _("{channel_or_role} removed from the blocklist on starboard {name}").format(
channel_or_role=channel_or_role.mention, name=starboard.name
)
await ctx.send(msg, allowed_mentions=discord.AllowedMentions(roles=False))
@starboard.command(name="inherit")
async def inherit(
self,
ctx: commands.Context,
starboard: Optional[StarboardExists],
) -> None:
"""
Set whether to inherit the parent channels blocklist/allowlist settings.
If this is enabled then starred messages in threads and forum channels
will be filtered based on their parent channels blocklist/allowlist settings.
e.g. if a message is starred in a thread and the parent channel is in the blocklist
the message will not be starred.
`<name>` is the name of the starboard to adjust
"""
guild = ctx.guild
if not starboard:
if len(self.starboards.get(guild.id, [])) < 1:
await ctx.send(_("There are no starboards setup on this server!"))
return
if len(self.starboards[guild.id]) > 1:
await ctx.send(
_(
"There's more than one starboard setup in this server. "
"Please provide a name for the starboard you wish to use."
)
)
return
starboard = list(self.starboards[guild.id].values())[0]
starboard.inherit = not starboard.inherit
await self._save_starboards(guild)
if starboard.inherit:
msg = _("Starboard {name} will now inherit parent channel settings.").format(
name=starboard.name
)
else:
msg = _("Starboard {name} will not check if the parent channel is blocked.").format(
name=starboard.name
)
await ctx.send(msg)
@whitelist.command(name="add")
async def whitelist_add(
self,
ctx: commands.Context,
starboard: Optional[StarboardExists],
channel_or_role: Union[
discord.TextChannel,
discord.CategoryChannel,
discord.Thread,
discord.ForumChannel,
discord.VoiceChannel,
discord.Role,
],
) -> None:
"""
Add a channel to the starboard allowlist
`<name>` is the name of the starboard to adjust
`<channel_or_role>` is the channel or role you would like to add to the allowlist
"""
guild = ctx.guild
if not starboard:
if len(self.starboards.get(guild.id, [])) < 1:
await ctx.send(_("There are no starboards setup on this server!"))
return
if len(self.starboards[guild.id]) > 1:
await ctx.send(
_(
"There's more than one starboard setup in this server. "
"Please provide a name for the starboard you wish to use."
)
)
return
starboard = list(self.starboards[guild.id].values())[0]
if channel_or_role.id in starboard.whitelist:
msg = _("{channel_or_role} is already allowed for starboard {name}").format(
channel_or_role=channel_or_role.mention, name=starboard.name
)
await ctx.send(msg, allowed_mentions=discord.AllowedMentions(roles=False))
return
else:
self.starboards[ctx.guild.id][starboard.name].whitelist.append(channel_or_role.id)
await self._save_starboards(guild)
msg = _("{channel_or_role} allowed on starboard {name}").format(
channel_or_role=channel_or_role.mention, name=starboard.name
)
await ctx.send(msg, allowed_mentions=discord.AllowedMentions(roles=False))
if isinstance(channel_or_role, discord.TextChannel):
star_channel = ctx.guild.get_channel(starboard.channel)
if channel_or_role.is_nsfw() and not star_channel.is_nsfw():
await ctx.send(
_(
"The channel you have provided is designated "
"as NSFW but your starboard channel is not. "
"They will both need to be set the same "
"in order for this to work properly."
)
)
@whitelist.command(name="remove")
async def whitelist_remove(
self,
ctx: commands.Context,
starboard: Optional[StarboardExists],
channel_or_role: Union[
discord.TextChannel,
discord.CategoryChannel,
discord.Thread,
discord.ForumChannel,
discord.VoiceChannel,
discord.Role,
],
) -> None:
"""
Remove a channel to the starboard allowlist
`<name>` is the name of the starboard to adjust
`<channel_or_role>` is the channel or role you would like to remove from the allowlist
"""
guild = ctx.guild
if not starboard:
if len(self.starboards.get(guild.id, [])) < 1:
await ctx.send(_("There are no starboards setup on this server!"))
return
if len(self.starboards[guild.id]) > 1:
await ctx.send(
_(
"There's more than one starboard setup in this server. "
"Please provide a name for the starboard you wish to use."
)
)
return
starboard = list(self.starboards[guild.id].values())[0]
if channel_or_role.id not in starboard.whitelist:
msg = _("{channel_or_role} is not on the allowlist for starboard {name}").format(
channel_or_role=channel_or_role.mention, name=starboard.name
)
await ctx.send(msg, allowed_mentions=discord.AllowedMentions(roles=False))
return
else:
self.starboards[ctx.guild.id][starboard.name].whitelist.remove(channel_or_role.id)
await self._save_starboards(guild)
msg = _("{channel_or_role} removed from the allowlist on starboard {name}").format(
channel_or_role=channel_or_role.mention, name=starboard.name
)
await ctx.send(msg, allowed_mentions=discord.AllowedMentions(roles=False))
@starboard.command(name="channel", aliases=["channels"])
async def change_channel(
self,
ctx: commands.Context,
starboard: Optional[StarboardExists],
channel: discord.TextChannel,
) -> None:
"""
Change the channel that the starboard gets posted to
`<name>` is the name of the starboard to adjust
`<channel>` The channel of the starboard.
"""
guild = ctx.guild
if not starboard:
if len(self.starboards.get(guild.id, [])) < 1:
await ctx.send(_("There are no starboards setup on this server!"))
return
if len(self.starboards[guild.id]) > 1:
await ctx.send(
_(
"There's more than one starboard setup in this server. "
"Please provide a name for the starboard you wish to use."
)
)
return
starboard = list(self.starboards[guild.id].values())[0]
if not channel.permissions_for(guild.me).send_messages:
send_perms = _("I don't have permission to post in ")
await ctx.send(send_perms + channel.mention)
return
if not channel.permissions_for(guild.me).embed_links:
embed_perms = _("I don't have permission to embed links in ")
await ctx.send(embed_perms + channel.mention)
return
if channel.id == starboard.channel:
msg = _("Starboard {name} is already posting in {channel}").format(
name=starboard.name, channel=channel.mention
)
await ctx.send(msg)
return
self.starboards[ctx.guild.id][starboard.name].channel = channel.id
await self._save_starboards(guild)
msg = _("Starboard {name} set to post in {channel}").format(
name=starboard.name, channel=channel.mention
)
await ctx.send(msg)
@starboard.command(name="toggle")
async def toggle_starboard(
self, ctx: commands.Context, starboard: Optional[StarboardExists]
) -> None:
"""
Toggle a starboard on/off
`<name>` is the name of the starboard to toggle
"""
guild = ctx.guild
if not starboard:
if len(self.starboards.get(guild.id, [])) < 1:
await ctx.send(_("There are no starboards setup on this server!"))
return
if len(self.starboards[guild.id]) > 1:
await ctx.send(
_(
"There's more than one starboard setup in this server. "
"Please provide a name for the starboard you wish to use."
)
)
return
starboard = list(self.starboards[guild.id].values())[0]
if starboard.enabled:
msg = _("Starboard {name} disabled.").format(name=starboard.name)
else:
msg = _("Starboard {name} enabled.").format(name=starboard.name)
self.starboards[ctx.guild.id][starboard.name].enabled = not starboard.enabled
await self._save_starboards(guild)
await ctx.send(msg)
@starboard.command(name="selfstar")
async def toggle_selfstar(
self, ctx: commands.Context, starboard: Optional[StarboardExists]
) -> None:
"""
Toggle whether or not a user can star their own post
`<name>` is the name of the starboard to toggle
"""
guild = ctx.guild
if not starboard:
if len(self.starboards.get(guild.id, [])) < 1:
await ctx.send(_("There are no starboards setup on this server!"))
return
if len(self.starboards[guild.id]) > 1:
await ctx.send(
_(
"There's more than one starboard setup in this server. "
"Please provide a name for the starboard you wish to use."
)
)
return
starboard = list(self.starboards[guild.id].values())[0]
if starboard.selfstar:
msg = _("Selfstarring on starboard {name} disabled.").format(name=starboard.name)
else:
msg = _("Selfstarring on starboard {name} enabled.").format(name=starboard.name)
self.starboards[ctx.guild.id][starboard.name].selfstar = not starboard.selfstar
await self._save_starboards(guild)
await ctx.send(msg)
@starboard.command(name="autostar")
async def toggle_autostar(
self, ctx: commands.Context, starboard: Optional[StarboardExists]
) -> None:
"""
Toggle whether or not the bot will add the emoji automatically to the starboard message.
`<name>` is the name of the starboard to toggle
"""
guild = ctx.guild
if not starboard:
if len(self.starboards.get(guild.id, [])) < 1:
await ctx.send(_("There are no starboards setup on this server!"))
return
if len(self.starboards[guild.id]) > 1:
await ctx.send(
_(
"There's more than one starboard setup in this server. "
"Please provide a name for the starboard you wish to use."
)
)
return
starboard = list(self.starboards[guild.id].values())[0]
if starboard.autostar:
msg = _("Autostarring on starboard {name} disabled.").format(name=starboard.name)
else:
msg = _("Autostarring on starboard {name} enabled.").format(name=starboard.name)
self.starboards[ctx.guild.id][starboard.name].autostar = not starboard.autostar
await self._save_starboards(guild)
await ctx.send(msg)
@starboard.command(name="colour", aliases=["color"])
async def colour_starboard(
self,
ctx: commands.Context,
starboard: Optional[StarboardExists],
colour: Union[discord.Colour, str],
) -> None:
"""
Change the default colour for a starboard
`<name>` is the name of the starboard to toggle
`<colour>` The colour to use for the starboard embed
This can be a hexcode or integer for colour or `author/member/user` to use
the original posters colour or `bot` to use the bots colour.
Colour also accepts names from
[discord.py](https://discordpy.readthedocs.io/en/latest/api.html#colour)
"""
guild = ctx.guild
if not starboard:
if len(self.starboards.get(guild.id, [])) < 1:
await ctx.send(_("There are no starboards setup on this server!"))
return
if len(self.starboards[guild.id]) > 1:
await ctx.send(
_(
"There's more than one starboard setup in this server. "
"Please provide a name for the starboard you wish to use."
)
)
return
starboard = list(self.starboards[guild.id].values())[0]
if isinstance(colour, str):
colour = colour.lower()
if colour not in ["user", "member", "author", "bot"]:
await ctx.send(_("The provided colour option is not valid."))
return
else:
starboard.colour = colour
else:
self.starboards[ctx.guild.id][starboard.name].colour = colour.value
await self._save_starboards(guild)
msg = _("Starboard `{name}` colour set to `{colour}`.").format(
name=starboard.name, colour=starboard.colour
)
await ctx.send(msg)
@starboard.command(name="emoji")
async def set_emoji(
self,
ctx: commands.Context,
starboard: Optional[StarboardExists],
emoji: RealEmoji,
) -> None:
"""
Set the emoji for the starboard
`<name>` is the name of the starboard to change the emoji for
`<emoji>` must be an emoji on the server or a default emoji
"""
guild = ctx.guild
if not starboard:
if len(self.starboards.get(guild.id, [])) < 1:
await ctx.send(_("There are no starboards setup on this server!"))
return
if len(self.starboards[guild.id]) > 1:
await ctx.send(
_(
"There's more than one starboard setup in this server. "
"Please provide a name for the starboard you wish to use."
)
)
return
starboard = list(self.starboards[guild.id].values())[0]
if type(emoji) == discord.Emoji:
if emoji not in guild.emojis:
await ctx.send(_("That emoji is not on this guild!"))
return
self.starboards[ctx.guild.id][starboard.name].emoji = discord.PartialEmoji.from_str(
str(emoji)
)
await self._save_starboards(guild)
msg = _("{emoji} set for starboard {name}").format(emoji=emoji, name=starboard.name)
await ctx.send(msg)
@starboard.command(name="threshold")
async def set_threshold(
self,
ctx: commands.Context,
starboard: Optional[StarboardExists],
threshold: int,
) -> None:
"""
Set the threshold before posting to the starboard
`<name>` is the name of the starboard to change the threshold for
`<threshold>` must be a number of reactions before a post gets
moved to the starboard
"""
guild = ctx.guild
if not starboard:
if len(self.starboards.get(guild.id, [])) < 1:
await ctx.send(_("There are no starboards setup on this server!"))
return
if len(self.starboards[guild.id]) > 1:
await ctx.send(
_(
"There's more than one starboard setup in this server. "
"Please provide a name for the starboard you wish to use."
)
)
return
starboard = list(self.starboards[guild.id].values())[0]
if threshold <= 0:
threshold = 1
self.starboards[ctx.guild.id][starboard.name].threshold = threshold
await self._save_starboards(guild)
msg = _("Threshold of {threshold} reactions set for {name}").format(
threshold=threshold, name=starboard.name
)
await ctx.send(msg)