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_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 :
@@ -1136,10 +1135,16 @@ def get_notifications(self) -> str:
1136
1135
return " " .join (set (mentions ))
1137
1136
1138
1137
async def set_title (self , title : str ) -> None :
1138
+ topic = f"Title: { title } \n "
1139
+
1139
1140
user_id = match_user_id (self .channel .topic )
1140
- ids = "," .join (i .id for i in self ._other_recipients )
1141
+ topic += f"User ID: { user_id } "
1142
+
1143
+ if self ._other_recipients :
1144
+ ids = "," .join (str (i .id ) for i in self ._other_recipients )
1145
+ topic += f"\n Other Recipients: { ids } "
1141
1146
1142
- await self .channel .edit (topic = f"Title: { title } \n User ID: { user_id } \n Other Recipients: { ids } " )
1147
+ await self .channel .edit (topic = topic )
1143
1148
1144
1149
async def _update_users_genesis (self ):
1145
1150
genesis_message = await self .get_genesis_message ()
@@ -1162,24 +1167,37 @@ async def _update_users_genesis(self):
1162
1167
await genesis_message .edit (embed = embed )
1163
1168
1164
1169
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 )
1167
- self ._other_recipients += users
1170
+ topic = ""
1171
+ title , user_id , _ = parse_channel_topic (self .channel .topic )
1172
+ if title is not None :
1173
+ topic += f"Title: { title } \n "
1168
1174
1175
+ topic += f"User ID: { user_id } "
1176
+
1177
+ self ._other_recipients += users
1169
1178
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 } " )
1171
1179
1180
+ topic += f"\n Other Recipients: { ids } "
1181
+
1182
+ await self .channel .edit (topic = topic )
1172
1183
await self ._update_users_genesis ()
1173
1184
1174
1185
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 )
1186
+ topic = ""
1187
+ title , user_id , _ = parse_channel_topic (self .channel .topic )
1188
+ if title is not None :
1189
+ topic += f"Title: { title } \n "
1190
+
1191
+ topic += f"User ID: { user_id } "
1192
+
1177
1193
for u in users :
1178
1194
self ._other_recipients .remove (u )
1179
1195
1180
- 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 } " )
1196
+ if self ._other_recipients :
1197
+ ids = "," .join (str (i .id ) for i in self ._other_recipients )
1198
+ topic += f"\n Other Recipients: { ids } "
1182
1199
1200
+ await self .channel .edit (topic = topic )
1183
1201
await self ._update_users_genesis ()
1184
1202
1185
1203
@@ -1240,16 +1258,24 @@ async def find(
1240
1258
await thread .close (closer = self .bot .user , silent = True , delete_channel = False )
1241
1259
thread = None
1242
1260
else :
1261
+
1262
+ def check (topic ):
1263
+ _ , user_id , other_ids = parse_channel_topic (topic )
1264
+ return recipient_id == user_id or recipient_id in other_ids
1265
+
1243
1266
channel = discord .utils .find (
1244
- lambda x : str ( recipient_id ) in x .topic if x .topic else False ,
1267
+ lambda x : ( check ( x .topic )) if x .topic else False ,
1245
1268
self .bot .modmail_guild .text_channels ,
1246
1269
)
1247
1270
1248
1271
if channel :
1249
1272
thread = await Thread .from_channel (self , channel )
1250
1273
if thread .recipient :
1251
- # only save if data is valid
1252
- self .cache [recipient_id ] = thread
1274
+ # only save if data is valid.
1275
+ # also the recipient_id here could belong to other recipient,
1276
+ # it would be wrong if we set it as the dict key,
1277
+ # so we use the thread id instead
1278
+ self .cache [thread .id ] = thread
1253
1279
thread .ready = True
1254
1280
1255
1281
if thread and recipient_id not in [x .id for x in thread .recipients ]:
@@ -1265,10 +1291,11 @@ async def _find_from_channel(self, channel):
1265
1291
searching channel history for genesis embed and
1266
1292
extracts user_id from that.
1267
1293
"""
1268
- user_id = - 1
1269
1294
1270
- if channel .topic :
1271
- user_id = match_user_id (channel .topic )
1295
+ if not channel .topic :
1296
+ return None
1297
+
1298
+ _ , user_id , other_ids = parse_channel_topic (channel .topic )
1272
1299
1273
1300
if user_id == - 1 :
1274
1301
return None
@@ -1282,7 +1309,7 @@ async def _find_from_channel(self, channel):
1282
1309
recipient = None
1283
1310
1284
1311
other_recipients = []
1285
- for uid in match_other_recipients ( channel . topic ) :
1312
+ for uid in other_ids :
1286
1313
try :
1287
1314
other_recipient = self .bot .get_user (uid ) or await self .bot .fetch_user (uid )
1288
1315
except discord .NotFound :
0 commit comments