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+ 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+
2078@bot .command ()
2179async def ping (ctx ):
2280 await ctx .send ("Pong!" )
@@ -38,19 +96,22 @@ async def wiki(ctx):
3896 suppress_embeds = True ,
3997 )
4098
99+
41100@bot .command ()
42101async def close (ctx ):
43102 channel = ctx .channel
44103 author = ctx .message .author
45104
46105 # 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+ ):
48110 parent = channel .parent
49111
50112 # Check if the post (thread) was sent in a forum,
51113 # so we can add a tag
52114 if parent .type == discord .ChannelType .forum :
53-
54115 # Get tag from forum
55116 tag = None
56117 for _tag in parent .available_tags :
@@ -65,18 +126,21 @@ async def close(ctx):
65126 await ctx .message .delete ()
66127
67128 # 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+ )
69132
70133 # We need to archive after adding tags in case it was a forum.
71134 await channel .edit (archived = True )
72135 else :
73136 # Remove command message
74137 await ctx .message .delete ()
75138
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+ )
80144
81145
82146@bot .command ()
0 commit comments