对此非常陌生。我一直在阅读 discord.py 的 readthedocs API 参考,但它对我来说没有多大意义。到目前为止,我有
import os
import discord
#os.environ['targetID']
bot = discord.Client()
intents = discord.Intents.default()
intents.members = True
channel_id = os.environ['channelID']
voice_channel = bot.get_channel(channel_id)
async def on_ready():
print ("Ready")
channel_id = os.environ['channelID']
voice_channel = bot.get_channel(channel_id)
await voice_channel.connect()
#async def on_voice_state_update(Member, channel[None], channel['general']):
# print(client.member.id)
#move_to(None)
bot.run(os.environ['token'])
目标是让机器人加入具有 targetID
的用户加入的语音频道,但我无法首先让机器人加入频道。
答案 0 :(得分:0)
on_voice_state_update 正是您所需要的。当服务器上发生语音状态更改时将调用此事件,这包括
一名成员加入了一个语音频道。
一名成员离开了语音频道。
成员被自己静音或耳聋。
一名成员被公会管理员静音或耳聋。
这个函数接受 3 个参数,更新发生的成员,之前的状态和之后的状态。
触发此事件时,请检查 member.id == target_member.id
。如果是这样,那么await after.channel.connect()
你还应该添加检查机器人是否已经在这个频道和其他东西中,但这应该为你指明正确的方向
答案 1 :(得分:0)
根据 Nathan Marotte 的回答,我将提供一个代码示例。
您可以使用 on_voice_state_update 函数来检查成员所在的频道。
因此看看下面的代码:
@bot.event
async def on_voice_state_update(member, before, after):
targetID = bot.get_user(TargetIDHere)
if before.channel is None and after.channel is not None and member.id == targetID.id: # Condition that must be fulfilled
await member.voice.channel.connect() # Connect to the channel
解释代码中的不同功能:
before.channel is None
= 检查用户是否在频道中/不在频道中。
after.channel is not None
= 检查用户加入的频道,然后授予角色。
member.id == targetID.id
= 检查加入的 member.id
是否匹配 targetID.id
。