How To How To Convert Username To Discord Id?
Solution 1:
Use a converter to get the Member
object of the target, which will include their id
.
from discord import Member
from dicord.ext.commands import Bot
bot = Bot(command_prefix='!')
@bot.command()asyncdefgetids(ctx, member: Member):
await ctx.send(f"Your id is {ctx.author.id}")
await ctx.send(f"{member.mention}'s id is {member.id}")
bot.run("token")
Converters are pretty flexible, so you can give names, nicknames, ids, or mentions.
Solution 2:
on_message
callback function is passed the message
.
message
is a discord.Message
instance.
It has author
and
mentions
attributes which could be instances of discord.Member
or discord.User
depending on whether the message is sent in a private channel.
The discord.Member
class subclasses the discord.User
and the user id
can be accessed there.
Solution 3:
You could use get_member_named to do something like
@client.command(pass_context = True)asyncdefname_to_id(ctx, *, name):
server = ctx.message.server
user_id = server.get_member_named(name).id
The name can have an optional discriminator argument, e.g. “Jake#0001” or “Jake” will both do the lookup. However the former will give a more precise result.
Solution 4:
prefix_choice = "!"
bot = commands.Bot(max_messages=10000, command_prefix=commands.when_mentioned_or(prefix_choice))
@bot.command()asyncdefmembersLog(ctx):
for i, member inenumerate(ctx.message.server.members):
list_mem_num = (f'{i}')
list_mem_id = (f'{member.id}')
list_mem = (f'{member}')
list_mem_name = (f'{member.name}')
list_all = (f'Number: {list_mem_num} ID: {list_mem_id} Name: {list_mem} ({list_mem_name})\n')
print(list_all)
You can use this to collect all memberinfo of the server where the call comes from. This is the code I use for this.
Post a Comment for "How To How To Convert Username To Discord Id?"