17
17
from core .utils import (
18
18
is_image_url ,
19
19
days ,
20
+ parse_channel_topic ,
20
21
match_title ,
21
22
match_user_id ,
22
- match_other_recipients ,
23
23
truncate ,
24
24
get_top_hoisted_role ,
25
25
create_thread_channel ,
@@ -119,17 +119,16 @@ def cancelled(self, flag: bool):
119
119
120
120
@classmethod
121
121
async def from_channel (cls , manager : "ThreadManager" , channel : discord .TextChannel ) -> "Thread" :
122
- recipient_id = match_user_id (
123
- channel .topic
124
- ) # there is a chance it grabs from another recipient's main thread
122
+ # there is a chance it grabs from another recipient's main thread
123
+ _ , recipient_id , other_ids = parse_channel_topic (channel .topic )
125
124
126
125
if recipient_id in manager .cache :
127
126
thread = manager .cache [recipient_id ]
128
127
else :
129
128
recipient = manager .bot .get_user (recipient_id ) or await manager .bot .fetch_user (recipient_id )
130
129
131
130
other_recipients = []
132
- for uid in match_other_recipients ( channel . topic ) :
131
+ for uid in other_ids :
133
132
try :
134
133
other_recipient = manager .bot .get_user (uid ) or await manager .bot .fetch_user (uid )
135
134
except discord .NotFound :
@@ -1162,23 +1161,31 @@ async def _update_users_genesis(self):
1162
1161
await genesis_message .edit (embed = embed )
1163
1162
1164
1163
async def add_users (self , users : typing .List [typing .Union [discord .Member , discord .User ]]) -> None :
1165
- title = match_title (self .channel .topic )
1166
- user_id = match_user_id (self .channel .topic )
1164
+ title , user_id , _ = parse_channel_topic (self .channel .topic )
1165
+ if title is not None :
1166
+ title = f"Title: { title } \n "
1167
+ else :
1168
+ title = ""
1169
+
1167
1170
self ._other_recipients += users
1168
1171
1169
1172
ids = "," .join (str (i .id ) for i in self ._other_recipients )
1170
- await self .channel .edit (topic = f"Title: { title } \n User ID: { user_id } \n Other Recipients: { ids } " )
1173
+ await self .channel .edit (topic = f"{ title } User ID: { user_id } \n Other Recipients: { ids } " )
1171
1174
1172
1175
await self ._update_users_genesis ()
1173
1176
1174
1177
async def remove_users (self , users : typing .List [typing .Union [discord .Member , discord .User ]]) -> None :
1175
- title = match_title (self .channel .topic )
1176
- user_id = match_user_id (self .channel .topic )
1178
+ title , user_id , _ = parse_channel_topic (self .channel .topic )
1179
+ if title is not None :
1180
+ title = f"Title: { title } \n "
1181
+ else :
1182
+ title = ""
1183
+
1177
1184
for u in users :
1178
1185
self ._other_recipients .remove (u )
1179
1186
1180
1187
ids = "," .join (str (i .id ) for i in self ._other_recipients )
1181
- await self .channel .edit (topic = f"Title: { title } \n User ID: { user_id } \n Other Recipients: { ids } " )
1188
+ await self .channel .edit (topic = f"{ title } User ID: { user_id } \n Other Recipients: { ids } " )
1182
1189
1183
1190
await self ._update_users_genesis ()
1184
1191
@@ -1240,16 +1247,24 @@ async def find(
1240
1247
await thread .close (closer = self .bot .user , silent = True , delete_channel = False )
1241
1248
thread = None
1242
1249
else :
1250
+
1251
+ def check (topic ):
1252
+ _ , user_id , other_ids = parse_channel_topic (topic )
1253
+ return recipient_id == user_id or recipient_id in other_ids
1254
+
1243
1255
channel = discord .utils .find (
1244
- lambda x : str ( recipient_id ) in x .topic if x .topic else False ,
1256
+ lambda x : ( check ( x .topic )) if x .topic else False ,
1245
1257
self .bot .modmail_guild .text_channels ,
1246
1258
)
1247
1259
1248
1260
if channel :
1249
1261
thread = await Thread .from_channel (self , channel )
1250
1262
if thread .recipient :
1251
- # only save if data is valid
1252
- self .cache [recipient_id ] = thread
1263
+ # only save if data is valid.
1264
+ # also the recipient_id here could belong to other recipient,
1265
+ # it would be wrong if we set it as the dict key,
1266
+ # so we use the thread id instead
1267
+ self .cache [thread .id ] = thread
1253
1268
thread .ready = True
1254
1269
1255
1270
if thread and recipient_id not in [x .id for x in thread .recipients ]:
@@ -1265,10 +1280,11 @@ async def _find_from_channel(self, channel):
1265
1280
searching channel history for genesis embed and
1266
1281
extracts user_id from that.
1267
1282
"""
1268
- user_id = - 1
1269
1283
1270
- if channel .topic :
1271
- user_id = match_user_id (channel .topic )
1284
+ if not channel .topic :
1285
+ return None
1286
+
1287
+ _ , user_id , other_ids = parse_channel_topic (channel .topic )
1272
1288
1273
1289
if user_id == - 1 :
1274
1290
return None
@@ -1282,7 +1298,7 @@ async def _find_from_channel(self, channel):
1282
1298
recipient = None
1283
1299
1284
1300
other_recipients = []
1285
- for uid in match_other_recipients ( channel . topic ) :
1301
+ for uid in other_ids :
1286
1302
try :
1287
1303
other_recipient = self .bot .get_user (uid ) or await self .bot .fetch_user (uid )
1288
1304
except discord .NotFound :
0 commit comments