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
+ item = InboxItem (
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
+ await item .asave ()
42
+
43
+
44
+ @bot .event
45
+ async def on_raw_reaction_remove (payload ):
46
+ """Handle removing messages from inbox when users remove the inbox emoji"""
47
+ if payload .emoji .name == INBOX_EMOJI :
48
+ # Remove the inbox item
49
+ items = InboxItem .objects .filter (
50
+ message_id = str (payload .message_id ),
51
+ user_id = str (payload .user_id ),
52
+ )
53
+ await items .adelete ()
54
+
55
+
56
+ @bot .command ()
57
+ async def inbox (ctx ):
58
+ """Display a user's inbox items"""
59
+ user_id = str (ctx .message .author .id )
60
+ inbox_items = InboxItem .objects .filter (user_id = user_id ).order_by ("-created_at" )
61
+
62
+ msg = "Currently tracking the following messages:\n "
63
+
64
+ # Use async query
65
+ if not await inbox_items .aexists ():
66
+ await ctx .send ("Your inbox is empty." )
67
+ return
68
+
69
+ async for item in inbox_items :
70
+ msg += "* " + item .summary () + "\n "
71
+
72
+ # Create an embed to display the inbox
73
+ embed = discord .Embed ()
74
+ embed .description = msg
75
+ await ctx .send (embed = embed )
76
+
77
+
20
78
@bot .command ()
21
79
async def ping (ctx ):
22
80
await ctx .send ("Pong!" )
@@ -38,19 +96,22 @@ async def wiki(ctx):
38
96
suppress_embeds = True ,
39
97
)
40
98
99
+
41
100
@bot .command ()
42
101
async def close (ctx ):
43
102
channel = ctx .channel
44
103
author = ctx .message .author
45
104
46
105
# Check if it's a public or private post (thread)
47
- if channel .type in (discord .ChannelType .public_thread , discord .ChannelType .private_thread ):
106
+ if channel .type in (
107
+ discord .ChannelType .public_thread ,
108
+ discord .ChannelType .private_thread ,
109
+ ):
48
110
parent = channel .parent
49
111
50
112
# Check if the post (thread) was sent in a forum,
51
113
# so we can add a tag
52
114
if parent .type == discord .ChannelType .forum :
53
-
54
115
# Get tag from forum
55
116
tag = None
56
117
for _tag in parent .available_tags :
@@ -65,18 +126,21 @@ async def close(ctx):
65
126
await ctx .message .delete ()
66
127
67
128
# Send notification to the thread
68
- await channel .send (f"# This was marked as done by { author .mention } " , suppress_embeds = True )
129
+ await channel .send (
130
+ f"# This was marked as done by { author .mention } " , suppress_embeds = True
131
+ )
69
132
70
133
# We need to archive after adding tags in case it was a forum.
71
134
await channel .edit (archived = True )
72
135
else :
73
136
# Remove command message
74
137
await ctx .message .delete ()
75
138
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
-
139
+ await channel .send (
140
+ "The !close command is intended to be used inside a thread/post" ,
141
+ suppress_embeds = True ,
142
+ delete_after = 5 ,
143
+ )
80
144
81
145
82
146
@bot .command ()
0 commit comments