I’m trying to create a simple discord bot in python that would broadcast my webradio in a voice channel. I found this topic (How to create a discord bot that streams online radio in Python), but using the given code, I get the following error:
python radio.py
Traceback (most recent call last):
File "/home/radio.py", line 11, in <module>
client = Bot(command_prefix=list(PREFIX))
TypeError: 'NoneType' object is not iterable
Here is the code I’ve used:
import os
from discord import FFmpegPCMAudio
from discord.ext.commands import Bot
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
PREFIX = os.getenv('DISCORD_PREFIX')
client = Bot(command_prefix=list(PREFIX))
@client.event
async def on_ready():
print('Music Bot Ready')
@client.command(aliases=['p', 'pla'])
async def play(ctx, url: str = 'http://stream.radioparadise.com/rock-128'):
channel = ctx.message.author.voice.channel
global player
try:
player = await channel.connect()
except:
pass
player.play(FFmpegPCMAudio('http://stream.radioparadise.com/rock-128'))
@client.command(aliases=['s', 'sto'])
async def stop(ctx):
player.stop()
client.run(TOKEN)
Being a beginner, I’m stuck on this error, how can I solve it?
EDIT: I followed Puncher’s recommendation and now I’m getting this error:
Music Bot Ready
pla
Ignoring exception in command play:
Traceback (most recent call last):
File "/usr/local/lib/python3.10/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "/home/radio.py", line 27, in play
player.play(FFmpegPCMAudio('https://rdx.kaed-tzo.com/listen/orx_radio/radio.mp3'))
NameError: name 'player' is not defined
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.10/site-packages/discord/ext/commands/bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "/usr/local/lib/python3.10/site-packages/discord/ext/commands/core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/usr/local/lib/python3.10/site-packages/discord/ext/commands/core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NameError: name 'player' is not defined
3
Answers
Learning from its mistakes, here is a working simple code. Hope this saves others some time.
The problem in your code is
client = Bot(command_prefix=list(PREFIX))
. You’re trying to convertPREFIX
to a list, butPREFIX
returnsNone
, that’s why this error occurs.The reason why it returns
None
is, that it can’t find the env variableDISCORD_PREFIX
in your.env
. Make sure that.env
and the env variable exists.It raises an exception. (Which is also ignored. This is why it’s generally a bad idea to bare except.)
You probably don’t want to be using
global player
anyway, because each instance of the command should probably have its own player.If you intended for everything to have its own player, simply except when you try to reference the player: