問題描述
我正在制作一個可以在 vc 中播放聲音的機器人.我已經(jīng)編寫了加入通話的代碼,播放 mp3,然后離開通話,但是當(dāng)用戶不在通話中時,我收到此錯誤:
I am making a bot that will play sounds in a vc. I have made the code to join the call, play the mp3, then leave the call but when the user is not in a call I get this error:
Ignoring exception in command ring:
Traceback (most recent call last):
File "C:UsersmaxAppDataRoamingPythonPython39site-packagesdiscordextcommandscore.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "C:UsersmaxOneDriveDesktopCodeDiscordBotsNokiaNokia.py", line 91, in ring
channel = ctx.author.voice.channel
AttributeError: 'NoneType' object has no attribute 'channel'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:UsersmaxAppDataRoamingPythonPython39site-packagesdiscordextcommandsot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "C:UsersmaxAppDataRoamingPythonPython39site-packagesdiscordextcommandscore.py", line 859, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:UsersmaxAppDataRoamingPythonPython39site-packagesdiscordextcommandscore.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'channel'
這是我目前擁有的代碼:
This is my code that I have so far:
@client.command()
async def ring(ctx):
channel = ctx.author.voice.channel
vc = await channel.connect()
vc.play(discord.FFmpegPCMAudio("Audio/NokiaRingtone.mp3"))
time.sleep(5)
await ctx.voice_client.disconnect()
還有一件事;我想知道為什么當(dāng)我的機器人與 vc 斷開連接時,我會在聊天中得到這些:
One more thing; I was wondering why I get these in the chat when my bot disconnects from the vc:
websocket connection is closing.
websocket connection is closing.
websocket connection is closing.
如果有人可以幫我檢查用戶是否在語音頻道中,并告訴我為什么我的控制臺中有這三行,那就太好了.
If anyone could help me check if the user is in a voice channel and tell me why I get those three lines in my console that would be great.
推薦答案
你可以簡單地用 if 語句檢查它是否不是非類型
You can simply check if it's not a nonetype with an if statement
@bot.command()
async def foo(ctx):
voice_state = ctx.member.voice
if voice_state is None:
# Exiting if the user is not in a voice channel
return await ctx.send('You need to be in a voice channel to use this command')
# Put the rest of the code here
這篇關(guān)于檢查用戶是否在語音頻道 discord.py的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!