你如何检查是否有人在 Discord 中加入语音频道,然后向某人发送 dm

时间:2021-05-28 15:39:16

标签: python discord discord.py

我想要做的是,如果有人加入特定的语音频道,我会收到一个 DM。我是编码新手(只有很少的经验),所以我必须使用互联网。我的代码是来自互联网的组合。我只是让它工作,当有人加入时,它会在控制台中打印一些东西。

@bot.event
async def on_voice_state_update(member, before, after):
    targetID = bot.get_user(747390765428113418)

    if before.channel is None and after.channel is not None: 
        print(" Member joined")

为了澄清,我想将消息发送给 myslef

2 个答案:

答案 0 :(得分:0)

你快到了。为了向自己发送消息,您必须执行discord.Member.send()。您还需要获取成员对象,因为 get_user 方法返回的是用户而不是成员。

targetID = member.guild.get_member(747390765428113418) # Get member from guild
await targetID.send(f"{member.mention} has joined the VC {after.channel.mention}")

这将发送给您在代码中指定的 ID 为 747390765428113418 的用户。

答案 1 :(得分:0)

A 稍微更改了您的代码:

@bot.event
async def on_voice_state_update(member, before, after):
    if before.channel is None and after.channel is not None: #Checking member is joined (not left) from voice channel
        await member.send("Your message") #Sending message