当机器人独自一人时不会离开语音频道

发布于 2025-01-11 07:56:47 字数 454 浏览 0 评论 0原文

我有一个个人音乐机器人,可以为我和朋友播放音乐,我只有一个播放和离开命令,这两个命令都可以完美工作,但我想知道是否可以对机器人进行一点升级,让机器人在单独放置时自动离开语音通道。在阅读了一些文档之后,我想我明白了,但是我的这段代码根本不起作用,它几乎就像 python 由于某种原因忽略了它,所以我想我在这里遗漏了一些东西...... 所以我想知道我的代码是否有原因:

    @client.event
async def on_voice_state_update(member):
    voice_state = member.guild.voice_client
    if len(voice_state.channel.members) == 1:
        await voice_state.disconnect()

无法工作,我没有收到任何错误消息,而且实际上什么也没有发生。这里的一切都是应该的吗?

I have a personal music bot that plays music for me and friends, I only have a play and leave commands which both work perfectly, but I was wondering if a little upgrade to the bot automatically leaving voice channel when left in it alone was possible. After some reading in documentation, I thought I got it but this piece of my code simply doesn't work, it's almost like python is ignoring it for some reason so I guess I am missing something here...
so I was wondering if there is a reason why my code:

    @client.event
async def on_voice_state_update(member):
    voice_state = member.guild.voice_client
    if len(voice_state.channel.members) == 1:
        await voice_state.disconnect()

won't work, I don't get any error messages and literally nothing happens. Is it all as it should be here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩人痒 2025-01-18 07:56:47

您缺少此事件的一些参数。只需添加 beforeafter 就可以了。

完整的事件可能是:

@client.event
async def on_voice_state_update(member, before, after):
    voice_state = member.guild.voice_client
    if voice_state is not None and len(voice_state.channel.members) == 1:
    # If the bot is connected to a channel and the bot is the only one in the channel
        await voice_state.disconnect() # Disconnect the bot from the channel

您可以在此处的文档中查看更多信息:

You are missing some arguments for this event. Simply add before and after and you should be fine.

The full event could be:

@client.event
async def on_voice_state_update(member, before, after):
    voice_state = member.guild.voice_client
    if voice_state is not None and len(voice_state.channel.members) == 1:
    # If the bot is connected to a channel and the bot is the only one in the channel
        await voice_state.disconnect() # Disconnect the bot from the channel

You can see more in the docs here:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文