【发布时间】: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