python 如何在www.example.com中使用用户ID获取用户头像discord.py?

tktrz96b  于 2023-01-01  发布在  Python
关注(0)|答案(6)|浏览(105)

我尝试使用下面的代码,但它不工作.

@bot.command()
async def avatar(ctx,*, avamember):
    user = bot.get_user(avamember)
    await ctx.send(f"{user.avatar_url}")

编辑:对于任何有类似问题的人,虽然没有在文档中提到,discord.成员可以从@username中取出用户ID,所以没有任何复杂的方法。

0g0grzrc

0g0grzrc1#

对我不起作用,但这起作用了

@bot.command()
async def userprofile(ctx, member:discord.Member):
    await ctx.send(member.avatar)

我不确定你们怎么样

qzlgjiam

qzlgjiam2#

代码的更优化版本:

@bot.command()
async def get_user_icon(ctx, member:discord.Member):
    await ctx.send(member.avatar_url)
2ul0zpep

2ul0zpep3#

我假设你用@UserNameHere标记用户是不一致的。把这个输入作为成员对象要容易得多:)
你也不需要用引号把url括起来。
如果它在轮齿中,则此代码为:

@commands.command()
async def avatar(self, ctx, *,  avamember : discord.Member=None):
    userAvatarUrl = avamember.avatar_url
    await ctx.send(userAvatarUrl)

如果位于主www.example.com文件中,则此代码为bot.py:

@bot.command()
async def avatar(ctx, *,  avamember : discord.Member=None):
    userAvatarUrl = avamember.avatar_url
    await ctx.send(userAvatarUrl)
tcbh2hod

tcbh2hod4#

if message.content == '!avatar':
    clientProfilePicture = message.author.avatar_url
    await message.channel.send(clientProfilePicture)
ktecyv1j

ktecyv1j5#

你不能只使用avamember作为参数,你需要使用avamember:discord.Member来定义avamember。也许你正在尝试创建一个avatar命令,这会起作用

@bot.command()
  async def avatar(ctx, *, avamember: discord.Member = None):
       if avamember == None:
            embed = discord.Embed(description='❌ Error! Please specify a user',
                                  color=discord.Color.red())
            await ctx.reply(embed=embed, mention_author=False)
        else:
            userAvatarUrl = avamember.avatar_url
            embed = discord.Embed(title=('{}\'s Avatar'.format(avamember.name)), colour=discord.Colour.red())
            embed.set_image(url='{}'.format(userAvatarUrl))
            await ctx.reply(embed=embed, mention_author=False)

如果你用的是Cogs那就用这个

@commands.command()
  async def avatar(self, ctx, *, avamember: discord.Member = None):
       if avamember == None:
            embed = discord.Embed(description='❌ Error! Please specify a user',
                                  color=discord.Color.red())
            await ctx.reply(embed=embed, mention_author=False)
        else:
            userAvatarUrl = avamember.avatar_url
            embed = discord.Embed(title=('{}\'s Avatar'.format(avamember.name)), colour=discord.Colour.red())
            embed.set_image(url='{}'.format(userAvatarUrl))
            await ctx.reply(embed=embed, mention_author=False)
kadbb459

kadbb4596#

import discord
from discord.ext import commands
import os
import random

intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix='>',intents=intents)

@client.event
async def on_ready():
    print("Log : "+str(client.user))

@client.command()
async def avatar(ctx, *, member: discord.Member = None):
    if not member:
        member = ctx.message.author
    userAvatar = member.avatar_url
    em = discord.Embed(color = discord.Color.from_rgb(255, 0, 0), title = "Avatar Link", url=userAvatar)
    em.set_image(url=f"{userAvatar}")
    em.set_author(name=f"{member}", icon_url=f"{userAvatar}")
    em.set_footer(text=f'Requested by {ctx.message.author}', icon_url=f"{ctx.author.avatar_url}")
    await ctx.reply(embed=em)

client.run("token")

相关问题