我希望每当用户加入频道时使漫游器上线,并在用户离开频道时使其脱机。我对discord.js很陌生,但这是我的代码:
@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 before.channel is None and after.channel is not None:
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("Bot done0")
print(member, "joined")
@client.event
async def on_voice_state_update(member, before, after):
if before.channel is not None and after.channel is None:
if before.channel.id == [""Channel ID 1""] or before.channel.id == [""Channel ID 2""]:
await client.change_presence(status=discord.Status.offline)
print("Bot done1")
print(member, "left")
我当然会用通道ID替换““通道ID 1”“和”“通道ID 2”“。
感谢您的帮助!
答案 0 :(得分:0)
您有2个'def'命名为:“ on_voice_state_update”,因此,当此事件发生时,机器人不知道他必须呼叫哪个。删除2个“ def”之一,然后仅在一个名为“ on_voice_state_update”的“ def”中执行所需的操作。
@client.event
async def on_voice_state_update(member, before, after):
if before.channel is None and after.channel is not None:
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("Bot done0")
print(member, "joined")
elif before.channel is not None and after.channel is None:
if before.channel.id == [""Channel ID 1""] or before.channel.id == [""Channel ID 2""]:
await client.change_presence(status=discord.Status.offline)
print("Bot done1")
print(member, "left")