0

I am new to making discord bots and while I was making a music bot and run on my ubuntu server with python Venv I receive some random number while I tried to play music and the music didn't start. But when I run my program in my Windows PC it works fine.

[youtube] Extracting URL: https://youtu.be/1hOfUcLaqxs
[youtube] 1hOfUcLaqxs: Downloading webpage
[youtube] 1hOfUcLaqxs: Downloading ios player API JSON
[youtube] 1hOfUcLaqxs: Downloading android player API JSON
[youtube] 1hOfUcLaqxs: Downloading m3u8 information
1082175706039079022

Here is my code, can anyone pls help me, TQ

import nextcord
from nextcord.ext import commands
from nextcord import Intents
import requests
import json
import yt_dlp
import asyncio

intents = Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='?', intents=intents)

voice_clients = {}

yt_dl_opts = {'format':'bestaudio/best'}
ytdl = yt_dlp.YoutubeDL(yt_dl_opts)
ffmpeg_options = {'options':"-vn", "before_options": "-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5"}


@bot.event
async def on_message(msg):
    if msg.content.startswith("?play"):
        try:
            voice_client = await msg.author.voice.channel.connect()
            voice_clients[voice_client.guild.id] = voice_client
        except:
            print("error")
        try:
            url = msg.content.split()[1]

            loop = asyncio.get_event_loop()
            data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=False))
            
            song = data['url']
            player = nextcord.FFmpegPCMAudio(song, **ffmpeg_options)

            voice_clients[msg.guild.id].play(player)

        except Exception as err:
            print(err)

    if msg.content.startswith("?pause"):
        try:
            voice_clients[msg.guild.id].pause()
        
        except Exception as err:
            print(err)
    
    if msg.content.startswith("?resume"):
        try:
            voice_clients[msg.guild.id].resume()
        
        except Exception as err:
            print(err)

    if msg.content.startswith("?stop"):
        try:
            voice_clients[msg.guild.id].stop()
            await voice_clients[msg.guild.id].disconnect()
        
        except Exception as err:
            print(err)
    
@bot.command(name="HI")
async def SendMessage(ctx):
    await ctx.send('Hello Aquila Here ! ( •̀ ω •́ )✧')

@bot.command(name="info")
async def SendMessage(ctx):
    await ctx.send("HIHI It's Aquila This Bot is currently under development\nHere Are Some Commands Available \n\n?Quotes - Some Useless 名言\n?HI - Just... Say Hi ")

@bot.command(name="Quotes")
async def SendMessage(ctx):
    response = requests.get('https://v1.hitokoto.cn/')
    response_json = json.loads(response.text)
    hitokoto = str(response_json['hitokoto']) + str("--- ") + str(response_json['from'])
    await ctx.send(hitokoto)

@bot.event
async def on_ready():
    print("Aquila Online ! ( •̀ ω •́ )✧")

if __name__ == '__main__':
    bot.run("---")

I am not sure whether it is ffmpeg problem or yt_dlp Tried to run on Window PC It works but when running on Linux With Python Venv It cannot works

Yuhari
  • 1

1 Answers1

0

Avoid catching errors, printing them and ignoring them with try: ... except Exception as err: print(err), as you lose the traceback containing plenty of context, and that error may break future code.

My guess though from that being a snowflake-style ID is that it may be a KeyError from when you try to get the voice client from the dictionary. You do not need to store this in a global variable, your bot has a voice_clients property, and every Guild has a voice_client property (which is None if the client does not exist, make sure to handle that).

    try:
        voice_client = await msg.author.voice.channel.connect()
        voice_clients[voice_client.guild.id] = voice_client
    except:
        print("error")

This block is losing all errors and information from if connect() fails, please remove all of these so you get better error feedback.

ooliver
  • 9
  • 2