11import discord
22from discord .ext import commands
3- from discord import app_commands
3+
44from settings import MEDIA_CHANNEL_ID , FB_IDEAS_CHANNEL_ID
5+ from asyncio import sleep
56from re import sub
67
8+
79class AutoThreads (commands .Cog ):
810 def __init__ (self , bot ):
911 self .bot = bot
12+ self .waiting_for_forward_comment = False
1013
1114 @commands .Cog .listener ("on_message" )
1215 async def auto_threads (self , msg ):
@@ -15,17 +18,35 @@ async def auto_threads(self, msg):
1518 if msg .author .bot :
1619 return
1720 if msg .channel .id == FB_IDEAS_CHANNEL_ID and "https://discord.com/channels/1138536747932864532" in msg .content :
18- print ("hi" )
1921 await create_auto_thread (msg )
20- elif msg .channel .id == MEDIA_CHANNEL_ID and (msg .attachments != [] or "https://" in msg .content or "http://" in msg .content ):
21- print (msg .channel .id == MEDIA_CHANNEL_ID )
22- await create_auto_thread (msg )
22+ if msg .channel .id == MEDIA_CHANNEL_ID :
23+ if (msg .attachments != [] or "https://" in msg .content or "http://" in msg .content ):
24+ await create_auto_thread (msg )
25+ elif msg .flags .forwarded :
26+ self .waiting_for_forward_comment = True
27+ await self .handle_forward (msg )
28+ elif not self .waiting_for_forward_comment :
29+ await msg .delete ()
30+ await msg .channel .send (f"{ msg .author .mention } Обсуждайте в ветках!" , delete_after = 3 )
2331 else :
2432 await msg .delete ()
2533 await msg .channel .send (f"{ msg .author .mention } Обсуждайте в ветках!" , delete_after = 3 )
2634
27- async def create_auto_thread (msg ):
28- content = sub (r"https?:\/\/\S+" , "" , msg .content )
35+ async def handle_forward (self , msg ):
36+ end_msg = msg
37+ msg_content = msg .message_snapshots [0 ].content
38+ await sleep (0.7 )
39+ self .waiting_for_forward_comment = False
40+ if msg .channel .last_message_id != msg .id :
41+ end_msg = await msg .channel .fetch_message (msg .channel .last_message_id )
42+ if not msg_content :
43+ msg_content = end_msg .content
44+ await create_auto_thread (end_msg , msg_content )
45+
46+
47+ async def create_auto_thread (msg , msg_content = None ):
48+ msg_content = msg_content if msg_content else msg .content
49+ content = sub (r"https?:\/\/\S+" , "" , msg_content )
2950 thread_name = content [:97 ]
3051 if len (thread_name ) == 97 :
3152 thread_name += "..."
@@ -34,4 +55,3 @@ async def create_auto_thread(msg):
3455 except discord .errors .HTTPException :
3556 channel_name = sub (r"[^\w-]*" , "" , msg .channel .name )
3657 await msg .create_thread (name = f"Обсуждение { channel_name } { msg .author .display_name } " )
37-
0 commit comments