You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Python开发Discord机器人实现@提及成员功能的相关问题

Discord机器人@指定成员代码修改方案

你要实现@成员的效果,需要调用Discord.py封装的成员提及属性,两种常用场景的修改方式如下:

  • 场景1:@发送|bro命令的触发者
    直接调用message.author.mention获取触发用户的提及字段即可,修改后代码:
@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('|bro'):
        await message.channel.send(f"You are dumb {message.author.mention}")
  • 场景2:@用户发送命令时指定的其他成员
    适配|bro @xxx的命令格式,从消息的提及成员列表中取目标对象即可:
@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('|bro'):
        # 判断是否有@目标成员
        if not message.mentions:
            await message.channel.send("请在命令后@你要指定的成员")
            return
        target_member = message.mentions[0]
        await message.channel.send(f"You are dumb {target_member.mention}")

补充说明:mention是Discord官方封装的提及标识,直接拼接在消息内容中发送,就会在频道内显示为可点击的@成员效果,不需要手动拼接@符号加用户名。

内容的提问来源于stack exchange,提问作者Cologne

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 13:06:04