I'm trying to make a command who shows the specified user's profile picture. no errors it just doesn't work.
code:
#client.command()
async def pfp(ctx, *,member : discord.Member):
embed = discord.Embed(colour = random.choice(mycolor))
mylist1 = ["What a creep",
"stalker"]
embed.set_author(name=f"{random.choice(mylist1)}")
embed.set_image(url=ctx.member.avatar_url)
await ctx.send(embed=embed)
Your error might come from this line :
embed.set_image(url=ctx.member.avatar_url)
Context objects don't have any question attribute. If you want the command author's profile picture :
async def pfp(ctx, *, member: discord.Member):
(...)
embed.set_image(url=member.avatar_url)
(...)
Also, typing question = discord.Member is useless, it would just asign the Member class to your question variable.
I've also noticed that mycolor seems undefined.
Well, I'm assuming that it just doesn't send a message. I think that you want #client.command(pass_context=True, aliases=[]) instead of #client.command(). pass_context makes it assign ctx.
The code should be like this.
#client.command(pass_context=True)
async def pfp(ctx, *,member : discord.Member):
embed = discord.Embed(colour = discord.Red())
embed.set_image(url='{}'.format(member.avatar_url))
await ctx.send(embed=embed
Related
I'm making a discord bot with Python and I want to send an embed when the use use the command ]kill .So when I tag someone and use the command the output is not the member name.
I need to make it the tagged user's name.
This is my code.
killgifs = ['https://c.tenor.com/1dtHuFICZF4AAAAC/kill-smack.gif' , 'https://c.tenor.com/gQAWuiZnbZ4AAAAC/pokemon-anime.gif' , 'https://c.tenor.com/PJbU0yjG3BUAAAAd/anime-girl.gif' , 'https://c.tenor.com/Re9dglY0sCwAAAAC/anime-wasted.gif']
#bot.command(name='kill')
async def kill (ctx,person) :
author = ctx.author
embed = discord.Embed (color=discord.Color.red())
embed.set_author(name=f'{author} kills {person}')
embed.set_image(url = (random.choice(killgifs)))
await ctx.send(embed=embed)
You can work with discord.Member here since you can't mention a person in an embed title/author field. Simply change your code to the following:
killgifs = ['https://c.tenor.com/1dtHuFICZF4AAAAC/kill-smack.gif',
'https://c.tenor.com/gQAWuiZnbZ4AAAAC/pokemon-anime.gif',
'https://c.tenor.com/PJbU0yjG3BUAAAAd/anime-girl.gif',
'https://c.tenor.com/Re9dglY0sCwAAAAC/anime-wasted.gif']
#bot.command(name='kill')
async def kill(ctx, person: discord.Member): # Make the person a discord.Member
author = ctx.author
embed = discord.Embed(color=discord.Color.red())
embed.set_author(name=f'{author} kills {person.display_name}') # Display the name
embed.set_image(url=(random.choice(killgifs)))
await ctx.send(embed=embed)
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.
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)
I have been getting into coding my very own discord bot using Discord.py in Thonny, using python version 3.7.6. I want to have an embed when a certain command is typed (!submit) to have the users name as the title and the content of the message as the description. I am fine having !submit ' ' in my embed but if there is any way of taking that out and only having the content of the message minus the !submit i would highly appreciate it. Right now with my code i am getting two errors, one is that the client.user.name is the name of the bot (submit bot) and not the author (old code), and i am getting this message 'Command raised an exception: TypeError: Object of type Member is not JSON serializable' with my new code(below), if anyone could offer insight please reply with the appropriate fixes!
client = commands.Bot(command_prefix = '!')
channel = client.get_channel(707110628254285835)
#client.event
async def on_ready():
print ("bot online")
#client.command()
async def submit(message):
embed = discord.Embed(
title = message.message.author,
description = message.message.content,
colour = discord.Colour.dark_purple()
)
channel = client.get_channel(707110628254285835)
await channel.send(embed=embed)
client.run('TOKEN')
client = commands.Bot(command_prefix = '!')
#got rid of the old channel variable here
#client.event
async def on_ready():
print ("bot online")
#client.command()
async def submit(ctx, *, message): #the `*` makes it so that the message can include spaces
embed = discord.Embed(
title = ctx.author, #author is an instance of the context class
description = message, #No need to get the content of a message object
colour = discord.Colour.dark_purple()
)
channel = client.get_channel(707110628254285835)
await channel.send(embed=embed)
client.run('TOKEN')
Problems:
1. channel is defined twice,
2. function commands in discord.py take an implicit context argument, usually called ctx.
Overall, it looks like you aren't understand the underlying OOP concepts that discord.py has to offer. It might be helpful to refresh your memory with an online class or article.
You're pretty close...
The following items should help:
Working with commands you generally define the context of the command as "ctx", which is the first and sometime only argument. "message" is generally used on message events like async def on_message(message):.
To break out the message from the command itself you add arguments to the function definition.
To get the name of the user you need to cast to a string to avoid the TypeError.
To send the message back in the same channel as the !submit was entered, you can use await ctx.send(embed=embed)
Try:
#client.command()
async def submit(ctx, *, extra=None):
embed = discord.Embed(
title=str(ctx.author.display_name),
description=extra,
colour=discord.Colour.dark_purple()
)
await ctx.send(embed=embed)
Result:
Say I want to make a bot with a "poke" feature (aka sends a pm to a user saying "Boop" when someone says "!poke #user#0000"), how would I do this? It works perfectly when I do this:
#bot.command(pass_context=True)
async def poke(ctx, message):
await client.send_message(ctx.message.author, 'boop')
but only if I want to poke the author of the message. I want to poke whoever's being #'d.
I know the discord.py documents say I can use this:
start_private_message(user)
but I don't know what to put in place of user.
It's actually simpler than that
#bot.command(pass_context=True)
async def poke(ctx, member: discord.Member):
await bot.send_message(member, 'boop')
send_message contains logic for private messages, so you don't have to use start_private_message yourself. The : discord.Member is called a converter, and is described in the documentation here
I might be necroing this post a bit, but the recent version of this would look like this:
#bot.command():
async def poke(ctx, user: discord.Member=None):
if user is None:
await ctx.send("Incorrect Syntax:\nUsage: `!poke [user]`")
await user.send("boop")
The if user is None: block is optional, but is useful if you want to have an error message if a command isn't used correctly.