gpt4 book ai didi

python - 检查用户是否在语音 channel discord.py

转载 作者:行者123 更新时间:2023-12-04 22:52:12 31 4
gpt4 key购买 nike

我正在制作一个可以在 vc 中播放声音的机器人。我已经编写了加入通话的代码,播放 mp3,然后离开通话,但是当用户不在通话中时,我收到此错误:

Ignoring exception in command ring:
Traceback (most recent call last):
File "C:\Users\max\AppData\Roaming\Python\Python39\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "C:\Users\max\OneDrive\Desktop\Code\DiscordBots\Nokia\Nokia.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:\Users\max\AppData\Roaming\Python\Python39\site-packages\discord\ext\commands\bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\max\AppData\Roaming\Python\Python39\site-packages\discord\ext\commands\core.py", line 859, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\max\AppData\Roaming\Python\Python39\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: AttributeError: 'NoneType' object has no attribute 'channel'
这是我到目前为止的代码:
@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()
如果有人可以帮助我检查用户是否在语音 channel 中,那就太好了。

最佳答案

您可以使用 if 语句简单地检查它是否不是非类型

@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

关于python - 检查用户是否在语音 channel discord.py,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65385279/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com