#client.command()
async def hug(ctx, *, user: discord.Member = None):
hug_embed = discord.Embed(title='-w- hug -w-', description='', color=0xafe8fa)
hug_embed.add_field(name=f'{ctx.author.mention} hugged {user.mention}', value='', inline=False)
if user:
await ctx.message.channel.send(embed=hug_embed)
else:
await ctx.send('hug someone ;n;')
I got the error of
In embed.fields.0.value: This field is required
how do i mention another person, with it sending in an embed response
As the error suggests you need to put something in the value of the field. Right now you have '', try with 'something else'
Related
I recently made a ban command for my bot but its not working, can anyone help? whenever i do !ban it just gives the exception and member.kick is blacked out, also i user kick insted of ban because of debugging purposes
Thanks!
Code:
#commands.command()
async def ban(self, ctx, member:nextcord.Member,*, reason="No Reason Provided"):
try:
if reason == None:
embed = nextcord.Embed(title="Member Banned!", description=f"{member.mention} has been banned from '{ctx.guild}'\n Reason: `No Reason Provided`", colour=BLUE_COLOUR)
embed.timestamp = datetime.datetime.utcnow()
embed.set_thumbnail(url="https://cdn.discordapp.com/attachments/942086556980764722/967720168199426069/Ban_Command.gif")
await ctx.reply(embed=embed)
await member.kick(reason=reason)
else:
author = ctx.message.author.name
SendConfirmation = nextcord.Embed(title="Member Banned! ", description=f"Banned: {member.mention}\n Moderator: **{author}** \n Reason: **{reason}**", color=GOLD_COLOUR)
SendDM = nextcord.Embed(title="You Have been Banned", colour=BLUE_COLOUR)
embed.add_field(name="Server Name", value=f"{ctx.guild.name}", inline=True)
embed.add_field(name="Reason", value=f"{reason}", inline=True)
embed.timestamp = datetime.datetime.utcnow()
await member.send(embed=SendDM)
await ctx.reply(embed=SendConfirmation)
except Exception:
ErrorEmbed = nextcord.Embed(title="Something Went Wrong", description="There was an error trying to perform this command.", colour=ERROR_COLOUR)
ErrorEmbed.timestamp = datetime.datetime.utcnow()
await ctx.reply(embed=ErrorEmbed)
return
There were 2 issues with the code:
Your reason argument was never none because it was set by default on "No Reason Provided" so you would never use the if statement
you created the variable SendDM and then used the embed function on a new variable that was never defined (called embed)
#commands.command()
async def ban(self,ctx, member:nextcord.Member,*, reason=None):
if reason == None:
embed = nextcord.Embed(title="Member Banned!", description=f"{member.mention} has been banned from '{ctx.guild}'\n Reason: `No Reason Provided`")
embed.timestamp = datetime.datetime.utcnow()
embed.set_thumbnail(url="https://cdn.discordapp.com/attachments/942086556980764722/967720168199426069/Ban_Command.gif")
await ctx.reply(embed=embed)
await member.ban(reason=reason)
else:
author = ctx.message.author.name
SendConfirmation = nextcord.Embed(title="Member Banned! ", description=f"Banned: {member.mention}\n Moderator: **{author}** \n Reason: **{reason}**")
SendDM = nextcord.Embed(title="You Have been Banned")
SendDM.add_field(name="Server Name", value=f"{ctx.guild.name}", inline=True)
SendDM.add_field(name="Reason", value=f"{reason}", inline=True)
SendDM.timestamp = datetime.datetime.utcnow()
await member.send(embed=SendDM)
await ctx.reply(embed=SendConfirmation)
await member.ban(reason=reason)
This code should work, I tested it on my machine, I took away embed colors and the try and except block but you can add them back.
One last thing, I suggest to remove the try and except block and handle errors with actual error handlers. You can find how to use them in the documentation of the nextcord library.
Code
#bot.command()
async def avatar(ctx, *, avamember : discord.Member=None):
userAvatarUrl = avamember.avatar_url
await ctx.send(userAvatarUrl)
error:
Command raised an exception: AttributeError: 'Member' object has no attribute 'avatar_url'
#bot.command()
async def avatar(ctx, *, member: discord.Member = None):
if not member:
member = ctx.message.author
em = discord.Embed(title=str(member), color=0xAE0808)
em.set_image(url=member.avatar_url)
await ctx.reply(embed=em, mention_author=False)
On responds on your comment: "AttributeError: 'Member' object has no attribute 'avatar_url'"
In my IDE you will see I used "client" rather than "bot", but that does not have anything to do with your code. Besides that, I am using the prefix ">" which can be obviously different for you.
Results:
Code in IDE:
I have made a bot in discord.py which has a kick command. But I want the bot to say "please tell me whom to kick" when someone uses the command without saying whom to kick. I mean if someone uses the kick command without mentioning whom to kick, the bot will say "please tell me whom to kick".
I am trying to make a good bot so please help me.
async def kick(ctx, member : discord.Member, *, reason=None):
if (ctx.message.author.permissions_in(ctx.message.channel).kick_members):
await member.kick(reason=reason)
await ctx.send(f"{member.mention} has successfully been kickded for {reason}")
if not (ctx.message.author.permissions_in(ctx.message.channel).kick_members):
await ctx.send("You don't perms to play football with Araforce and kick them. Sed")
if not user:
await ctx.message.delete()
msg = await ctx.send("You are a bit idiot, don't you know I can't kick anyone if you don't tell whom to kick? Tell me whom to kick and the go away.")
await sleep(4.7)
await msg.delete()
return
if not reason:
await ctx.message.delete()
msg2 = await ctx.send("Hmm.. Why I will kick him? Specify a reason please.")
await sleep(4.7)
await msg2.delete()
return```
async def kick(ctx, user: discord.User = None, *, reason=None):
if not user:
await ctx.message.delete()
msg = await ctx.send("You must specify a user.")
await sleep(4.7)
await msg.delete()
return
if not reason:
await ctx.message.delete()
msg2 = await ctx.send("You must specify a reason.")
await sleep(4.7)
await msg2.delete()
return
I changed ifs to the error handler, which is necessary because if you won't pass one of the arguments (user or reason) you would get an error.
Full code:
#client.command()
async def kick(ctx, member: discord.Member, *, reason): #if you will change "member" and "reason" to something else remember to also change it in error handler
await member.kick(reason=reason)
await ctx.send(f"{member.mention} has successfully been kicked for {reason}")
#kick.error #cool mini error handler
async def kick_error(ctx, error):
if isinstance(error, discord.ext.commands.errors.MissingRequiredArgument):
if str(error) == "member is a required argument that is missing.": #change this if you changed "member"
await ctx.message.delete()
await ctx.send("You are a bit idiot, don't you know I can't kick anyone if you don't tell whom to kick? Tell me whom to kick and then go away.", delete_after=4.7) #you don't have to use "await sleep" you can use "delete_after" parameter
elif str(error) == "reason is a required argument that is missing.": #change this if you changed "reason"
await ctx.message.delete()
await ctx.send("Hmm.. Why I will kick him? Specify a reason please.", delete_after=4.7)
elif isinstance(error, discord.ext.commands.errors.MissingPermissions):
await ctx.send("You don't have perms to play football with Araforce and kick them. Sed")
Discord error handling example
I'm creating a new bot (my third time now, but it's been a while) and I am creating a simple ban command. It is line-for-line the same as my other commands, with the addition of the #commands.has_permissions() decorator and an embed to display the ban. However, after adding some prints to show where it gets to, it doesn't make it past await user.ban().
# ---BAN---
#client.command(name="ban", pass_ctx=True)
#commands.has_permissions(ban_members=True)
async def ban(ctx, user: discord.User=None, *, reason: str=None):
if not user:
await ctx.send("Specify a user")
return
embed = discord.Embed (
color = discord.Color.magenta()
)
embed.add_field(name=f"{user} was banned!", value=f"For reason: {reason}", inline=False)
await user.ban()
await ctx.send(embed=embed)
#ban.error
async def ban_error(ctx, error):
if isinstance(error, BadArgument):
await ctx.send("Please specify a **valid** user!")
No error is thrown, but it only gets to await user.ban() before just stopping. Is my user wrong somehow, or did I make an obvious mistake?
'User' object has no attribute 'ban' , instead you need to pass a member object:
async def ban(ctx, user: discord.Member=None, *, reason: str=None):
And you're not getting any errors because #ban.error is catching them but is only handling the BadArgument exception while the rest are ignored.
I've been programming a discord bot letely, and for its ban command here is the code i use:
#client.command(aliases=['Ban'])
async def ban(ctx, member: discord.Member, days: int = 1):
if get(ctx.author.roles, id=548841535223889923):
await member.ban(delete_message_days=days)
await ctx.send("Banned {}".format(ctx.author))
else:
await ctx.send(ctx.author.mention + " you don't have permission to use this command.".format(ctx.author))
However, if i try to use this on other servers, it just tells me i dont have the required role..
So how do I get the role ID for a new server the bot joins automatically or is there some other way to make this work?
You can use a check for permissions instead of looking for a specific role ID
import discord
from discord.ext import commands
#client.command(aliases=["Ban"])
#commands.has_permission(ban_members=True)
async def...
you can use a has_permission, like this:
#client.command(aliases=["Ban"])
#commands.has_permission(administrator=True)
async def ban(ctx, member: discord.Member, days: int = 1):
await member.ban(delete_message_days=days)
await ctx.send("Banned {}".format(ctx.author))
You can then add a missing permissions error handler:
#client.event
async def on_command_error(ctx, error):
if isinstance(error, commands.MissingPermissions):
await ctx.send("You do not have the permissions required for this command.")
return
raise error