11import discord
2- from core .models import DiscordMessage
2+ from core .models import DiscordMessage , InboxItem
33from discord .ext import commands , tasks
44from django .conf import settings
55from django .utils import timezone
1010
1111bot = commands .Bot (command_prefix = "!" , intents = intents )
1212
13+ # Inbox emoji used for adding messages to user's inbox
14+ INBOX_EMOJI = "📥"
15+
1316
1417@bot .event
1518async def on_ready ():
1619 print (f"Bot is ready. Logged in as { bot .user } " )
1720 poll_database .start () # Start polling the database
1821
1922
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+
2086@bot .command ()
2187async def ping (ctx ):
2288 await ctx .send ("Pong!" )
@@ -38,19 +104,22 @@ async def wiki(ctx):
38104 suppress_embeds = True ,
39105 )
40106
107+
41108@bot .command ()
42109async def close (ctx ):
43110 channel = ctx .channel
44111 author = ctx .message .author
45112
46113 # 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+ ):
48118 parent = channel .parent
49119
50120 # Check if the post (thread) was sent in a forum,
51121 # so we can add a tag
52122 if parent .type == discord .ChannelType .forum :
53-
54123 # Get tag from forum
55124 tag = None
56125 for _tag in parent .available_tags :
@@ -65,18 +134,21 @@ async def close(ctx):
65134 await ctx .message .delete ()
66135
67136 # 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+ )
69140
70141 # We need to archive after adding tags in case it was a forum.
71142 await channel .edit (archived = True )
72143 else :
73144 # Remove command message
74145 await ctx .message .delete ()
75146
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+ )
80152
81153
82154@bot .command ()
0 commit comments