在 Python Discord 机器人中,在 `on_member_join` 中如何获取成员头像?

huangapple go评论92阅读模式
英文:

How to get avatar in on_member_join using python discord bot?

问题

I want to get avatar of member in on_member_join using python discord bot as we know that it only takes member I can't use ctx so how can I get the avatar image. This is required because I want to make welcome embeds like mee6 or Koya. Can anyone help me to solve this problem

we know ctx is required to get avatar

avatmember = ctx.author
await ctx.send(avatmember.avatar.url)

if we write it as

await channel.send(member.avatar.url)

it shows

await channel.send(member.avatar.url)
                       ^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'url'

My code

intents = discord.Intents.default()
intents.members = True
intents.message_content = True
robota = commands.Bot(command_prefix=".", intents=intents, help_command=None)
clients = discord.Client(intents=intents)

@robota.event
async def on_member_join(member):
    channel = robota.get_channel(Channel_ID)
    embed=discord.Embed(title="Welcome!",description=f"{member.mention} Just Joined")
    embed.set_thumbnail(url=member.avatar.url)
    await channel.send(embed=embed)

在 Python Discord 机器人中,在 `on_member_join` 中如何获取成员头像?

英文:

Sir,

I want to get avatar of member in on_member_join using python discord bot as we know that it only takes member I can't use ctx so how can I get the avatar image. This is required because I want to make welcome embeds like mee6 or Koya. Can anyone help me to solve this problem

we know ctx is required to get avatar
avatmember = ctx.author
await ctx.send(avatmember.avatar.url)

if we write it as

await channel.send(member.avatar.url)
`
it shows

await channel.send(member.avatar.url)
^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'url'

My code

intents = discord.Intents.default()
intents.members = True
intents.message_content = True
robota = commands.Bot(command_prefix=".", intents=intents, help_command=None)
clients = discord.Client(intents=intents)

@robota.event
async def on_member_join(member):
    channel = robota.get_channel(Channel_ID)
    embed=discord.Embed(title="Welcome!",description=f"{member.mention} Just Joined")
    embed.set_thumbnail(url=member.avatar.url)
    await channel.send(embed=embed)

在 Python Discord 机器人中,在 `on_member_join` 中如何获取成员头像?

答案1

得分: 1

你在声明函数时是否传递了member?如果你想创建一个带有成员头像的欢迎消息,可以使用类似以下方式 -

@bot.event
async def on_member_join(member):
    channel = bot.get_channel(WELCOME_CHANNEL_ID)
    await channel.send("欢迎!")

    embed = discord.Embed(title=f"欢迎加入服务器,{member.name}!")
    embed.set_thumbnail(url=member.avatar.url)

    await channel.send(embed=embed)
英文:

Did you pass member when declaring the function?

If you want to create a welcome message with member avatar then you can use something like this -

@bot.event
async def on_member_join(member):
    channel = bot.get_channel(WELCOME_CHANNEL_ID)
    await channel.send("Welcome!")

    embed = discord.Embed(title=f"Welcome to the server, {member.name}!")
    embed.set_thumbnail(url=member.avatar.url)

    await channel.send(embed=embed)

huangapple
  • 本文由 发表于 2023年4月10日 22:42:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75978063.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定