How to get the avatar url in discord.py - python

I am trying to get the avatar URL using the following code:
import discord
from discord.ext import commands
client = commands.Bot(command_prefix=".")
#client.command()
async def avatar(ctx, *, member : discord.member):
mava = member.avatar_url
print(mava)
client.run(TOKEN)
I get the following error.
discord.ext.commands.errors.MissingRequiredArgument: member is a required argument that is missing.
I have been digging for hours, including watching tutorials, but I have not been able to find a solution.

There are a few things that are needed. You need to change the *, member: discord.member) to member: discord.Member:
#client.command()
async def avatar(ctx, member: discord.member): # No asterik, capitalize discord.Member
mava = member.avatar_url
print(mava)
I would also advise using intents.members as well.
In total, this means, when executing the command, you will do the following:
.avatar #user OR .avatar [user id]

Related

Discord.py move a member without mentioning a user

I want to make i discord bot that can move a member to a specified channel without having to mention them.
import discord
from discord.ext import commands
token = '<token>'
bot = commands.Bot(command_prefix='#')
#bot.command()
async def m(ctx, member : discord.Member, channel : discord.VoiceChannel):
await member.move_to(channel)
#bot.event
async def on_ready():
print('Ready')
bot.run(token)
The command would be: #m #user General
This works great but i need the command to be easier to type by not having to mention a user and simply just moving the author of the message. How do I do that?
You can use ctx.author and make the member argument optional.
Now we just move around the member argument to be last (because a channel is required), and set the default value to None to make it optional.
#bot.command()
#commands.guild_only()
async def m(ctx, channel: discord.VoiceChannel, member: discord.Member = None):
member_to_move = member or ctx.author # if `member` is None, it will be the author instead.
await member_to_move.move_to(channel)
Edit: added #commands.guild_only decorator. This is to ensure that the command can only be used within a guild (and will raise an error if invoked, lets say, in DMs).
You can use ctx.author to get the author of the message:
#bot.command()
#commands.guild_only()
async def m(ctx, channel : discord.VoiceChannel):
await ctx.author.move_to(channel)
So, you can use the command like this: #m General.
Also I added #commands.guild_only() check to be sure that the command is invoked in the guild channel.

Unable to get an addrole command working in Discord.py

i've encountered an issue where I can't seem to get my addrole command to work. I've searched through every video and article and have spent hours to try and figure out why my code isn't working.
I want it so whenever a admin calls the ,addrole command, the user they mentioned gets that role.
(example: ,addrole #{rolename} {#user}).
Here is 2 sections of the code which I think may be the issue.
Here is the imports and some other things.
from discord.ext import commands
import random
from discord import Intents
import os
from discord import Embed
from discord.ext.commands import has_permissions
from discord.utils import get
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv("TOKEN")
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix=",", intents=intents)
Here is the command.
#commands.has_permissions(administrator=True)
async def addrole(ctx, role: discord.Role, user: discord.Member):
await user.add_roles(role)
await ctx.send(f"{user.mention} has successfully received the role {role.mention}.")
I've tried changing so many things but whenever I run it and call the command nothing happens.
Thanks.
Your command is not responding as it is missing client.command() which actually calls the command
#client.command()
#commands.has_permissions(administrator=True)
async def addrole(ctx, role: discord.Role, user: discord.Member):
await user.add_roles(role)
await ctx.send(f"{user.mention} has successfully received the role {role.mention}.")
It may also be better if your allow the role to have multiple arguments, which means you can add a role to a user with spaces, for example, This role instead of Thisrole, this can be added by adding * which creates an multiple string argument. You would then need to pass it as the last argument.
#client.command()
#commands.has_permissions(administrator=True)
async def addrole(ctx, user: discord.Member=None, *, role: discord.Role):
if user == None:
await ctx.send('Please format like ,addrole #member role')
return
await user.add_roles(role)
await ctx.send(f"{user.mention} has successfully received the role {role.mention}.")

How can I kick a user by ID in Discord.py

I was wondering how I could make a command that kicks a specific user every time, without having to mention the user.
I am looking for something like this:
#client.command()
async def kick(ctx):
user = #user id
await user.kick(reason=None)
And not like this:
#client.command()
async def kick(ctx, member : discord.Member, *, reason=None):
await member.kick(reason=reason)
Thanks in advance
You can get a member from his/her id by using discord.Guild.get_member.
#client.command()
async def kick(ctx):
user = ctx.guild.get_member(<user id>)
await user.kick(reason=None)
Or you can use discord.utils.get. I'd recommend you to use the first one but this is also a option.
#client.command()
async def kick(ctx):
user = discord.utils.get(ctx.guild.members, id=<user id>)
await user.kick(reason=None)
References
discord.Guild.get_member()
discord.utils.get()
you can use the built in converter for member : discord.Member
like this:
from discord.ext import commands
#client.command()
async def kick(ctx, member: commands.MemberConverter, *, reason):
await ctx.guild.kick(member, reason=reason)
correct me if i'm wrong but it should work
you can watch this video if you're confused: https://www.youtube.com/watch?v=MX29zp9RKlA (this is where i learned all of my discord.py coding stuff)

How to get the ID of any user given the Username in Discord?

import discord
import os
flist = []
client = discord.Client()
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('?ID'):
for i in ID.split()[1].split('#'):
flist.append(i)
print(flist)
print(discord.utils.get(client.get_all_members(), name = flist[0], discriminator = flist[1]).id)
client.run(TOKEN)
I want to have a bot get the id of a use just by entering the the name and the hashtag number but it returns an error that the final line has nonetype. How could I fix this so it shows the id as a string?
First of all let me recommend you to use the discord.ext.commands extension for discord.py, it will greatly help you to make commands.
You just need to get a discord.Member or discord.User in this case. Since these classes have built in converters, you can use them as typehints to create an instance:
from discord.ext import commands
bot = commands.Bot(command_prefix='?', ...)
#bot.command()
async def showid(ctx, member: discord.Member): # a Union could be used for extra 'coverage'
await ctx.send(f'ID: {member.id}')
Using a typehint in this case allows the command user to invoke the command like:
?showid <id>, ?showid <mention>, ?showid <name#tag>, ?showid <nickname> etc with the same result.
If you don't want to use discord.ext.commands, you can use discord.utils.get as well:
client = discord.Client(...)
#client.event
async def on_message(message):
...
if message.content.startswith('?ID'):
member = discord.utils.get(message.guild.members, name=message.content.split()[1])
await message.channel.send(member.id)
...

discord.py Mention user by name

I am trying to mention a user by their name in 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
#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. The mention on the Member object returns us the string to allow us to mention them directly.
#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.
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.
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
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)

Categories

Resources