Skip to content

Commit 719efcc

Browse files
committed
Merge branch 'feat/revive' of https://github.com/team-nameless/nameless-discord-bot into feat/revive-music
2 parents 4eb89b8 + a729a30 commit 719efcc

File tree

4 files changed

+91
-76
lines changed

4 files changed

+91
-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/custom/crud.py

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ class NamelessCRUD:
1414
async def init() -> None:
1515
await raw_db.connect()
1616

17+
@staticmethod
18+
async def dispose() -> None:
19+
await raw_db.disconnect()
20+
1721
@staticmethod
1822
async def get_or_create_guild_entry(
1923
guild: discord.Guild, *, include_cross_chat: bool = False

nameless/nameless.py

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

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

4753
async def on_ready(self):
4854
logging.info("Setting presence.")
@@ -65,6 +71,7 @@ def start_bot(self, *, is_debug: bool = False) -> None:
6571
async def close(self) -> None:
6672
logging.warning("Shutting down...")
6773
nameless_config["nameless"]["is_shutting_down"] = True
74+
await NamelessCRUD.dispose()
6875
await super().close()
6976
exit(0)
7077

nameless/prisma/schema.prisma

+15-8
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,22 @@ model Guild {
1818
PlayerSetting PlayerSettings?
1919
}
2020

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

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

3239
model CrossChatMessage {

0 commit comments

Comments
 (0)