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)
Related
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.
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}.")
I start creating a Discord bot with the Python API of Discord. So, I tried to make a command that give roles on a discord server. Here is my code :
client = commands.Bot(command_prefix = '/')
#client.command()
async def addrole(ctx, role: discord.Role, user: discord.Member):
if ctx.author.guild_permissions.administrator:
await user.add_roles(role)
await ctx.send(f"Successfully given {role.mention} to {user.mention}.")
When I try to use the command, any error happear. I try to make like the websearch I've done but still don't work. Can someone help me ?
I don't believe basic things like these would require Discords intents, especially the "member intent". What I have done is I've cleaned up your code and required a member to be passed before a multiple string role, if needed.
#client.command()
#commands.has_permissions(manage_roles=True)
async def addrole(ctx, member: discord.Member = None, *, role: discord.Role = None):
if role == None:
await ctx.send(f'Provide a role to add')
return
if member == None:
await ctx.send(f'Provide a member to add a role')
return
await member.add_roles(role)
await ctx.send(f"Successfully added role, {role} to {member.name}")
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)
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]