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

discord.py创建一个bot,当特定用户加入语音频道时,该bot在文本频道中发送专门的预设消息,discord.py,Discord.py,例如,当user1连接到vc1时,在文本频道general中发送“hello user1”,但当user2连接到vc1时,在文本频道general中发送“hi user2” 我刚开始使用discord.py和python进行编码,一般来说,最简单的方法是什么 下面是我迄今为止尝试过的一个例子 @client.event async def on_voice_state_update(member, before, after): print(member) vc

例如,当user1连接到vc1时,在文本频道general中发送“hello user1”,但当user2连接到vc1时,在文本频道general中发送“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

从这个角度来看,你的活动是构建得很好的,但是多一点结构会很好,而且有些事情是完全错误的

查看以下代码:

@client.event
语音状态更新时的异步定义(成员、之前、之后):
通道=在.channel之前或之后
如果channel.id==VoiceChannelID:#插入语音频道id
如果before.channel为None,after.channel为None:#成员加入定义的通道
channel1=client.get_channel(GeneralTextChatID)#定义常规通道
等待频道1。发送(f“欢迎来到语音频道{会员.提及}!”)#提及会员
我们做了什么?

  • 在用户加入之前和之后检查频道(
    channel=
  • 已检查连接的通道是否与定义的通道匹配
  • 如果频道正确,则发送消息
  • 使用
    f字串
    来提及成员

应该注意,f-string需要python 3.6+您是上帝,非常感谢!这正是我需要的。祝你今天愉快,伙计。