【问题标题】:Check if user is in a voice channel discord.py检查用户是否在语音频道 discord.py
【发布时间】:2021-03-30 18:35:31
【问题描述】:

我正在制作一个可以在 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()

还有一件事;我想知道为什么当我的机器人与 vc 断开连接时,我会在聊天中得到这些:

websocket connection is closing.
websocket connection is closing.
websocket connection is closing.

如果有人可以帮助我检查用户是否在语音频道中,并告诉我为什么我的控制台中有这三行,那就太好了。

【问题讨论】:

    标签: python ffmpeg discord.py


    【解决方案1】:

    您可以简单地使用 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
    

    【讨论】:

      猜你喜欢
      • 2021-09-08
      • 2021-04-13
      • 2021-07-05
      • 1970-01-01
      • 2021-05-03
      • 1970-01-01
      • 2018-04-04
      • 2021-08-05
      • 2019-07-03
      相关资源
      最近更新 更多