Converting to "discord.member" failed for parameter "member" - python

#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'))

Related

Profile command issue

I want to make a simple profile command. When there is no mention bot shows your profile, when there is mention, bot shows profile of mentioned user.
#!профиль - профиль
#client.command(passContent=True, aliases=['profile'])
#commands.has_role("🍿║Участники")
async def профиль(ctx, member):
colour=member.colour
if member==None:
профиль_сообщение=discord.Embed(
title=f'Профиль {member.name}',
colour=colour
)
await ctx.send(embed=профиль_сообщение)
else:
return
When I am using command WITHOUT mention I get this:
discord.ext.commands.errors.MissingRequiredArgument: member is a required argument that is missing.
If you want to show your profile when the member parameter's empty, you can give a value to it. For example you can do async def профиль(ctx, member: discord.Member=None):. That means, if user doesn't fill the member parameter, it'll be None. Then in the code, you can add:
#client.command(passContent=True, aliases=['profile'])
#commands.has_role("🍿║Участники")
async def профиль(ctx, member):
colour=member.colour
if member==None:
member = ctx.author
профиль_сообщение=discord.Embed(
title=f'Профиль {member.name}',
colour=colour
)
await ctx.send(embed=профиль_сообщение)
So if the member is None, then it'll be the ctx.author.

How do I make mentioning a member optional within a command?

I've created a code that sends the gif of a hug on command and specifies who it's to, however, I also want to make it optional to mention a member.
the current code is:
#client.command()
async def hug(ctx, member):
username = ctx.message.author.display_name
embed = discord.Embed(title = (f'{username} has sent a hug to {member}!'), description = ('warm, fuzzy and comforting <3'), color = 0x83B5E3)
image = random.choice([(url1), (url2),....(url10)])
embed.set_image(url=image)
await ctx.channel.send(embed=embed)
I want to change it so that if the author uses the command and doesn't mention the member, the command still works, and sends one of the gifs instead. Do I have to create an if statement?
Additionally, if it is possible, how do I change it so that the member's display name is used just like how the author's display name is used?
I've tried doing something like this, but it doesn't work:
#client.command()
async def hug(ctx, member):
username = ctx.message.author.display_name
name = member.display_name
embed = discord.Embed(title = (f'{username} has sent a hug to {name}!'), description = ('warm, fuzzy and comforting <3'), color = 0x83B5E3)
image = random.choice([(url1), (url2),...(url10)])
embed.set_image(url=image)
await ctx.channel.send(embed=embed)
Thank you in advance for any help
You can define your member argument to None by default. If you invoke your command without mentionning anyone, member will have None as value and the if member statement won't be triggered.
Also, by defining member as a Member object in the function's arguments, you'll be able to access the mentionned member's informations.
Here's how you use it :
#client.command()
async def hug(ctx, member: discord.Member = None):
if member:
embed = discord.Embed(title=f'{ctx.author} has sent a hug to {member}!',
description='warm, fuzzy and comforting <3',
color=0x83B5E3)
else:
embed = discord.Embed(color=0x83B5E3)
image = random.choice([(url1), (url2),....(url10)])
embed.set_image(url=image)
await ctx.channel.send(embed=embed)

discord.py how to accept optional arguments

I have made a system that uses JSON files to create a currency system with a discord bot using discord.py
I want users to be able to do +balance to show them how much they have, or +balance in order to check someone else's balance. I tried doing a try: to grab the members' balance and excepting if there was no argument for the member, but I don't know what kind of error it is for that. How do I make it so that the bot will assume if there is no argument that the member they want is ctx.message.author?
if exists('balances.json'):
with open('balances.json', 'r') as file:
balances = json.load(file)
print(balances)
def save():
with open('balances.json', 'w+') as f:
json.dump(balances, f)
#Allows users to check their balance
#bot.command(pass_context=True)
async def balance(ctx, member: discord.Member):
global balances
if str(member.id) not in balances.keys():
await ctx.channel.send(f'{member.name} not registered')
return
if str(member.id) in balances.keys():
requestedUserBalance = balances[str(member.id)]
await ctx.channel.send(f'{member.name} has {requestedUserBalance} LotusTokens')
To give a function a default behavior when an optional variable is not passed, you can give it a default value of None and a behavior when that variable is None.
#bot.command(pass_context=True)
async def balance(ctx, member: discord.Member=None):
if member is None:
member = ctx.message.author
# do stuff

How to move a specific person to a specific VC

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))

Missing permission when trying to remove a role with string variable as the argument in name parameter

I have a function which removes a role from an user for a certain time. It works fine with predefined role names like:
role_to_remove = discord.utils.get(ctx.guild.roles, name="placeholder")
But it doesn't work when I'm setting roles using commands, which save the user input in string variable.
Here is the function which removes roles:
role1_name = ''
role2_name = ''
roleDefault_name = ''
#client.command()
#commands.has_role('VIP')
async def deleterole(ctx, member: discord.Member, *, reason=None):
role_to_remove = roleDefault_name
for role in member.roles:
if role == role2_name:
role_to_remove = discord.utils.get(ctx.guild.roles, name=role2_name)
elif role == role1_name:
role_to_remove = discord.utils.get(ctx.guild.roles, name=role1_name)
else:
await ctx.send(f"None of the roles specified in config are assigned to {member}")
role_to_remove = discord.utils.get(ctx.guild.roles, name=roleDefault_name)
await member.remove_roles(role_to_remove)
await asyncio.sleep(60.0)
await member.add_roles(role_to_remove)
These are functions which are used to set role names by user:
#client.command()
#commands.has_permissions(administrator=True)
async def set_role1_name(ctx, role1):
global role1_name
role1_name = role1
#client.command()
#commands.has_permissions(administrator=True)
async def set_role2_name(ctx, role2):
global role2_name
role2_name = role2
#client.command()
#commands.has_permissions(administrator=True)
async def set_role_default(ctx, roledefault):
global roleDefault_name
roleDefault_name = roledefault
When I use the deleterole command, I get the following error (despite the fact that the bot has administrator permissions):
discord.errors.Forbidden: 403 Forbidden (error code: 50013): Missing Permissions
Are you sure you are actually getting the correct role to remove and not None?
discord.utils.get returns None when it does not find a role with the specified name and trying to remove a role that does not exist might trigger a 403 Forbidden.

Categories

Resources