简体   繁体   English

discord.py 按名称提及用户

[英]discord.py Mention user by name

I am trying to mention a user by their name in discord.py.我试图在 discord.py 中以用户的名字提及用户。 My current code is:我目前的代码是:

@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))

But it does not work.但它不起作用。 How do I do this?我该怎么做呢?

Edit, I found an answer.编辑,我找到了答案。

It is rather straight forward member.mention这是相当直接的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)

We can skip the manual work and use discord's Member Model directly.我们可以跳过手工操作,直接使用discord的Member Model The mention on the Member object returns us the string to allow us to mention them directly. 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)

Nevermind, I found a way.没关系,我找到了方法。 I simply turned on Privileged indents in the Discord Developers page.我只是在 Discord Developers 页面中打开了特权缩进。

Another solution if you don't want to mention the member twice by using discord.py MemberConverter extension, And also don't forget to activate the members privileged indents from the discord developer portal.如果您不想通过使用 discord.py MemberConverter扩展两次提及成员,另一种解决方案,并且不要忘记从不和谐开发者门户激活成员特权缩进。

resources: https://discordpy.readthedocs.io/en/rewrite/ext/commands/api.html#discord.ext.commands.MemberConverter How to convert a string to a user discord.py资源: https : //discordpy.readthedocs.io/en/rewrite/ext/commands/api.html#discord.ext.commands.MemberConverter 如何将字符串转换为用户 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM