当特定的人加入语音频道时,我如何制作一个加入语音频道的机器人



对此非常陌生。我一直在阅读readthedocs API对discord.py的参考,这对我来说没有多大意义。到目前为止,我有

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用户加入的语音频道,但我一开始就很难让机器人加入一个频道。

根据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匹配。

on_voice_state_update就是您所需要的。当服务器上发生语音状态更改时,将调用此事件,包括

  • 成员加入语音频道。

  • 成员离开语音频道。

  • 一个成员会自动静音或震耳欲聋。

  • 一名会员被公会管理员静音或震耳欲聋。

此函数接受3个参数,即发生更新的成员、之前的状态和之后的状态。当触发此事件时,请检查member.id == target_member.id。如果是这种情况,则await after.channel.connect()

你还应该添加检查机器人是否已经在这个频道和其他东西中,但这应该为你指明正确的方向

最新更新