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

Discord中@here机制及discord.py相关实现技术疑问

Discord API & discord.py 技术疑问解答

1. @here 是否属于 discord.Role?

@here 不是标准的discord.Role对象。

通过以下代码可验证这一点:

  • 遍历成员角色列表,仅会返回实际创建的服务器角色与@everyone:
@bot.command(name = "printMemberRoles")
async def printMemberRoles(ctx : commands.Context, user_id : int):
    member = ctx.guild.get_member(user_id)
    for role in member.roles:
        print(role.name, str(role.id))
  • 遍历服务器角色列表,同样不会出现@here:
@bot.command(name = "printGuildRoles")
async def printGuildRoles(ctx : commands.Context):
    for role in ctx.guild.roles:
        print(role.name, str(role.id))

至于AllowedMentions类将@here与@everyone归为同一参数,是因为二者均为Discord平台定义的特殊提及类型,而非实际角色实体,它们的权限控制与通知逻辑由平台统一处理。

2. 若@here不是角色,Discord如何实现@在线成员的通知?

Discord的@here是服务器端原生支持的特殊提及机制,和你实现的fake_here逻辑完全不同:

  • 你的fake_here是通过遍历频道成员、筛选在线用户后显式提及每个成员,消息内容会包含所有符合条件成员的@标识;
  • 而Discord的@here是平台层面的指令,发送后由Discord服务器直接向当前频道的在线成员推送通知,消息内容仅保留@here文本,不会展开成具体成员提及,且这部分筛选逻辑是Discord内部处理的,不会开放给第三方开发者。

你的fake_here实现代码:

@bot.command(name = "fake_here")
async def fake_here(ctx : commands.Context):
    mentions = [member.mention for member in ctx.channel.members if member.status != discord.Status.offline]
    await ctx.send(" ".join(mentions))

3. 能否通过discord.py自定义类似@here的"提及装饰器"?

可以实现类似的批量通知效果,但无法让Discord原生识别自定义的@many_roles这类标识,具体实现思路如下:

  1. 先筛选符合条件的成员(比如拥有至少5个角色的用户);
  2. 发送消息时拼接这些成员的提及,同时可添加自定义文本模拟标识,实际触发通知的仍是成员的mention属性。

示例代码:

@bot.command(name="many_roles_mention")
async def many_roles_mention(ctx: commands.Context):
    # 筛选拥有至少5个角色的非机器人成员
    target_members = [member for member in ctx.guild.members if len(member.roles) >=5 and not member.bot]
    if not target_members:
        await ctx.send("没有符合条件的成员")
        return
    mentions = " ".join([m.mention for m in target_members])
    await ctx.send(f"@many_roles {mentions}")

这种方式能实现类似@here的批量通知,自定义文本仅作显示用,真正触发通知的是后面的成员提及。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 07:21:34