Profile command issue - python

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.

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.

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

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

Pythion Discord Bot #'ing people

I need when I run a command like /insult #name the bot #'s the person in the argument of the command and sends an image. I can do most of the rest but I can't seem to figure out of to have it #mention the person.
To mention a user in a command, you can use member: discord.Member. This helps you get a member object in the command itself. You can view more on how you can use a discord.Member object here. An example on how to use this in a command can also be found in the docs, view this: Discord Converters.
You can view how these can be incorporated below, including a None variable as a default to avoid errors in your console if the ctx.author does not mention a member.
#client.command() # or bot.command(), or whatever you're using
async def insult(ctx, member:discord.Member=None):
if member == None: # Happens if ctx.author does not mention a member..
member = ctx.author # ..so by default the member will be ctx.author
# You can use member.mention to mention/ ping/ # the person assigned as member
await ctx.send(f"Be insulted {member.mention}!")
# A not as good way to do it would be:
await ctx.send(f"Be insulted <#{member.id}>!")
# both methods work the same way, but member.mention is recommended

Give and remove roles with a bot, Discord.py

How do I make a bot in Discord.py that will assign roles present in a role.json file, while using the same command to both remove and add the same role. For example, ?role <rolename> will both add and remove a role, depending on if the user has the role assigned. I'm a bit confused on how to achieve this.
My current bot uses ?roleadd <rolename> ?roleremove <rolename>.
I'm not sure where your role.json file comes into play, but here's how I would implement such a command
#bot.command(name="role")
async def _role(ctx, role: discord.Role):
if role in ctx.author.roles:
await ctx.author.remove_roles(role)
else:
await ctx.author.add_roles(role)
This uses the Role converter to automatically resolve the role object from its name, id, or mention.
This code basically just checks that if the command raiser is the owner of the server or not and then assigns the specified role to him.
#bot.command()
#commands.is_owner()
async def role(ctx, role:discord.Role):
"""Add a role to someone"""
user = ctx.message.mentions[0]
await user.add_roles(role)
await ctx.send(f"{user.name} has been assigned the role:{role.name}")
Let me break the code down:
#bot.command()
#commands.is_owner()
These are just plain function decorators. They are pretty much self-explanatory. But still let me tell.
#bot.command() just defines that it is a command.
#commands.is_owner() checks that the person who has raised that command is the owner.
async def role(ctx, role:discord.Role): > This line defines the function.
user = ctx.message.mentions[0] #I don't know about this line.
await user.add_roles(role) #This line adds the roles to the user.
await ctx.send(f"{user.name} has been assigned the role:{role.name}")
#It just sends a kind of notification that the role has been assigned

Discord Bot Kick Command

how i can set kick command with a role use just that Moderator role will can use
my kick command :
#client.command(pass_context = True)
async def kick(ctx, userName: discord.User):
"""Kick A User from server"""
await client.kick(userName)
await client.say("__**Successfully User Has Been Kicked!**__")
You can use the commands.has_permissions decorator to ensure the caller has a specific permission.
#client.command(...)
#commands.has_permissions(kick_members=True)
async def kick(ctx, ...):
pass
Just a word of warning though, according to the function docstring, it checks for the user having any required permission instead of all.
It is also recommended to add the bot_has_permissions check too to make sure it can actually kick users too.

Categories

Resources