discord.py 创建一个机器人,当特定用户加入语音频道时,它会在文本频道中发送专门的预设消息

时间:2021-03-30 00:07:58

标签: discord.py

例如,当 user1 连接到 vc1 时,“hello user1”在文本通道通用中发送,但当用户 2 连接到 vc1 时,“hi user2”在文本通道通用中发送

我是使用 discord.py 和 python 编码的新手,一般来说,最简单的方法是什么 以下是我迄今为止尝试过的示例

@client.event
    async def on_voice_state_update(member, before, after):
      print(member)
      vc = client.get_channel(id=channel id number)
      vc1 = vc.members
      if member in vc1[0] == "User#1234":
        send msg in general text channel

1 个答案:

答案 0 :(得分:1)

从角度来看,您的活动构建得很好,但多一点结构会更好,而且有些地方完全错误。

看看下面的代码:

@client.event
async def on_voice_state_update(member, before, after):
    channel = before.channel or after.channel

    if channel.id == VoiceChannelID: # Insert voice channel ID
        if before.channel is None and after.channel is not None: # Member joins the defined channel
            channel1 = client.get_channel(GeneralTextChatID) # Define the general channel
            await channel1.send(f"Welcome to the voice channel {member.mention}!") # Mention the member

我们做了什么?

  • 在用户加入之前和之后检查频道 (channel=)
  • 检查加入的频道是否与定义的频道匹配
  • 如果频道正确则发送消息
  • 使用 f-string 提及该成员
相关问题