So today I was tried to learn about python discord.py .
At some point I tried the following thing:
A person tells a bot a command ($DM 'discord user' 'content') and through all the research I had done, I could only find client.author.send("message") where client = discord.Client().
Is there any way to do something like:
user = 'example#0000'
client.user.send("message")
?
There's a few ways of doing this
From user ID
#client.command()
async def DM(ctx, id: int, *, content):
user = client.get_user(id)
await user.send(content)
# Invoking
# $DM 123123123123123 something here
From user name#discriminator
#client.command()
async def DM(ctx, username, *, content):
name, discriminator = username.split("#")
user = discord.utils.get(ctx.guild.members, name=name, discriminator=discriminator)
await user.send(content)
# Invoking
# $DM example#1111 something here
With user mentions, ID's, names, nicknames (best option imo)
#client.command()
async def DM(ctx, member: discord.Member, *, content):
await member.send(content)
# Invoking
# $DM #example#1111 something here
# $DM 1231231231231 something here
# $DM example#1111 something here
Also make sure you enabled intents.members, for more info look at one of my previous answers
PS: You need to use commands.Bot in order for all of this to work, not discord.Client
Related
I want to create a promote command which when issued it promotes the member to the next rank, but I can't get it to remove/add his roles
import discord
import os
from discord.utils import get
bot = discord.Bot(intents=discord.Intents.all())
admin = 990420666568278086
#roles
prospect = 869470046953537546
member = 869470100061814784
patched = 1003733917704134737
#bot.event
async def on_ready():
print(f"{bot.user} is ready and online!")
#bot.slash_command(name="promote", description = "Promote people to the next rank")
async def promote(ctx, user: discord.Member):
if ctx.author.get_role(admin):
if user.get_role(prospect):
await user.remove_roles(prospect)
await user.add_roles(member, patched)
bot.run("DISCORD-TOKEN")
Please refering to discord.Member.add_roles and discord.Member.remove_roles. Both of them are taking discord.Role as parameter. Hence your code should look like user.remove_roles(guild.get_role(prospect)) where guild.get_role(prospect) is returning discord.Role
This is what your code should looks like to make it works. Happy coding!
async def promote(ctx, user: discord.Member):
guild=ctx.guild
if ctx.author.get_role(admin):
if user.get_role(prospect):
await user.remove_roles(guild.get_role(prospect))
await user.add_roles(guild.get_role(member), guild.get_role(patched))
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.
When I try to run the command $getUserId #user using the below code, and try to print it, I get the #mention username as a string and not the user's id:
from discord.ext import commands
client = commands.Bot(command_prefix = "$")
#client.command()
async def getUserId(ctx, *arg):
if not arg:
userId = ctx.author.id
else:
userId = arg.id
await ctx.send(userId)
I could not find good documentation on finding the id from a given mention without using on_message(message). I have had luck using async def getUserId(ctx, user: Discord.user) and importing discord itself, but this causes an error when I just run $getUserId
How would I go about approaching this having: the mentioned user as a parameter and handling all exceptions when the $getUserId command is given
?
you can make something like that :
#bot.command()
async def get_userId(ctx, user: discord.User):
await ctx.send(user.id)
for the firt you need mention your user
or you can get like this
#bot.command()
async def get_userId(ctx, user:discord.User):
user_id = user.id
get_user_id = bot.get_user(user_id)
await ctx.send(get_user_id)
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'm trying to get it so when you type a command it will move me instantly, right now you have to #me on discord
#commands.command(name='movejohan', aliases=['mj', 'MJ'])
async def MoveJ(self, message, member: discord.Member = None):
"""Moves Johan to xxxxx"""
channel = bot.get_channel(xxxxxxxxxx)
await member.move_to(channel, reason='Moved By {}'.format(message.author))
This is what I have and I don't know how to make it so it moves me specifically, I thought to change member: discord.Member = None to member: discord.Member = bot.get_user(xxxxxxxx)
but that did not work, any ideas?
There's nothing wrong with how you're using the member: discord.Member = None argument.
I'd suggest something like this:
#commands.command(name='movejohan', aliases=['mj', 'MJ'])
async def MoveJ(self, message, member: discord.Member = None):
"""Moves Johan to xxxxx"""
if not member:
member = discord.utils.get(message.guild.members, id=xxxxxxxx) # put your user ID here!
# As Fin mentioned, you can also use message.guild.get_member(userID)
channel = self.bot.get_channel(xxxxxxxxxx)
await member.move_to(channel, reason='Moved By {}'.format(message.author))
When you're putting in the member argument, you can use any attribute a member has - you can use their user ID, display name, username, you don't need to explicitly mention them.
You want to specify the guild to get the member from, which can be done using context:
#commands.command(name='movejohan', aliases=['mj', 'MJ'])
async def MoveJ(self, message):
"""Moves Johan to xxxxx"""
channel = self.bot.get_channel(xxxxx)
johan = message.guild.get_member(xxxxx)
await johan.move_to(channel, reason='Moved by {}'.format(message.author))