4
4
import discord .ui
5
5
from discord import app_commands
6
6
from discord .ext import commands
7
- from prisma .models import CrossChatMessage , CrossChatSubscription
7
+ from prisma .models import CrossChatMessage , CrossChatRoom , CrossChatSubscription
8
8
9
9
from nameless import Nameless
10
10
from nameless .custom .crud import NamelessCRUD
11
- from nameless .custom .ui import NamelessYesNoPrompt
12
11
13
12
__all__ = ["CrossOverCommand" ]
14
13
@@ -30,16 +29,19 @@ async def on_message(self, message: discord.Message):
30
29
return
31
30
32
31
subs = await CrossChatSubscription .prisma ().find_many (
33
- where = {"GuildId" : message .guild .id , "ChannelId" : message .channel .id }
32
+ where = {"GuildId" : message .guild .id , "ChannelId" : message .channel .id },
33
+ include = {"Room" : True },
34
34
)
35
35
36
36
for sub in subs :
37
- guild = self .bot .get_guild (sub .TargetGuildId )
37
+ assert sub .Room is not None
38
+
39
+ guild = self .bot .get_guild (sub .Room .GuildId )
38
40
39
41
if guild is None :
40
42
return
41
43
42
- channel = guild .get_channel (sub .TargetChannelId )
44
+ channel = guild .get_channel (sub .Room . ChannelId )
43
45
44
46
if channel is None :
45
47
return
@@ -94,16 +96,18 @@ async def on_message_edit(self, _: discord.Message, message: discord.Message):
94
96
"ChannelId" : message .channel .id ,
95
97
"Messages" : {"some" : {"OriginMessageId" : message .id }},
96
98
},
97
- include = {"Messages" : True },
99
+ include = {"Messages" : True , "Room" : True },
98
100
)
99
101
100
102
for sub in subs :
101
- guild = self .bot .get_guild (sub .TargetGuildId )
103
+ assert sub .Room is not None
104
+
105
+ guild = self .bot .get_guild (sub .Room .GuildId )
102
106
103
107
if guild is None :
104
108
return
105
109
106
- channel = guild .get_channel (sub .TargetChannelId )
110
+ channel = guild .get_channel (sub .Room . ChannelId )
107
111
108
112
if channel is None :
109
113
return
@@ -143,16 +147,18 @@ async def on_message_delete(self, message: discord.Message):
143
147
"ChannelId" : message .channel .id ,
144
148
"Messages" : {"some" : {"OriginMessageId" : message .id }},
145
149
},
146
- include = {"Messages" : True },
150
+ include = {"Messages" : True , "Room" : True },
147
151
)
148
152
149
153
for sub in subs :
150
- guild = self .bot .get_guild (sub .TargetGuildId )
154
+ assert sub .Room is not None
155
+
156
+ guild = self .bot .get_guild (sub .Room .GuildId )
151
157
152
158
if guild is None :
153
159
return
154
160
155
- channel = guild .get_channel (sub .TargetChannelId )
161
+ channel = guild .get_channel (sub .Room . ChannelId )
156
162
157
163
if channel is None :
158
164
return
@@ -173,86 +179,77 @@ async def on_message_delete(self, message: discord.Message):
173
179
174
180
@app_commands .command ()
175
181
@app_commands .guild_only ()
176
- @app_commands .describe (
177
- target_guild = "Target guild ID to establish." ,
178
- target_channel = "Target channel to establish." ,
179
- )
180
- async def create_link (
181
- self ,
182
- interaction : discord .Interaction [Nameless ],
183
- target_guild : str ,
184
- target_channel : str ,
185
- ):
186
- """Create link to another guild."""
182
+ @app_commands .checks .has_permissions (manage_guild = True )
183
+ async def publish (self , interaction : discord .Interaction [Nameless ]):
184
+ """Establish this channel to the public."""
187
185
await interaction .response .defer ()
188
186
189
- guild = self .bot .get_guild (int (target_guild ))
190
-
191
- if guild is None :
192
- await interaction .followup .send ("That guild does not exist." )
193
- return
187
+ assert interaction .guild is not None
188
+ assert interaction .channel is not None
194
189
195
- channel = guild .get_channel (int (target_channel ))
190
+ room_data : CrossChatRoom | None = await CrossChatRoom .prisma ().find_first (
191
+ where = {"ChannelId" : interaction .channel .id , "GuildId" : interaction .guild .id }
192
+ )
196
193
197
- if channel is None :
198
- await interaction .followup .send ("That channel does not exist." )
199
- return
194
+ if room_data is None :
195
+ room_data = await CrossChatRoom .prisma ().create (
196
+ data = {
197
+ "GuildId" : interaction .guild .id ,
198
+ "ChannelId" : interaction .channel .id ,
199
+ }
200
+ )
200
201
201
- if isinstance ( channel , ( discord . ForumChannel , discord . CategoryChannel )):
202
- await interaction . followup . send ( "Invalid channel to link to." )
203
- return
202
+ await interaction . followup . send (
203
+ f"Your cross-chat room code is: ` { room_data . RoomId } `"
204
+ )
204
205
205
- assert interaction .guild is not None
206
- assert interaction .channel is not None
206
+ @app_commands .command ()
207
+ @app_commands .guild_only ()
208
+ @app_commands .describe (room_code = "Room code to subscribe." )
209
+ @app_commands .checks .has_permissions (manage_guild = True )
210
+ async def connect (self , interaction : discord .Interaction [Nameless ], room_code : str ):
211
+ """Create link to another guild."""
212
+ await interaction .response .defer ()
207
213
208
- temp_data : (
209
- CrossChatSubscription | None
210
- ) = await CrossChatSubscription .prisma ().find_first (
211
- where = {
212
- "ChannelId" : interaction .channel .id ,
213
- "TargetGuildId" : guild .id ,
214
- "TargetChannelId" : channel .id ,
215
- }
214
+ room_data : CrossChatRoom | None = await CrossChatRoom .prisma ().find_first (
215
+ where = {"RoomId" : room_code }
216
216
)
217
217
218
- if temp_data is not None :
219
- await interaction .followup .send ("You had a link with this place before. " )
218
+ if room_data is None :
219
+ await interaction .followup .send ("Room code does not exist! " )
220
220
return
221
221
222
- await interaction .followup .send ("Sending out request, please wait." )
222
+ assert interaction .guild is not None
223
+ assert interaction .channel is not None
223
224
224
- prompt = NamelessYesNoPrompt ()
225
+ this_guild = interaction .guild
226
+ that_guild = await self .bot .fetch_guild (room_data .GuildId )
225
227
226
- await channel . send ( "You have an incoming link!" , view = prompt )
227
- await prompt . wait ()
228
+ assert this_guild is not None
229
+ assert that_guild is not None
228
230
229
- if not prompt .is_a_yes :
230
- await interaction .followup .send ("Response declined." )
231
+ if (
232
+ room_data .GuildId == this_guild .id
233
+ and room_data .ChannelId == interaction .channel .id
234
+ ):
235
+ await interaction .followup .send ("Don't connect to yourself!" )
231
236
return
232
237
233
- await NamelessCRUD .get_or_create_guild_entry (interaction . guild )
234
- await NamelessCRUD .get_or_create_guild_entry (guild )
238
+ await NamelessCRUD .get_or_create_guild_entry (this_guild )
239
+ await NamelessCRUD .get_or_create_guild_entry (that_guild )
235
240
236
241
await CrossChatSubscription .prisma ().create (
237
242
data = {
238
- "Guild" : {"connect" : {"Id" : interaction . guild .id }},
243
+ "Guild" : {"connect" : {"Id" : this_guild .id }},
239
244
"ChannelId" : interaction .channel .id ,
240
- "TargetGuildId" : guild .id ,
241
- "TargetChannelId" : channel .id ,
245
+ "Room" : {"connect" : {"RoomId" : room_code }},
242
246
}
243
247
)
244
248
245
- await CrossChatSubscription .prisma ().create (
246
- data = {
247
- "Guild" : {"connect" : {"Id" : guild .id }},
248
- "ChannelId" : channel .id ,
249
- "TargetGuildId" : interaction .guild .id ,
250
- "TargetChannelId" : interaction .channel .id ,
251
- }
249
+ await interaction .followup .send (
250
+ "Linking success! Please note that the other guild need to do the same."
252
251
)
253
252
254
- await interaction .followup .send ("Linking success!" )
255
-
256
253
257
254
async def setup (bot : Nameless ):
258
255
await bot .add_cog (CrossOverCommand (bot ))
0 commit comments