【问题标题】:Changing status of a discord.py Bot when a user enters a specific voice channel当用户进入特定语音频道时更改 discord.py Bot 的状态
【发布时间】:2020-10-10 17:41:30
【问题描述】:

我希望让机器人在用户加入频道时上线,并在用户离开频道时下线。我对 discord.js 很陌生,但这是我的代码:

import discord
from discord.ext import commands

client = commands.Bot(command_prefix = '.')

@client.event
async def on_ready():
    await client.change_presence(status=discord.Status.offline)
    print("Bot ready")

@client.event
async def on_voice_state_update(member, before, after):
    if not before.channel:
        if after.channel.id == [""Channel ID 1""] or after.channel.id == [""Channel ID 2""]:
                await client.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.listening, name="users | Ready to mute"))
                print(f'{member.name} joined {after.channel.name}')
    elif before.channel and after.channel:
        if before.channel != after.channel:
            if after.channel.id == [""Channel ID 1""] or after.channel.id == [""Channel ID 2""]:
                if member.roles[1].name == "Admin" or member.roles[1].name == "Moderator":
                    await client.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.listening, name="users | Ready to mute"))
                    print(f'{member.name} joined {after.channel.name}')
    elif before.channel and not after.channel:
            if member.roles[1].name == "Admin" or member.roles[1].name == "Moderator":
                await client.change_presence(status=discord.Status.offline)
                print(f'{member.name} left {before.channel.name}')

client.run("Key")

当然,我会将“Channel ID 1”和“Channel ID 2”替换为频道 ID,并将“Key”替换为我的身份验证密钥。

我遇到的问题是事件似乎没有被注册。

感谢您的帮助!

【问题讨论】:

  • 一些变化,id 是一个 int 而不是字符串,正如 here 所解释的那样。因此,您应该将 channel.id 与整数进行比较,而不是列表项。

标签: python discord discord.py discord.py-rewrite


【解决方案1】:

进行了一些更改,id 是一个 int 而不是一个字符串,如此处所述。所以 您应该将 channel.id 与整数进行比较,而不是列表 项目。

– 插入CheesyLine

【讨论】:

    猜你喜欢
    • 2018-04-04
    • 2021-04-05
    • 2020-11-04
    • 2020-10-07
    • 2021-07-13
    • 2021-03-02
    • 2020-10-04
    • 2022-01-15
    • 2021-06-06
    相关资源
    最近更新 更多