so i want make a discord bot that joins my channels and plays a mp3 file from my computer.
the problem is when the bot joins it gives me this error : RuntimeError: PyNaCl library needed in order to use voice.
but i know for sure pynacl is installed.
and how do i let the bot play mp3 files ?
this is the code :
import discord
from discord.ext import commands
from discord.ext.commands import Bot
import asyncio
client = discord.Client()
bot = commands.Bot(command_prefix= "#")
@bot.command(pass_context=True)
async def brent(ctx):
voice_channel = ctx.message.author.voice.voice_channel
await bot.join_voice_channel(voice_channel)
you need to install discord.py voice
run this in your terminal:
python3 -m pip install -U discord.py[voice]
i did it but still the same error :
Ignoring exception in command brent
Traceback (most recent call last):
File "C:\Usersangelo\PycharmProjectsangelo\venv\lib\site-packages\discord\ext\commands\core.py", line 50, in wrapped
ret = yield from coro(*args, kwargs)
File "C:/Users/angelo/PycharmProjects/angelo/bot.py", line 59, in brent
await bot.join_voice_channel(voice_channel)
File "C:\Usersangelo\PycharmProjectsangelo\venv\lib\site-packages\discord\client.py", line 3209, in join_voice_channel
voice = VoiceClient(kwargs)
File "C:\Usersangelo\PycharmProjectsangelo\venv\lib\site-packages\discord\voice_client.py", line 217, in __init__
raise RuntimeError("PyNaCl library needed in order to use voice")
RuntimeError: PyNaCl library needed in order to use voice
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Usersangelo\PycharmProjectsangelo\venv\lib\site-packages\discord\ext\commands\bot.py", line 846, in process_commands
yield from command.invoke(ctx)
File "C:\Usersangelo\PycharmProjectsangelo\venv\lib\site-packages\discord\ext\commands\core.py", line 374, in invoke
yield from injected(ctx.args, *ctx.kwargs)
File "C:\Usersangelo\PycharmProjectsangelo\venv\lib\site-packages\discord\ext\commands\core.py", line 54, in wrapped
raise CommandInvokeError(e) from e
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: RuntimeError: PyNaCl library needed in order to use voice
Well I dont think you have, ive just tried this and it works for me...
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix= "#")
@bot.event
async def on_ready():
print('Bot ready')
@bot.command(pass_context=True)
async def brent(ctx):
voice_channel = ctx.message.author.voice.voice_channel
await bot.join_voice_channel(voice_channel)
bot.run('token')
the bot joins the channel im in but it gives me that error so i probably cant play sound i think.
im new in python so im sorry if i say stupid things.
btw do you know how i let the bot play mp3 files or play sound from an url ?
i suggest you learn python first before creating a discord bot, I am not going to spoonfeed code.
i know the basics but im new to making discord bots. the only thing i want to do is troll my friends by playing i file have
I'm giving you a small snippet of code for playing mp3 from a file...
The code is written with discord.py rewrite and here are the docs, you should really take a look at them, because everything is explained there.
voice = await message.author.voice.channel.connect()
voice.play(discord.FFmpegPCMAudio('./res/rick_roll.mp3'))
and if you want specific length:
voice = await message.author.voice.channel.connect()
voice.play(discord.FFmpegPCMAudio('./res/rick_roll.mp3'))
counter = 0
duration = 10 # In seconds
while not counter >= duration:
await asyncio.sleep(1)
counter = counter + 1
await voice.disconnect()
thanks for helping me but i still get this error : RuntimeError: PyNaCl library needed in order to use voice
Because you don't have PyNaCl installed, which is listed in the documentation as a requirement for voice.
thats weird maybe i installed i wrong or something. how should i install it ?
Like I said above:
python3 -m pip install -U discord.py[voice] put that into your terminal
this is what i get:
Requirement already up-to-date: discord.py[voice] in c:\usersangeloappdata\local\programs\python\python36-32\lib\site-packages
Requirement already up-to-date: aiohttp<1.1.0,>=1.0.0 in c:\usersangeloappdata\local\programs\python\python36-32\lib\site-packages (from discord.py[voice])
Requirement already up-to-date: websockets<4.0,>=3.1 in c:\usersangeloappdata\local\programs\python\python36-32\lib\site-packages (from discord.py[voice])
Requirement already up-to-date: PyNaCl==1.0.1; extra == "voice" in c:\usersangeloappdata\local\programs\python\python36-32\lib\site-packages (from discord.py[voice])
Requirement already up-to-date: async-timeout in c:\usersangeloappdata\local\programs\python\python36-32\lib\site-packages (from aiohttp<1.1.0,>=1.0.0->discord.py[voice])
Requirement already up-to-date: chardet in c:\usersangeloappdata\local\programs\python\python36-32\lib\site-packages (from aiohttp<1.1.0,>=1.0.0->discord.py[voice])
Requirement already up-to-date: multidict>=2.0 in c:\usersangeloappdata\local\programs\python\python36-32\lib\site-packages (from aiohttp<1.1.0,>=1.0.0->discord.py[voice])
Requirement already up-to-date: cffi>=1.1.0 in c:\usersangeloappdata\local\programs\python\python36-32\lib\site-packages (from PyNaCl==1.0.1; extra == "voice"->discord.py[voice])
Requirement already up-to-date: six in c:\usersangeloappdata\local\programs\python\python36-32\lib\site-packages (from PyNaCl==1.0.1; extra == "voice"->discord.py[voice])
Requirement already up-to-date: pycparser in c:\usersangeloappdata\local\programs\python\python36-32\lib\site-packages (from cffi>=1.1.0->PyNaCl==1.0.1; extra == "voice"->discord.py[voice])
and it still doesnt work
Open Command Prompt as an Administrator.
Run:
py -3.6 -m pip install -U pynacl
When running your bot:
py -3.6 my_bot_file.py
If you are using an IDE, ensure that the interpreter is the correct one.
mysterialPy thanks man , the bot joins and plays my file :D
but now it ignores my other commands :/
discord.ext.commands.errors.CheckFailure: The check functions for command (my other commands) failed.
nvm it worked thanks :)
voice = await message.author.voice.channel.connect()
NameError: name 'message' is not defined
Any fix?
@avrwhy Please stop necroing issues.
As the README recommends, you should join either the official discord.py server or the Discord API server for help using the library.
Most helpful comment
I'm giving you a small snippet of code for playing mp3 from a file...
The code is written with discord.py rewrite and here are the docs, you should really take a look at them, because everything is explained there.
and if you want specific length: