1
1
import discord
2
2
from discord .ext import commands
3
- from discord import app_commands
3
+
4
4
from settings import MEDIA_CHANNEL_ID , FB_IDEAS_CHANNEL_ID
5
+ from asyncio import sleep
5
6
from re import sub
6
7
8
+
7
9
class AutoThreads (commands .Cog ):
8
10
def __init__ (self , bot ):
9
11
self .bot = bot
12
+ self .waiting_for_forward_comment = False
10
13
11
14
@commands .Cog .listener ("on_message" )
12
15
async def auto_threads (self , msg ):
@@ -15,17 +18,35 @@ async def auto_threads(self, msg):
15
18
if msg .author .bot :
16
19
return
17
20
if msg .channel .id == FB_IDEAS_CHANNEL_ID and "https://discord.com/channels/1138536747932864532" in msg .content :
18
- print ("hi" )
19
21
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 )
23
31
else :
24
32
await msg .delete ()
25
33
await msg .channel .send (f"{ msg .author .mention } Обсуждайте в ветках!" , delete_after = 3 )
26
34
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 )
29
50
thread_name = content [:97 ]
30
51
if len (thread_name ) == 97 :
31
52
thread_name += "..."
@@ -34,4 +55,3 @@ async def create_auto_thread(msg):
34
55
except discord .errors .HTTPException :
35
56
channel_name = sub (r"[^\w-]*" , "" , msg .channel .name )
36
57
await msg .create_thread (name = f"Обсуждение { channel_name } { msg .author .display_name } " )
37
-
0 commit comments