not-lain commited on
Commit
036628e
·
1 Parent(s): 77815e1

Refactor command handling to use application commands and improve error handling

Browse files
Files changed (1) hide show
  1. app.py +18 -9
app.py CHANGED
@@ -73,11 +73,14 @@ music_bot = MusicBot()
73
  @bot.event
74
  async def on_ready():
75
  print(f'Bot is ready! Logged in as {bot.user}')
 
76
  try:
77
- synced = await bot.tree.sync()
78
- print(f"Synced {len(synced)} command(s)")
 
 
79
  except Exception as e:
80
- print(e)
81
 
82
  @bot.tree.command(name="play", description="Play a song from URL")
83
  async def play(interaction: discord.Interaction, url: str):
@@ -92,20 +95,26 @@ async def play(interaction: discord.Interaction, url: str):
92
  else:
93
  await interaction.followup.send('Added to queue!')
94
 
95
- @bot.command()
96
- async def skip(ctx):
 
97
  if music_bot.voice_client:
98
  music_bot.voice_client.stop()
99
- await ctx.send('Skipped current song!')
 
 
100
 
101
- @bot.command()
102
- async def leave(ctx):
 
103
  if music_bot.voice_client:
104
  await music_bot.voice_client.disconnect()
105
  music_bot.voice_client = None
106
  music_bot.queue = []
107
  music_bot.is_playing = False
108
- await ctx.send('Bot disconnected!')
 
 
109
 
110
  def run_discord_bot():
111
  bot.run(os.getenv('DISCORD_TOKEN'))
 
73
  @bot.event
74
  async def on_ready():
75
  print(f'Bot is ready! Logged in as {bot.user}')
76
+ print("Syncing commands...")
77
  try:
78
+ await bot.tree.sync(guild=None) # Set to None for global sync
79
+ print("Successfully synced commands globally!")
80
+ except discord.app_commands.errors.CommandSyncFailure as e:
81
+ print(f"Failed to sync commands: {e}")
82
  except Exception as e:
83
+ print(f"An error occurred while syncing commands: {e}")
84
 
85
  @bot.tree.command(name="play", description="Play a song from URL")
86
  async def play(interaction: discord.Interaction, url: str):
 
95
  else:
96
  await interaction.followup.send('Added to queue!')
97
 
98
+ # Replace the existing skip command with this version
99
+ @bot.tree.command(name="skip", description="Skip the current song")
100
+ async def skip(interaction: discord.Interaction):
101
  if music_bot.voice_client:
102
  music_bot.voice_client.stop()
103
+ await interaction.response.send_message('Skipped current song!')
104
+ else:
105
+ await interaction.response.send_message('No song is currently playing!')
106
 
107
+ # Replace the existing leave command with this version
108
+ @bot.tree.command(name="leave", description="Disconnect bot from voice channel")
109
+ async def leave(interaction: discord.Interaction):
110
  if music_bot.voice_client:
111
  await music_bot.voice_client.disconnect()
112
  music_bot.voice_client = None
113
  music_bot.queue = []
114
  music_bot.is_playing = False
115
+ await interaction.response.send_message('Bot disconnected!')
116
+ else:
117
+ await interaction.response.send_message('Bot is not in a voice channel!')
118
 
119
  def run_discord_bot():
120
  bot.run(os.getenv('DISCORD_TOKEN'))