0

I am trying to implement a check for bot having manage_nicknames permission and if it doesn't have it it should spit out a embed

@commands.command(pass_context=True ,description='Set a nickname for a person', aliases=['name', 'set_name', 'prozvische'])
    async def setname(self, ctx: SlashContext, member: discord.Member, *, nickname=None):
       try:
          '''
          Change user's nickname
          '''
          await member.edit(nick=nickname)
          await ctx.message.delete()
          
       if ctx.message.guild.me.permissions_in(ctx.message.channel).manage_nicknames is False:
          embed=discord.Embed(title=" Error", description="I need the ``Manage Nicknames`` permission to do this.", color=0xdd2e44,)
          await ctx.send(embed=embed)

here is the code piece for the command and it spits out this error ExtensionFailed: Extension 'listener.konsolemod.fun' raised an error: SyntaxError: invalid syntax (fun.py, line 27)

2 Answers2

0

If you're doing this:

embed=discord.Embed(title=" Error", description="I need the ``Manage Nicknames`` permission to do this.", color=0xdd2e44,)

then you need to remove the comma after the color, because you're not declaring any values after that.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
athalon
  • 11
  • 2
0

In python, you must end a try clause with an except clause.

In your case, I suggest:

EAFP

from discord.ext.commands import MissingPermissions

@commands.command(pass_context=True, description='Set a nickname for a person',
                  aliases=['name', 'set_name', 'prozvische'])
async def setname(self, ctx: SlashContext, member: discord.Member, *, nickname=None):
    '''
    Change user's nickname
    '''
    try:
        await member.edit(nick=nickname)
        await ctx.message.delete()
    except MissingPermissions:
        embed = discord.Embed(title=" Error", description="I need the ``Manage Nicknames`` permission to do this.",
                              color=0xdd2e44, )
        await ctx.send(embed=embed)

LBYL

@commands.command(pass_context=True, description='Set a nickname for a person',
                  aliases=['name', 'set_name', 'prozvische'])
async def setname(self, ctx: SlashContext, member: discord.Member, *, nickname=None):
    '''
    Change user's nickname
    '''
    if ctx.message.guild.me.permissions_in(ctx.message.channel).manage_nicknames is False:
        embed = discord.Embed(title=" Error", description="I need the ``Manage Nicknames`` permission to do this.",
                              color=0xdd2e44, )
        await ctx.send(embed=embed)
        return
    await member.edit(nick=nickname)
    await ctx.message.delete()

What is the EAFP and LBYL

Dorian Turba
  • 3,260
  • 3
  • 23
  • 67