-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathstarboard.py
272 lines (227 loc) · 9.53 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
import typing
from datetime import datetime
import discord
from discord import Client
from discord.ext import commands
from core import checks
from core.models import PermissionLevel, getLogger
logger = getLogger(__name__)
class Starboard(commands.Cog):
"""
Basically a starboard . Leave a ⭐ if you like this plugin https://github.com/officialpiyush/modmail-plugins
"""
def __init__(self, bot):
self.bot: Client = bot
self.db = bot.plugin_db.get_partition(self)
self.channel = None
self.stars = 2
self.user_blacklist: list = list()
self.channel_blacklist: list = list()
self.bot.loop.create_task(self._set_val())
async def _update_db(self):
await self.db.find_one_and_update(
{"_id": "config"},
{
"$set": {
"channel": self.channel,
"stars": self.stars,
"blacklist": {
"user": self.user_blacklist,
"channel": self.channel_blacklist,
},
}
},
upsert=True,
)
async def _set_val(self):
config = await self.db.find_one({"_id": "config"})
if config is None:
await self._update_db()
return
self.channel = config.get("channel", None)
self.stars = config.get("stars", 2)
self.user_blacklist = config["blacklist"]["user"]
self.channel_blacklist = config["blacklist"]["channel"]
@commands.group(aliases=["st", "sb"], invoke_without_command=True)
@checks.has_permissions(PermissionLevel.ADMIN)
async def starboard(self, ctx: commands.Context):
await ctx.send_help(ctx.command)
@starboard.command(aliases=["setchannel", "setch", "sc"])
@checks.has_permissions(PermissionLevel.ADMIN)
async def channel(self, ctx: commands.Context, channel: discord.TextChannel):
"""
Set the starboard channel where the messages will go
**Usage:**
starboard channel **#this-is-a-channel**
"""
self.channel = str(channel.id)
await self._update_db()
await ctx.send(f"Done! {channel.mention} is the Starboard Channel now!")
@starboard.command(aliases=["setstars", "ss"])
@checks.has_permissions(PermissionLevel.ADMIN)
async def stars(self, ctx: commands.Context, stars: int):
"""
Set the number of stars the message needs to appear on the starboard channel
**Usage:**
starboard stars 2
"""
self.stars = stars
await self._update_db()
await ctx.send(
f"Done.Now this server needs `{stars}` :star: to appear on the starboard channel."
)
@starboard.group()
@checks.has_permissions(PermissionLevel.ADMIN)
async def blacklist(self, ctx: commands.Context):
"""
Blacklist users and channels
"""
if ctx.invoked_subcommand is None:
await ctx.send_help()
@blacklist.command(aliases=["user"])
@checks.has_permissions(PermissionLevel.ADMIN)
async def member(self, ctx: commands.Context, member: discord.Member):
"""
Blacklist a user so that the user's reaction dosen't get counted
**Usage:**
starboard blacklist member @user
"""
if str(member.id) in self.user_blacklist:
self.user_blacklist.remove(str(member.id))
removed = True
else:
self.user_blacklist.append(str(member.id))
removed = False
await ctx.send(
f"{'Un' if removed else None}Blacklisted **{member.name}#{member.discriminator}**"
)
return
@blacklist.command(name="channel")
@checks.has_permissions(PermissionLevel.ADMIN)
async def blacklist_channel(
self, ctx: commands.Context, channel: discord.TextChannel
):
"""
Blacklist Channels so that messages sent in those channels dont appear on starboard
**Usage:**
starboard blacklist channel **#channel**
"""
if str(channel.id) in self.channel_blacklist:
self.channel_blacklist.remove(str(channel.id))
await self._update_db()
removed = True
else:
self.channel_blacklist.append(str(channel.id))
await self._update_db()
removed = False
await ctx.send(f"{'Un' if removed else None}Blacklisted {channel.mention}")
return
@commands.Cog.listener()
async def on_raw_reaction_remove(self, payload: discord.RawReactionActionEvent):
await self.handle_reaction(payload=payload)
@commands.Cog.listener()
async def on_raw_reaction_add(self, payload: discord.RawReactionActionEvent):
await self.handle_reaction(payload=payload)
async def handle_reaction(self, payload: discord.RawReactionActionEvent):
config = await self.db.find_one({"_id": "config"})
if not config or not self.channel:
logger.info("No config or channel")
return
# check for blacklist
if self.channel_blacklist.__contains__(str(payload.channel_id)) or self.user_blacklist.__contains__(
str(payload.user_id)):
logger.info("Blacklisted")
return
guild: discord.Guild = self.bot.get_guild(int(self.bot.config["guild_id"]))
starboard_channel: discord.TextChannel = guild.get_channel(int(self.channel))
channel: discord.TextChannel = guild.get_channel(payload.channel_id)
user: discord.User = await self.bot.fetch_user(payload.user_id)
if not channel or not starboard_channel:
logger.info("No channel found")
return
message: discord.Message = await channel.fetch_message(payload.message_id)
if message.author.id == payload.user_id:
logger.info("Author added the reaction")
return
found_emote = False
for emote in message.reactions:
if emote.emoji == "⭐":
found_emote = True
reaction: discord.Reaction = emote
count = reaction.count
reacted_users: typing.List[discord.User] = await reaction.users().flatten()
has_author_reacted = discord.utils.find(lambda u: u.id == message.author.id, reacted_users)
if has_author_reacted:
count = count - 1
should_delete = False
if count < self.stars:
should_delete = True
messages = await starboard_channel.history(
limit=70,
around=message.created_at
).flatten()
found = False
for msg in messages:
if len(msg.embeds) <= 0:
logger.info("No embeds")
continue
if not msg.embeds[0].footer or not msg.embeds[0].footer.text or "⭐" not in msg.embeds[
0].footer.text:
print(msg.embeds)
logger.info("No stars")
continue
if msg.embeds[0].footer.text.endswith(str(payload.message_id)):
logger.info("got one")
found = True
if should_delete:
logger.info("delete message")
await msg.delete()
break
e = msg.embeds[0]
e.set_footer(text=f"⭐ {count} | {payload.message_id}")
await msg.edit(content=f"<#{payload.channel_id}>", embed=e)
break
if not found:
if should_delete:
logger.info("Should Delete")
return
embed = discord.Embed(
color=discord.Colour.gold(),
description=message.content,
timestamp=datetime.utcnow(),
title="Jump to message ►",
url=message.jump_url
)
embed.set_author(
name=str(message.author),
icon_url=message.author.avatar_url,
)
embed.set_footer(text=f"⭐ {count} | {payload.message_id}")
if len(message.attachments) > 1:
try:
embed.set_image(url=message.attachments[0].url)
except:
pass
await starboard_channel.send(
f"{channel.mention}", embed=embed
)
if not found_emote:
messages = await starboard_channel.history(
limit=70,
around=message.created_at
).flatten()
found = False
for msg in messages:
if len(msg.embeds) <= 0:
logger.info("No embeds")
continue
if not msg.embeds[0].footer or not msg.embeds[0].footer.text or "⭐" not in msg.embeds[0].footer.text:
print(msg.embeds)
logger.info("No stars")
continue
if msg.embeds[0].footer.text.endswith(str(payload.message_id)):
logger.info("got one")
found = True
await msg.delete()
def setup(bot):
bot.add_cog(Starboard(bot))