Skip to content

Commit 40377e2

Browse files
committed
Changed to uni-directional room-based approach.
1 parent 1683ce8 commit 40377e2

File tree

3 files changed

+86
-76
lines changed

3 files changed

+86
-76
lines changed

nameless/command/crossover.py

+63-66
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
import discord.ui
55
from discord import app_commands
66
from discord.ext import commands
7-
from prisma.models import CrossChatMessage, CrossChatSubscription
7+
from prisma.models import CrossChatMessage, CrossChatRoom, CrossChatSubscription
88

99
from nameless import Nameless
1010
from nameless.custom.crud import NamelessCRUD
11-
from nameless.custom.ui import NamelessYesNoPrompt
1211

1312
__all__ = ["CrossOverCommand"]
1413

@@ -30,16 +29,19 @@ async def on_message(self, message: discord.Message):
3029
return
3130

3231
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},
3434
)
3535

3636
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)
3840

3941
if guild is None:
4042
return
4143

42-
channel = guild.get_channel(sub.TargetChannelId)
44+
channel = guild.get_channel(sub.Room.ChannelId)
4345

4446
if channel is None:
4547
return
@@ -94,16 +96,18 @@ async def on_message_edit(self, _: discord.Message, message: discord.Message):
9496
"ChannelId": message.channel.id,
9597
"Messages": {"some": {"OriginMessageId": message.id}},
9698
},
97-
include={"Messages": True},
99+
include={"Messages": True, "Room": True},
98100
)
99101

100102
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)
102106

103107
if guild is None:
104108
return
105109

106-
channel = guild.get_channel(sub.TargetChannelId)
110+
channel = guild.get_channel(sub.Room.ChannelId)
107111

108112
if channel is None:
109113
return
@@ -143,16 +147,18 @@ async def on_message_delete(self, message: discord.Message):
143147
"ChannelId": message.channel.id,
144148
"Messages": {"some": {"OriginMessageId": message.id}},
145149
},
146-
include={"Messages": True},
150+
include={"Messages": True, "Room": True},
147151
)
148152

149153
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)
151157

152158
if guild is None:
153159
return
154160

155-
channel = guild.get_channel(sub.TargetChannelId)
161+
channel = guild.get_channel(sub.Room.ChannelId)
156162

157163
if channel is None:
158164
return
@@ -173,86 +179,77 @@ async def on_message_delete(self, message: discord.Message):
173179

174180
@app_commands.command()
175181
@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."""
187185
await interaction.response.defer()
188186

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
194189

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+
)
196193

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+
)
200201

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+
)
204205

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()
207213

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}
216216
)
217217

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!")
220220
return
221221

222-
await interaction.followup.send("Sending out request, please wait.")
222+
assert interaction.guild is not None
223+
assert interaction.channel is not None
223224

224-
prompt = NamelessYesNoPrompt()
225+
this_guild = interaction.guild
226+
that_guild = await self.bot.fetch_guild(room_data.GuildId)
225227

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
228230

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!")
231236
return
232237

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)
235240

236241
await CrossChatSubscription.prisma().create(
237242
data={
238-
"Guild": {"connect": {"Id": interaction.guild.id}},
243+
"Guild": {"connect": {"Id": this_guild.id}},
239244
"ChannelId": interaction.channel.id,
240-
"TargetGuildId": guild.id,
241-
"TargetChannelId": channel.id,
245+
"Room": {"connect": {"RoomId": room_code}},
242246
}
243247
)
244248

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."
252251
)
253252

254-
await interaction.followup.send("Linking success!")
255-
256253

257254
async def setup(bot: Nameless):
258255
await bot.add_cog(CrossOverCommand(bot))

nameless/nameless.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,14 @@ async def setup_hook(self):
4040
await self._register_commands()
4141

4242
logging.info("Syncing commands.")
43-
await self.tree.sync()
44-
logging.warning("Please wait at least one hour before using global commands.")
43+
44+
if bool(int(os.getenv("DEBUG", 0))):
45+
await self.tree.sync(guild=discord.Object(708668574201544745))
46+
await self.tree.sync(guild=discord.Object(507428680813772812))
47+
logging.warning("Commands are available in debug mode.")
48+
else:
49+
await self.tree.sync()
50+
logging.warning("Commands should be available in one hour.")
4551

4652
async def on_ready(self):
4753
logging.info("Setting presence.")

nameless/prisma/schema.prisma

+15-8
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,22 @@ model Guild {
1717
CrossChat CrossChatSubscription[]
1818
}
1919

20-
model CrossChatSubscription {
21-
SubscriptionId String @id @default(cuid())
22-
Guild Guild? @relation(fields: [GuildId], references: [Id])
23-
GuildId BigInt?
24-
ChannelId BigInt
25-
TargetGuildId BigInt
26-
TargetChannelId BigInt
20+
model CrossChatRoom {
21+
RoomId String @id @default(cuid())
22+
GuildId BigInt
23+
ChannelId BigInt
24+
IsOpen Boolean @default(true)
25+
CrossChatSubscription CrossChatSubscription[]
26+
}
2727

28-
Messages CrossChatMessage[]
28+
model CrossChatSubscription {
29+
SubscriptionId String @id @default(cuid())
30+
Guild Guild? @relation(fields: [GuildId], references: [Id])
31+
GuildId BigInt?
32+
ChannelId BigInt
33+
Room CrossChatRoom @relation(fields: [RoomId], references: [RoomId])
34+
RoomId String
35+
Messages CrossChatMessage[]
2936
}
3037

3138
model CrossChatMessage {

0 commit comments

Comments
 (0)