检查用户是否在语音频道 discord.py [英] Check if user is in a voice channel discord.py

查看:24
本文介绍了检查用户是否在语音频道 discord.py的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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()

还有一件事;我想知道为什么当我的机器人与 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

这篇关于检查用户是否在语音频道 discord.py的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆