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))
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.
#bot.command()
async def test(ctx,user : discord.member):
if user==None:
user=ctx.author
test=Image.open("test.jpg")
asset=ctx.author.avatar_url_as(size=128)
data = BytesIO(await asset.read())
pfp=Image.open(data)
pfp=pfp.resize((177,177))
test.paste(pfp,(446,339))
test.save("test1.jpg")
await ctx.send(file=discord.file('test1.jpg'))
its not working and i am getting following errors
if i type !test it would show me this error
discord.ext.commands.errors.MissingRequiredArgument: user is a required argument that is missing.
but if use !test #mention.some.username it will show me this error
Converting to "discord.member" failed for parameter "user".
There are two mistakes that you are doing here.
discord.member is a module, You are meant to use discord.Member class for annotation of user.
You are doing:
if user == None:
user = ctx.author
but you haven't set a default value to user so it is a required argument.
The correct way is:
#bot.command()
async def test(ctx, user: discord.Member = None):
if user is None:
user = ctx.author
# rest of code here
We just declared a default value to the user as None and properly annotate user as discord.Member
just remove user : discord.member and it should work fine
#bot.command()
async def test(ctx):
user=ctx.author
test=Image.open("test.jpg")
asset=ctx.author.avatar_url_as(size=128)
data = BytesIO(await asset.read())
pfp=Image.open(data)
pfp=pfp.resize((177,177))
test.paste(pfp,(446,339))
test.save("test1.jpg")
await ctx.channel.send(file=discord.file('test1.jpg'))
So basically I want to make so the commands parameters will be case insensitive. For example: ?role [member] [role]. So, I don't have to type the full name or the same capitalization of the member and role name. Is it really possible? Because I've tried Dyno bot, and it seems like it's possible. I've tried this code but it doesn't work:
import discord
from discord.ext import commands
client = commands.Bot(command_prefix=("d", "D"), intents=intents, help_command=None, case_insensitive=True)
Like this, I didn't type the member name fully and the role name with the same capitalization.
If it's only for members (like in the image), you can specify the parameter to be a member object, then you can give the name, mention the member or give the id of the member.
An example would be:
#commands.command(name="userinfo")
async def userinfo(self, ctx, Member : discord.Member):
# Do your stuff
Lower the parameters using .lower()
#commands.command()
async def role(self, ctx, Member : discord.Member, Role):
role = Role.lower()
If you want the first letter capitalized then do this:
#commands.command()
async def role(self, ctx, Member : discord.Member, Role):
role = Role.capitalize()
You can simply use discord's converter's
#commands.command()
async def role(self, ctx, member : discord.Member = None, Role: discord.role = None):
if role is None or member is None:
await ctx.send('you need to specify a member and a role')
else:
if role in member.roles():
await member.remove_role(role)
else:
await member.add_role(role)
References:
Converters
Role converter
Member converter
roles
I want to make it so if I type just ?lol it prints '987654321', but if I type ?lol #member and mention someone it prints '123456789'
#commands.command()
async def lol(self, ctx, *, member: discord.Member):
if member:
print('123456789')
else:
print('987654321')
You did almost everything right. However, you need to set discord.Member to None in your command.
Only then the bot will not see member as needed and will automatically output the value for None. If you then mention a member, your defined number will be displayed.
#commands.command()
async def lol(self, ctx, *, member: discord.Member = None):
if member:
print('123456789')
#await ctx.send("Member mentioned") # As you passed in ctx
else:
print('987654321')
#await ctx.send("No member mentioned.")
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