discord.py按名称提及用户

时间:2020-11-07 00:51:16

标签: python discord discord.py

我想在discord.py中用用户名提及用户。我当前的代码是:

@bot.command(name='mention')
@commands.has_role(OwnerCommands)
async def mention(ctx, *, member: discord.Member):
    memberid = member.id
    await ctx.message.delete()
    await ctx.send('<@{}>'.format(memberid))

但是它不起作用。我该怎么做?

编辑,我找到了答案。

4 个答案:

答案 0 :(得分:1)

这很简单member.mention

@bot.command(name='mention')
@commands.has_role(OwnerCommands)
async def mention(ctx, *, member: discord.Member):
    await ctx.message.delete()
    await ctx.send(member.mention)

答案 1 :(得分:0)

我们可以跳过手动工作,而直接使用不和谐的Member Model。成员对象上的mention返回字符串,使我们可以直接提及它们。

@bot.command(name='mention')
@commands.has_role(OwnerCommands)
async def mention(ctx, *, member: discord.Member):
    await ctx.message.delete()
    await ctx.send(member.mention)

答案 2 :(得分:0)

没关系,我找到了办法。我只是在Discord Developers页面中打开了Privileged indents。

答案 3 :(得分:0)

另一种解决方案,如果您不想使用discord.py MemberConverter 扩展名来两次提及成员,也不要忘记从discord开发人员门户激活成员特权缩进。 / p>

资源: https://discordpy.readthedocs.io/en/rewrite/ext/commands/api.html#discord.ext.commands.MemberConverter How to convert a string to a user discord.py

import discord
from discord.ext import commands
from discord.ext.commands import MemberConverter 

intents = discord.Intents(members = True)

client = commands.Bot(command_prefix="$", intents=intents)

@client.command()
async def mention(ctx, member):
  converter = MemberConverter()
  member = await converter.convert(ctx, (member))
  await ctx.send(member.mention)