How to get bot avatar link - python-3.x

It looks silly question but I want to embed bot avatar so how to do it. Like embed.set_thumbnail(url=user.avatar_url) I tried bot.avatar_url but it's not working.

Assuming you are using bot = discord.Client, Client does not have avatar_url as an attribute. Luckily though, Client does have a user attribute you can access, which means in your case you should be able to use bot.user.avatar_url.
Documentation here: http://discordpy.readthedocs.io/en/latest/api.html#discord.Client.user

Client or i should say bot, itself dont have an attribute of avatar_url cuz its just a discord client, so you need to use bot.user.avatar_url instead
#bot.command()
async def botavatar(ctx):
BotAvatar = bot.user.avatar_url
embed = discord.Embed(
title=f'{bot.user.name}\'s Avatar:',
color=discord.Colour.red())
embed.set_thumbnail(url=f'{BotAvatar}')
await ctx.send(embed=embed)

Actually, Due to recent updates to discord.py, you can get the bot's avatar url from bot.user.avatar.url
So, in a command
#bot.command()
async def avatar(ctx):
await ctx.send("{}".format(self.bot.user.avatar.url))
Hope that helps!

Related

How to send a welcome message in server using discord.py in a server

I would like my bot to send a msg when someone joins the server (also mention them)
#client.event
async def on_member_join(member):
print('welcome!'f'{member}')
the only thing i want to change is this should be displayed in the general channel (its printing it in the terminal now) and that instead of only displaying the person's name i want it to mention them.
Thank you for your help!
to send a message to a channel you need to get the channel and then send the message to there, you can get the channel using get_channel
client.get_channel(1234567890)# change to your channel id
or using discord.utils.get incase you want to use name instead
discord.utils.get(member.guild.channels, name="welcome")
and for mention, discord.py has provided us with member.mention
await channel.send(member.mention)
and combining both things we can make this
#client.event
async def on_member_join(member):
channel = client.get_channel(1234567890) # change to your channel id
await channel.send(f"Welcome {member.mention}!")
print(f'{member.name} joined the server')

Make discord python rewrite bot tag message author

Am updating an old bot made with Discord.py and I can't get it to tag people as it used to do.
if 'test' in message.content:
msg = f'test sucessful {.author}'
await message.channel.send(msg)
On the discord rewrite documentation the only example I found was with {.author}, and it doesnt work. It does print the user's name, like person#1234, but it doesnt tag them.
There are different ways of tagging a user - they are equivalent:
Examples
await message.channel.send(f"Test successful, {message.author.mention}!")
await message.channel.send(f"Test successful, <#{message.author.id}>!")

Change Discord Bot (Python) game or activity

I am trying to repair my Discord Bot and I cannot get any further can someone help me there?
await client.change_presence(activity=discord.Activity(name="<hilfe for help."))
The code should change the activity but it does nothing and just gives me a error.
I don't know what to change to get it working.
Here it is. It have to be discord.Game not discord.Activity
#bot.event
async def on_ready():
await bot.change_presence(activity=discord.Game('bread'))

Discord Bot cannot send messages because send is not an attribute of context

I am re implementing my commands the correct way accordingly to the documentation, using context and command decorators instead of on_message listeners, transferring my commands over is kind of a pain but the documentation has been rather helpful thankful. Unfortunately I've run into an issue which prevents me from sending messages...
Before the move, the way I would send messages was like this
#client.event
async def on_message(message):
if message.author.id in AdminID:
await client.send_message(message.channel. 'message')
Unfortunately this does not work on the new format because there is no message argument to get information from, what you have to do is use is the ctx (context) argument instead which looks something like this according to the documentation
#bot.command()
async def test(ctx, arg):
await ctx.send(arg)
Although the bot recognizes the command and goes there, i cannot send a message because send is not an attribute of ctx, this code is taken strait out of the documentation, am I missing something? Can someone help me figure this out? Thank you
You're looking at the documentation for a different version of the library than the one you're using.
You're using version 0.16, also called the "async" branch. The documentation for that branch is here
You're reading the documentation for the 1.0 version, also called the rewrite branch.
Your command would look something like
#bot.command(pass_context=True)
async def test(ctx):
if ctx.message.author.id in AdminID:
await client.send_message(ctx.message.channel, 'message')

Extract Discord user ID from a discord message

I am working on a python bot, and one thing I need is for the bot to see if someone #'d another person (i.e. I type something like "!callout #randomuser") in a discord channel. If it detects this, I want to get the user ID of the #'d user (in this case, the user ID of #randomuser)
I know this is probably a dumb question, but was unable to find an answer elsewhere. Thanks!
The easier way is to use the converters that come with the discord.ext.commands extension. That can give you a User object, which has a User.id attribute:
from discord.ext.commands import Bot
from discord import User
bot = Bot(command_prefix='!')
#bot.command(pass_context=True)
async def getid(ctx, user: User):
await bot.say("Their ID is {}".format(user.id))
#bot.command(pass_context=True)
async def callout(ctx, user: User):
await bot.say("{}, someone is talking about you".format(user.mention))
bot.run("token")
The simplest way would be using mentions. This generates a list of users, for all users that were mentioned (#user) in a message. The advantage of this is that it easily handles multiple mentions. This works in the rewrite and async version of discord.py
#commands.command()
async def test(ctx):
users = ctx.message.mentions
await ctx.send(users[0].id)

Resources