Skip to content

Commit bf3c559

Browse files
committed
fix: fix errors
1 parent 4837802 commit bf3c559

File tree

5 files changed

+1112
-1029
lines changed

5 files changed

+1112
-1029
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "rosetta"
3-
version = "0.6.1"
3+
version = "0.9.0"
44
description = "A discord music bot, with other cool stuffs"
55
readme = "README.md"
66
requires-python = ">=3.12"

rosetta/__main__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import os
23
from pathlib import Path
34

45
import discord
@@ -16,7 +17,13 @@
1617
intents.guilds = True
1718
intents.voice_states = True
1819

19-
bot = commands.Bot(command_prefix="!", intents=intents)
20+
pod_name = os.environ.get("SHARD_ID", "rosetta-0")
21+
shard_id = int(pod_name.split("-")[-1])
22+
total_shards = int(os.environ.get("TOTAL_SHARDS", 1))
23+
24+
bot = commands.Bot(
25+
command_prefix="!", intents=intents, shard_id=shard_id, shard_count=total_shards
26+
)
2027

2128

2229
@bot.event

rosetta/commands/play.py

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -145,19 +145,17 @@ async def search(self, interaction: discord.Interaction, keyword: str):
145145
view=SearchSelectView(self, tracks, interaction),
146146
)
147147

148-
@app_commands.command(name="shuffle", description="Shuffle")
149-
@app_commands.describe(ephemeral="hide response")
150-
async def shuffle(self, interaction: discord.Interaction, ephemeral: bool = False):
148+
async def do_shuffle(self, interaction: discord.Interaction, ephemeral: bool = False):
149+
"""Helper method to shuffle the queue"""
151150
await self.ensure_subscription(interaction)
152151
subscription = self.subscriptions.get(interaction.guild_id)
153152
random.shuffle(subscription.queue)
154153
await interaction.response.send_message(
155154
embed=SuccessEmbed(self.bot.user, "Shuffle complete"), ephemeral=ephemeral
156155
)
157156

158-
@app_commands.command(name="skip", description="Skip to next song")
159-
@app_commands.describe(ephemeral="hide response")
160-
async def skip(self, interaction: discord.Interaction, ephemeral: bool = False):
157+
async def do_skip(self, interaction: discord.Interaction, ephemeral: bool = False):
158+
"""Helper method to skip to the next song"""
161159
await self.ensure_subscription(interaction)
162160
await interaction.response.defer(ephemeral=ephemeral)
163161
subscription = self.subscriptions.get(interaction.guild_id)
@@ -173,6 +171,16 @@ async def skip(self, interaction: discord.Interaction, ephemeral: bool = False):
173171
embed=SuccessEmbed(self.bot.user, "No song left")
174172
)
175173

174+
@app_commands.command(name="shuffle", description="Shuffle")
175+
@app_commands.describe(ephemeral="hide response")
176+
async def shuffle(self, interaction: discord.Interaction, ephemeral: bool = False):
177+
await self.do_shuffle(interaction, ephemeral)
178+
179+
@app_commands.command(name="skip", description="Skip to next song")
180+
@app_commands.describe(ephemeral="hide response")
181+
async def skip(self, interaction: discord.Interaction, ephemeral: bool = False):
182+
await self.do_skip(interaction, ephemeral)
183+
176184
@app_commands.command(name="leave", description="Leave current channel")
177185
async def leave(self, interaction: discord.Interaction):
178186
await self.ensure_subscription(interaction)
@@ -215,13 +223,16 @@ async def updateNowPlaying(self, guild_id, message_id):
215223
if not subscription:
216224
self.updateNowPlaying.cancel()
217225
if subscription and not subscription.checkLock:
218-
message = self.bot.get_message(message_id)
219-
if message:
220-
await message.edit(
221-
embed=NowPlayingEmbed(
222-
track=subscription.nowPlaying, queue=subscription.queue
226+
try:
227+
message = await subscription.messageChannel.fetch_message(message_id)
228+
if message:
229+
await message.edit(
230+
embed=NowPlayingEmbed(
231+
track=subscription.nowPlaying, queue=subscription.queue
232+
)
223233
)
224-
)
234+
except discord.NotFound:
235+
self.updateNowPlaying.cancel()
225236

226237
async def ensure_voice(self, interaction: discord.Interaction):
227238
voice_client = interaction.guild.voice_client if interaction.guild else None
@@ -343,9 +354,14 @@ def __init__(
343354
self.original_interaction = interaction
344355

345356
async def callback(self, interaction: discord.Interaction):
357+
# Find the "Top" toggle button by custom_id
358+
top_button = next(
359+
(item for item in self.view.children if getattr(item, 'custom_id', None) == "Top"),
360+
None
361+
)
346362
top = (
347363
True
348-
if self.view.get_item("Top").style == discord.ButtonStyle.success
364+
if top_button and top_button.style == discord.ButtonStyle.success
349365
else False
350366
)
351367
self.view.clear_items()
@@ -416,8 +432,8 @@ def __init__(self, player: Player):
416432
@discord.ui.button(
417433
label="Skip", custom_id="skip", style=discord.ButtonStyle.primary, emoji="⏩"
418434
)
419-
async def skip(self, button: discord.ui.Button, interaction: discord.Interaction):
420-
await self.player.skip(interaction, True)
435+
async def skip(self, interaction: discord.Interaction, button: discord.ui.Button):
436+
await self.player.do_skip(interaction, True)
421437

422438
@discord.ui.button(
423439
label="Shuffle",
@@ -426,9 +442,9 @@ async def skip(self, button: discord.ui.Button, interaction: discord.Interaction
426442
emoji="🔀",
427443
)
428444
async def shuffle(
429-
self, button: discord.ui.Button, interaction: discord.Interaction
445+
self, interaction: discord.Interaction, button: discord.ui.Button
430446
):
431-
await self.player.shuffle(interaction, True)
447+
await self.player.do_shuffle(interaction, True)
432448

433449
@discord.ui.button(
434450
label="Stop Sync",
@@ -437,8 +453,8 @@ async def shuffle(
437453
emoji="♾️",
438454
)
439455
async def stopSync(
440-
self, button: discord.ui.Button, interaction: discord.Interaction
456+
self, interaction: discord.Interaction, button: discord.ui.Button
441457
):
442458
self.player.updateNowPlaying.cancel()
443-
self.disable_all_items()
459+
self.clear_items()
444460
await interaction.response.edit_message(view=self)

rosetta/utils/embeds.py

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from datetime import datetime
33
from typing import List
44

5-
from discord import Colour, Embed, EmbedField, EmbedFooter, User
5+
from discord import Colour, Embed, User
66
from openai.types.completion_usage import CompletionUsage
77

88
from .track import Track
@@ -12,77 +12,80 @@
1212

1313

1414
def PingEmbed(user: User, latency: float):
15-
return Embed(
15+
embed = Embed(
1616
title=":ping_pong: Pong",
1717
description=f"Ball flew back in {int(latency * 1000)}ms",
1818
colour=Colour.teal(),
19-
footer=EmbedFooter(text=user.name, icon_url=user.avatar),
2019
timestamp=datetime.now(),
2120
)
21+
embed.set_footer(text=user.name, icon_url=user.avatar)
22+
return embed
2223

2324

2425
def SuccessEmbed(user: User, message: str):
25-
return Embed(
26+
embed = Embed(
2627
title=f"{config['bot.emoji']['success']} Success",
2728
description=message,
2829
colour=Colour.green(),
29-
footer=EmbedFooter(text=user.name, icon_url=user.avatar),
3030
timestamp=datetime.now(),
3131
)
32+
embed.set_footer(text=user.name, icon_url=user.avatar)
33+
return embed
3234

3335

3436
def ErrorEmbed(user: User, error: str):
35-
return Embed(
37+
embed = Embed(
3638
title=f"{config['bot.emoji']['error']} Error",
3739
description=error,
3840
colour=Colour.red(),
39-
footer=EmbedFooter(text=user.name, icon_url=user.avatar),
4041
timestamp=datetime.now(),
4142
)
43+
embed.set_footer(text=user.name, icon_url=user.avatar)
44+
return embed
4245

4346

4447
def InfoEmbed(user: User, message: str):
45-
return Embed(
48+
embed = Embed(
4649
title=":information_source: Info",
4750
description=message,
4851
colour=Colour.blurple(),
49-
footer=EmbedFooter(text=user.name, icon_url=user.avatar),
5052
timestamp=datetime.now(),
5153
)
54+
embed.set_footer(text=user.name, icon_url=user.avatar)
55+
return embed
5256

5357

5458
def NowPlayingEmbed(track: Track, queue: List[Track]):
55-
queueMessage = None
56-
if len(queue) > 0:
57-
queueMessage = [
58-
EmbedField(
59-
name=f"💭 Next ({len(queue)} left)",
60-
value=f"{'\n'.join([f'- [{t.title}]({t.url}) `{t.time[1]}`' for t in queue[:3]])}",
61-
)
62-
]
63-
return Embed(
59+
embed = Embed(
6460
title=f"{config['bot.emoji']['youtube']} Now Playing",
6561
description=f"[**{track.title}**]({track.url})\n\n`{track.time[0]}`{track.progress}`{track.time[1]}`\n",
66-
thumbnail=track.thumbnail,
67-
fields=queueMessage,
6862
colour=Colour.green(),
69-
footer=EmbedFooter(text=track.author.name, icon_url=track.author.avatar),
7063
timestamp=datetime.now(),
7164
)
65+
embed.set_thumbnail(url=track.thumbnail)
66+
if len(queue) > 0:
67+
embed.add_field(
68+
name=f"💭 Next ({len(queue)} left)",
69+
value=f"{'\n'.join([f'- [{t.title}]({t.url}) `{t.time[1]}`' for t in queue[:3]])}",
70+
inline=False
71+
)
72+
embed.set_footer(text=track.author.name, icon_url=track.author.avatar)
73+
return embed
7274

7375

7476
def LeaveEmbed(user: User):
75-
return Embed(
77+
embed = Embed(
7678
title=":wave: Leaving",
7779
description="Bye~",
7880
colour=Colour.teal(),
79-
footer=EmbedFooter(text=user.name, icon_url=user.avatar),
8081
timestamp=datetime.now(),
8182
)
83+
embed.set_footer(text=user.name, icon_url=user.avatar)
84+
return embed
8285

8386

8487
def SearchEmbed(user: User, keyword: str, tracks: List[Track]):
85-
return Embed(
88+
embed = Embed(
8689
title=f':mag: Search result of **"{keyword}"**',
8790
description="\n".join(
8891
[
@@ -91,9 +94,10 @@ def SearchEmbed(user: User, keyword: str, tracks: List[Track]):
9194
]
9295
),
9396
colour=Colour.teal(),
94-
footer=EmbedFooter(text=user.name, icon_url=user.avatar),
9597
timestamp=datetime.now(),
9698
)
99+
embed.set_footer(text=user.name, icon_url=user.avatar)
100+
return embed
97101

98102

99103
def ProcessingEmbed(user: User, message: str = "Processing..."):

0 commit comments

Comments
 (0)