-
Notifications
You must be signed in to change notification settings - Fork 0
/
messagerater.py
500 lines (457 loc) · 23.4 KB
/
messagerater.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
import discord # It's dangerous to go alone! Take this. /ref
from discord import app_commands # v2.0, use slash commands
from discord.ext import commands # required for client bot making
import pymongo # for online database
from pymongo import MongoClient
import sys # kill switch for messagerater (search for :kill)
from datetime import datetime, timedelta
from time import mktime # for unix time code, tracking how often guilds were updated
from utils import *
mongoURI = open("mongo.txt","r").read()
cluster = MongoClient(mongoURI)
RaterDB = cluster["RaterDB"]
version = "1.0.1"
intents = discord.Intents.default()
intents.message_content = True
intents.emojis_and_stickers = True
intents.guilds = True
class Bot(commands.Bot):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
logChannel: discord.TextChannel
client = Bot(
intents = intents,
command_prefix = "/!\"@:\\#", #unnecessary, but needs to be set so.. uh.. yeah. Unnecessary terminal warnings avoided.
case_insensitive=True,
# activity = discord.Game(name="with slash (/) commands!"),
allowed_mentions = discord.AllowedMentions(everyone = False)
)
@client.event
async def on_ready():
print(f"[#] Logged in as {client.user}, in version {version}")#,color="green")
await client.tree.sync()
# await client.logChannel.send(f":white_check_mark: **Started ChannelTracker** in version {version}")
@client.event
async def setup_hook():
client.RaterDB = RaterDB
@client.tree.command(name="update",description="Update slash-commands")
async def updateCmds(itx: discord.Interaction):
if not isAdmin(itx):
await itx.response.send_message("You don't have the right role to be update the slash commands! (to prevent ratelimiting)",ephemeral=True) #todo
return
await client.tree.sync()
# commandList = await client.tree.fetch_commands()
# client.commandList = commandList
await itx.response.send_message("Updated commands")
messageIdMarkedForDeletion = []
async def updateStat(star_message, starboard_emoji):
# find original message
text = star_message.embeds[0].fields[0].value ## "[Jump!]({msgLink})"
link = text.split("(")[1][:-1]
# https: 0 / 1 / discord.com 2 / channels 3 / 985931648094834798 4 / 1006682505149169694 5 / 1014887159485968455 6
guild_id, channel_id, message_id = [int(i) for i in link.split("/")[4:]]
try:
ch = client.get_channel(channel_id)
original_message = await ch.fetch_message(message_id)
except discord.NotFound:
# if original message removed, remove starboard message
await logMsg(star_message.guild, f"{starboard_emoji} :x: Starboard message {star_message.id} was removed (from {message_id}) (original message could not be found)")
messageIdMarkedForDeletion.append(star_message.id)
await star_message.delete()
return
# star_stat_message = 0
# reactionTotal = 0
# # get message stars excluding Rina's
# for reaction in original_message.reactions:
# try:
# if str(reaction.emoji) == starboard_emoji:
# star_stat_message += reaction.count
# if reaction.me:
# star_stat_message -= 1
# except AttributeError: #is not a custom emoji
# pass
#
# star_stat_starboard = 0
# # get starboard stars excluding Rina's
# for reaction in star_message.reactions:
# try:
# if str(reaction.emoji) == starboard_emoji:
# star_stat_starboard += reaction.count
# if reaction.me:
# star_stat_starboard -= 1
# except AttributeError: #is not a custom emoji
# pass
#
# if star_stat_starboard > star_stat_message:
# star_stat = star_stat_starboard
# else:
# star_stat = star_stat_message
#
# for reaction in star_message.reactions:
# if reaction.emoji == '❌':
# reactionTotal = star_stat + reaction.count - 1 # stars (exc. rina) + x'es - rina's x
# star_stat -= reaction.count
# if reaction.me:
# star_stat += 1
#
# #if more x'es than stars, and more than 15 reactions, remove message
# if star_stat < 0 and reactionTotal > 10:
# await logMsg(star_message.guild, f"{starboard_emoji} :x: Starboard message {star_message.id} was removed (from {message_id}) (too many downvotes! Score: {star_stat}, Votes: {reactionTotal})")
# await star_message.delete()
# return
#
# # update message to new star value
# parts = star_message.content.split("**")
# parts[1] = str(star_stat)
# new_content = '**'.join(parts)
# await star_message.edit(content=new_content)
@client.event
async def on_raw_reaction_add(payload):
if payload.member.id == client.user.id:
return
#get the message id from payload.message_id through the channel (with payload.channel_id) (oof lengthy process)
ch = client.get_channel(payload.channel_id)
message = await ch.fetch_message(payload.message_id)
collection = RaterDB["guild_info"]
query = {"guild_id": message.guild.id}
guild = collection.find_one(query)
if guild is None:
# can't send logging message, because we have no clue what channel that's supposed to be Xd
debug("Not enough data is configured to work with the starboard. Please fix this with `/config`!",color="red")
return
try:
_star_channel = guild["starboard_channel"]
_rated_channel = guild["rated_channel"]
star_minimum = guild["starboard_min_upvotes"]
starboard_emoji = guild["starboard_emoji"]
starboard_embed_color = guild["starboard_embed_color"]
starboard_caption = guild["starboard_caption"]
starboard_image_only = guild["starboard_image_only"]
starboard_upvote_emoji = guild["starboard_upvote_emoji"]
starboard_downvote_emoji = guild["starboard_downvote_emoji"]
allow_downvotes = guild["starboard_allow_downvotes"]
except KeyError:
raise KeyError("Not enough data is configured to do add an item to the starboard! Please fix this with `/config`!")
star_channel = client.get_channel(_star_channel)
rated_channel = client.get_channel(_rated_channel)
if message.channel.id == star_channel.id:
await updateStat(message, starboard_emoji)
return
if message.channel.id != rated_channel.id:
return
for reaction in message.reactions:
try:
reaction.emoji
except AttributeError:
return
if str(reaction.emoji) == starboard_emoji:
if reaction.me:
# check if this message is already in the starboard. If so, update it
async for star_message in star_channel.history(limit=200):
for embed in star_message.embeds:
if embed.footer.text == str(message.id):
await updateStat(star_message, starboard_emoji)
return
return
elif reaction.count == star_minimum:
if message.author == client.user:
#can't starboard Rina's message
return
for attachment in message.attachments:
if attachment.height:
# is image or video
break #skips the for-loop's "else" statement, thus continuing the function
else:
if starboard_image_only:
return
msgLink = f"https://discord.com/channels/{message.guild.id}/{message.channel.id}/{message.id}"
embed = discord.Embed(
color=discord.Colour.from_rgb(r=starboard_embed_color[0], g=starboard_embed_color[1], b=starboard_embed_color[2]),
title='',
description=f"{message.content}",
timestamp=datetime.now()
)
embed.add_field(name="Source", value=f"[Jump!]({msgLink})")
embed.set_footer(text=f"{message.id}")
for attachment in message.attachments:
if attachment.height: #is image or video
embed.set_image(url=attachment.url)
break
# can only set one image per embed.. :/
try:
name = message.author.nick
except AttributeError:
name = message.author.name
embed.set_author(
name=f"{name}",
url="https://amitrans.org/", #todo
icon_url=message.author.display_avatar.url
)
msg = await star_channel.send(
starboard_caption,
embed=embed,
allowed_mentions=discord.AllowedMentions.none(),
)
await logMsg(star_channel.guild, f"{starboard_emoji} Starboard message {msg.jump_url} was created from {message.jump_url}. Content: \"\"\"{message.content}\"\"\" and attachments: {[x.url for x in message.attachments]}")
# add initial star reaction to starboarded message, and new starboard msg
# add star reaction to original message to prevent message from being re-added to the starboard
await message.add_reaction(starboard_emoji)
await msg.add_reaction(starboard_upvote_emoji)
if allow_downvotes:
await msg.add_reaction(starboard_downvote_emoji)
@client.event
async def on_raw_reaction_remove(payload):
#get the message id from payload.message_id through the channel (with payload.channel_id) (oof lengthy process)
ch = client.get_channel(payload.channel_id)
message = await ch.fetch_message(payload.message_id)
collection = RaterDB["guild_info"]
query = {"guild_id": message.guild.id}
guild = collection.find_one(query)
if guild is None:
# can't send logging message, because we have no clue what channel that's supposed to be Xd
debug("Not enough data is configured to work with the starboard! Please fix this with `/config`!",color="red")
return
try:
_star_channel = guild["starboard_channel"]
_rated_channel = guild["rated_channel"]
starboard_emoji = guild["starboard_emoji"]
except KeyError:
raise KeyError("Not enough data is configured to .. remove a star from an item on the starboard because idk what channel i need to look in! Please fix this with `/config`!")
star_channel = client.get_channel(_star_channel)
rated_channel = client.get_channel(_rated_channel)
if message.channel.id == star_channel.id:
await updateStat(message, starboard_emoji)
return
if message.channel.id != rated_channel.id:
return
for reaction in message.reactions:
if str(reaction.emoji) == starboard_emoji:
if reaction.me:
# check if this message is already in the starboard. If so, update it
async for star_message in star_channel.history(limit=500):
for embed in star_message.embeds:
if embed.footer.text == str(message.id):
await updateStat(star_message, starboard_emoji)
return
@client.event
async def on_raw_message_delete(message_payload):
collection = RaterDB["guild_info"]
query = {"guild_id": message_payload.guild_id}
guild = collection.find_one(query)
if guild is None:
debug("Not enough data is configured to work with the starboard! Please fix this with `/config`!",color="red")
return
try:
_star_channel = guild["starboard_channel"]
starboard_emoji = guild["starboard_emoji"]
except KeyError:
raise KeyError("Not enough data is configured to .. check starboard for a message matching the deleted message's ID, because idk what channel i need to look in! Please fix this with `/config`!")
star_channel = client.get_channel(_star_channel)
if message_payload.message_id in messageIdMarkedForDeletion: #global variable
messageIdMarkedForDeletion.remove(message_payload.message_id)
return
if message_payload.channel_id == star_channel.id:
# check if the deleted message is a starboard message; if so, log it at starboard message deletion
await logMsg(star_channel.guild, f"{starboard_emoji} :x: Starboard message was removed (from {message_payload.message_id}) (Starboard message was deleted manually).")
return
elif message_payload.channel_id != star_channel.id:
# check if this message's is in the starboard. If so, delete it
async for star_message in star_channel.history(limit=400):
for embed in star_message.embeds:
if embed.footer.text == str(message_payload.message_id):
try:
image = star_message.embeds[0].image.url
except AttributeError:
image = ""
try:
msg_link = str(message_payload.message_id)+" | "+(await client.get_channel(message_payload.channel_id).fetch_message(message_payload.message_id)).jump_url
except discord.NotFound:
msg_link = str(message_payload.message_id)+" (couldn't get jump link)"
await logMsg(star_channel.guild, f"{starboard_emoji} :x: Starboard message {star_message.id} was removed (from {msg_link}) (original message was removed (this starboard message's linked id matched the removed message's)). Content: \"\"\"{star_message.embeds[0].description}\"\"\" and attachment: {image}")
await star_message.delete()
return
@client.tree.command(name="config",description="Edit message rater settings (staff only)")
@app_commands.choices(mode=[
discord.app_commands.Choice(name='Rated channel (which channel do I look in?)', value=1),
discord.app_commands.Choice(name="Log (which channel logs the starboarded messages?)", value=2),
discord.app_commands.Choice(name='Starboard channel (Where are starboard messages sent?)', value=3),
discord.app_commands.Choice(name='Star minimum (How many stars should a message get before starboarded', value=4),
discord.app_commands.Choice(name='Starboard emoji (What emoji do you need to react with to starboard it)', value=5),
discord.app_commands.Choice(name='Upvote emoji (What emoji do you need to react with to upvote a starboarded message)', value=6),
discord.app_commands.Choice(name='Downvote emoji (What emoji do you need to react with to downvote a starboarded message)', value=7),
discord.app_commands.Choice(name='AllowDownvotes (Do you want downvotes to be added/have a function in messages?', value=8),
discord.app_commands.Choice(name='ImageOnly (Do you want non-images/non-attachment to be starboarded?)', value=9),
discord.app_commands.Choice(name='Message caption (What do you want to say at the top of the starboard message embed?)', value=10),
discord.app_commands.Choice(name='Embed color (What color do you want the embed to be?)', value=11),
])
@app_commands.describe(mode="What mode do you want to use?",
value="Fill in the value/channel-id of the thing/channel you want to edit")
async def config(itx: discord.Interaction, mode: int, value: str):
if not isAdmin(itx):
await itx.response.send_message("You don't have the right role to be able to execute this command! (sorrryyy)",ephemeral=True) #todo
return
query = {"guild_id": itx.guild_id}
collection = RaterDB["guild_info"]
modes = [
"", #0
"rated channel", #1
"log", #2
"starboard channel", #3
"star minimum", #4
"starboard emoji", #5
"upvote emoji", #6
"downvote emoji", #7
"allow downvotes", #8
"image only", #9
"message caption", #10
"embed color", #11
]
mode = modes[mode]
warning = ""
async def format(value, type, error_msg=None):
if type is int:
try:
return int(value)
except ValueError:
pass
elif type is float:
try:
return float(value)
except ValueError:
pass
elif type is bool:
true = ["true","1","yes","y","apply","allow","stroue","correct"]
if value.lower() in true:
return True
return False
if error_msg is not None:
await itx.response.send_message(error_msg, ephemeral=True)
return None
if mode == "rated channel":
if value is not None:
value = await format(value, int, error_msg="You have to give a numerical ID for the channel you want to use!")
if value is None:
return
ch = client.get_channel(value)
if type(ch) is not discord.TextChannel:
await itx.response.send_message(f"The ID you gave wasn't for the type of channel I was looking for! (Need <class 'discord.TextChannel'>, got {type(ch)})", ephemeral=True)
return
collection.update_one(query, {"$set": {"rated_channel": ch.id}}, upsert=True)
value = ch.id
elif mode == "log":
if value is not None:
value = await format(value, int, error_msg="You have to give a numerical ID for the channel you want to use!")
if value is None:
return
ch = client.get_channel(value)
if type(ch) is not discord.TextChannel:
await itx.response.send_message(f"The ID you gave wasn't for the type of channel I was looking for! (Need <class 'discord.TextChannel'>, got {type(ch)})", ephemeral=True)
return
collection.update_one(query, {"$set": {"log_channel": ch.id}}, upsert=True)
value = ch.id
elif mode == "starboard channel":
if value is not None:
value = await format(value, int, error_msg="You have to give a numerical ID for the channel you want to use!")
if value is None:
return
ch = client.get_channel(value)
if type(ch) is not discord.TextChannel:
await itx.response.send_message(f"The ID you gave wasn't for the type of channel I was looking for! (Need <class 'discord.TextChannel'>, got {type(ch)})", ephemeral=True)
return
collection.update_one(query, {"$set": {"starboard_channel": ch.id}}, upsert=True)
value = ch.id
elif mode == "star minimum":
if value is not None:
value = await format(value, int, error_msg="You need to give an integer value for your new minimum amount!")
if value is None:
return
collection.update_one(query, {"$set": {"starboard_min_upvotes": value}}, upsert=True)
elif mode == "starboard emoji":
if value is not None:
if ":" in value:
try:
value = value.split(":")[2][:-1] #get ID of emoji
except IndexError:
await itx.response.send_message("You have to give the emoji or numerical ID of the emoji you want to use!",ephemeral=True)
return
try:
value = int(value)
except ValueError:
pass
emoji = client.get_emoji(value)
if emoji is None:
warning += "This emoji might not exist! Are you sure you filled in a correct ID? Ignore this warning if you used a unicode emoji.\n"
else:
value = emoji
collection.update_one(query, {"$set": {"starboard_emoji": str(value)}}, upsert=True)
elif mode == "upvote emoji":
if value is not None:
if ":" in value:
try:
value = value.split(":")[2][:-1] #get ID of emoji
except IndexError:
await itx.response.send_message("You have to give the emoji or numerical ID of the emoji you want to use!",ephemeral=True)
return
try:
value = int(value)
except ValueError:
pass
emoji = client.get_emoji(value)
if emoji is None:
warning += "This emoji might not exist! Are you sure you filled in a correct ID or emoji? Ignore this warning if you used a unicode emoji.\n"
else:
value = emoji
collection.update_one(query, {"$set": {"starboard_upvote_emoji": str(value)}}, upsert=True)
elif mode == "downvote emoji":
if value is not None:
if ":" in value:
try:
value = value.split(":")[2][:-1] # get ID of emoji ["<a","name","123456789>"]
except IndexError:
await itx.response.send_message("You have to give the emoji or numerical ID of the emoji you want to use!", ephemeral=True)
return
try:
value = int(value)
except ValueError:
pass
emoji = client.get_emoji(value)
if emoji is None:
warning += "This emoji might not exist! Are you sure you filled in a correct ID? Ignore this warning if you used a unicode emoji.\n"
else:
value = emoji
collection.update_one(query, {"$set": {"starboard_downvote_emoji": str(value)}}, upsert=True)
elif mode == "allow downvotes":
if value is not None:
value = await format(value, bool)
collection.update_one(query, {"$set": {"starboard_allow_downvotes": value}}, upsert=True)
elif mode == "image only":
if value is not None:
value = await format(value, bool)
collection.update_one(query, {"$set": {"starboard_image_only": value}}, upsert=True)
elif mode == "message caption":
if value is not None:
collection.update_one(query, {"$set": {"starboard_caption": value}}, upsert=True)
elif mode == "embed color":
if value is not None:
_colors = value.split(",")
if len(_colors) != 3:
await itx.response.send_message("You must use the following layout: \"rrr, ggg, bbb\" like \"127, 0, 255\"! (where rrr is red a number value from 0-255)")
colors = []
for _color in _colors:
color = await format(_color.strip(), int, error_msg="You must use the following layout: \"rrr, ggg, bbb\" like \"127, 0, 255\"! (where rrr is red a number value from 0-255)\n" +
f'"{_color}" is not an integer!')
if color is None:
return None
colors.append(color)
collection.update_one(query, {"$set": {"starboard_embed_color": colors}}, upsert=True)
value = colors
await itx.response.send_message(warning+f"Edited value of '{mode}' to '{value}' successfully.",ephemeral=True)
async def on_message(message):
# kill switch, see cmd_addons for other on_message events.
if message.author.id == 262913789375021056:
if message.content == ":kill all the starboards.":
sys.exit(0)
try:
client.run(open('token.txt',"r").read())
except SystemExit:
print("Exited the program forcefully using the kill switch")