【问题标题】:Discord.py Message when member joins specific channelDiscord.py 会员加入特定频道时的消息
【发布时间】:2021-06-30 13:57:22
【问题描述】:

下午好。

我还有一个关于不和谐 python 机器人的问题。 所以我想做一个支持脚本之类的东西。我的想法是在所有支持者值班时为他们写一个命令,该命令保存在 json 文件中。这没问题,但我的问题是我希望用户在连接到名为“Support-Room”的特定语音频道时接收到有多少支持者值班。有谁知道我如何编写一个脚本,当成员加入这个特定的语音频道时,他会收到一条消息,例如:member.send('你现在在支持室。请等到支持者移动你。' )

@bot.command()
async def on_member_join(member):
    for channel in bot.get_all_channels():
        if channel.name == 'Support-Room':
            await member.send(''You are now in the Support-Room. Please wait till a Supporter moves you.')

这是我尝试过的,但它不起作用:(

【问题讨论】:

  • @bot.command() 错字 --> bot.command
  • 您真的(想要)在command 中使用event 吗?

标签: python discord discord.py


【解决方案1】:

混合事件和机器人命令不是它的工作方式。使用event 是解决方案。 我们必须使用一个名为on_voice_state_update 的事件,有关更多信息,请参阅docs

看看下面的代码:

@bot.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
            await member.send("Your text here.") # Send a DM to the member who joined

我们做了什么?

  • 查看了他之前和现在所在的频道。
  • 检查加入的频道是否与定义的频道匹配。
  • 如果频道正确,则向用户发送消息。

【讨论】:

    猜你喜欢
    • 2021-06-06
    • 1970-01-01
    • 2020-12-03
    • 2021-07-30
    • 1970-01-01
    • 2021-07-28
    • 2021-03-18
    • 1970-01-01
    • 2021-02-27
    相关资源
    最近更新 更多