1
1
import discord
2
- from core .models import DiscordMessage
2
+ from core .models import DiscordMessage , InboxItem
3
3
from discord .ext import commands , tasks
4
4
from django .conf import settings
5
5
from django .utils import timezone
10
10
11
11
bot = commands .Bot (command_prefix = "!" , intents = intents )
12
12
13
+ # Inbox emoji used for adding messages to user's inbox
14
+ INBOX_EMOJI = "📥"
15
+
13
16
14
17
@bot .event
15
18
async def on_ready ():
16
19
print (f"Bot is ready. Logged in as { bot .user } " )
17
20
poll_database .start () # Start polling the database
18
21
19
22
23
+ @bot .event
24
+ async def on_raw_reaction_add (payload ):
25
+ """Handle adding messages to inbox when users react with the inbox emoji"""
26
+ if payload .emoji .name == INBOX_EMOJI :
27
+ # Get the channel and message details
28
+ channel = bot .get_channel (payload .channel_id )
29
+ message = await channel .fetch_message (payload .message_id )
30
+
31
+ # Create a new inbox item using async
32
+ await InboxItem .objects .acreate (
33
+ message_id = str (message .id ),
34
+ channel_id = str (payload .channel_id ),
35
+ channel_name = f"#{ channel .name } " ,
36
+ server_id = str (payload .guild_id ),
37
+ user_id = str (payload .user_id ),
38
+ author = str (message .author .name ),
39
+ content = message .content ,
40
+ )
41
+
42
+
43
+ @bot .event
44
+ async def on_raw_reaction_remove (payload ):
45
+ """Handle removing messages from inbox when users remove the inbox emoji"""
46
+ if payload .emoji .name == INBOX_EMOJI :
47
+ # Remove the inbox item
48
+ items = InboxItem .objects .filter (
49
+ message_id = str (payload .message_id ),
50
+ user_id = str (payload .user_id ),
51
+ )
52
+ await items .adelete ()
53
+
54
+
55
+ @bot .command ()
56
+ async def inbox (ctx ):
57
+ """
58
+ Displays the content of the inbox for the user that calls the command.
59
+
60
+ Each message is saved with user_id (which is a discord id), and here we can
61
+ filter out all those messages depending on who called the command.
62
+
63
+ It retuns all tracked messages, starting from the one most recently saved
64
+ (a message that was most recently tagged with inbox emoji, not the message
65
+ that was most recently sent).
66
+ """
67
+ user_id = str (ctx .message .author .id )
68
+ inbox_items = InboxItem .objects .filter (user_id = user_id ).order_by ("-created_at" )
69
+
70
+ # Use async query
71
+ if not await inbox_items .aexists ():
72
+ await ctx .send ("Your inbox is empty." )
73
+ return
74
+
75
+ msg = "Currently tracking the following messages:\n "
76
+
77
+ async for item in inbox_items :
78
+ msg += "* " + item .summary () + "\n "
79
+
80
+ # Create an embed to display the inbox
81
+ embed = discord .Embed ()
82
+ embed .description = msg
83
+ await ctx .send (embed = embed )
84
+
85
+
20
86
@bot .command ()
21
87
async def ping (ctx ):
22
88
await ctx .send ("Pong!" )
@@ -38,19 +104,22 @@ async def wiki(ctx):
38
104
suppress_embeds = True ,
39
105
)
40
106
107
+
41
108
@bot .command ()
42
109
async def close (ctx ):
43
110
channel = ctx .channel
44
111
author = ctx .message .author
45
112
46
113
# Check if it's a public or private post (thread)
47
- if channel .type in (discord .ChannelType .public_thread , discord .ChannelType .private_thread ):
114
+ if channel .type in (
115
+ discord .ChannelType .public_thread ,
116
+ discord .ChannelType .private_thread ,
117
+ ):
48
118
parent = channel .parent
49
119
50
120
# Check if the post (thread) was sent in a forum,
51
121
# so we can add a tag
52
122
if parent .type == discord .ChannelType .forum :
53
-
54
123
# Get tag from forum
55
124
tag = None
56
125
for _tag in parent .available_tags :
@@ -65,18 +134,21 @@ async def close(ctx):
65
134
await ctx .message .delete ()
66
135
67
136
# Send notification to the thread
68
- await channel .send (f"# This was marked as done by { author .mention } " , suppress_embeds = True )
137
+ await channel .send (
138
+ f"# This was marked as done by { author .mention } " , suppress_embeds = True
139
+ )
69
140
70
141
# We need to archive after adding tags in case it was a forum.
71
142
await channel .edit (archived = True )
72
143
else :
73
144
# Remove command message
74
145
await ctx .message .delete ()
75
146
76
- await channel .send ("The !close command is intended to be used inside a thread/post" ,
77
- suppress_embeds = True ,
78
- delete_after = 5 )
79
-
147
+ await channel .send (
148
+ "The !close command is intended to be used inside a thread/post" ,
149
+ suppress_embeds = True ,
150
+ delete_after = 5 ,
151
+ )
80
152
81
153
82
154
@bot .command ()
0 commit comments