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

2024-10-02 02:36:48 发布

您现在位置:Python中文网/ 问答频道 /正文

这是很新的。我一直在阅读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'])

我们的目标是让bot加入targetID用户加入的语音频道,但我在让bot加入频道时遇到了麻烦


Tags: importidgetasyncosondefbot
2条回答

根据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()

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

相关问题 更多 >

    热门问题