0

When I try to run the command I always get this error message: SyntaxError: 'await' outside function Very simple mistake probably, but I really don't see the mistake in it. Can anyone help?

import discord
from discord.ext import commands

class unban(commands.Cog):

    def __init__(self, client):
        self.client = client

# Commandok

@commands.command()
async def unban(ctx, *, member): # unindent
    banned_users = await ctx.guild.bans() # unindent
    member_name, member_discriminator = member.split('#') # unindent

    for ban_entry in banned_users:
        user = ban_entry.user

    if (user.name, user.discriminator) == (member_name, member_discriminator):
        await ctx.guild.unban(user)
        await ctx.send(f'Unbanned {user.mention}')
    return

def setup(client):
    client.add_cog(unban(client))

discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.unban' raised an error: TypeError: cogs must derive from Cog sys:1: RuntimeWarning: coroutine 'Command.call' was never awaited

  • The error message in the question text is not the same as the one in the title. It looks like you edited the question but forgot to change the title to match. – Barmar Dec 27 '22 at 23:47

2 Answers2

0

I think you might have indented your function definition line too much.

@commands.command()
async def unban(ctx, *, member): # unindent
    banned_users = await ctx.guild.bans() # unindent
    member_name, member_discriminator = member.split('#') # unindent

    for ban_entry in banned_users:
        user = ban_entry.user

    if (user.name, user.discriminator) == (member_name, member_discriminator):
        await ctx.guild.unban(user)
        await ctx.send(f'Unbanned {user.mention}')
    return
ofido
  • 78
  • 2
  • 5
  • I just received this error message: discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.unban' raised an error: TypeError: cogs must derive from Cog sys:1: RuntimeWarning: coroutine 'Command.__call__' was never awaited –  Mar 30 '21 at 13:00
  • I have no clue about that code as you never mentioned it... Please supply it as well, did you try adding `await` at the start of that line? – ofido Mar 30 '21 at 13:15
  • I do not understand. I edited and write the full script. –  Mar 30 '21 at 13:31
0

Your if statement isn't in your for loop. This should work:

@commands.command()
async def unban(ctx, *, member):
    banned_users = await ctx.guild.bans()
    member_name, member_discrinimator = member.split('#')

    for ban_entry in banned_users:
        user = ban_entry.user

        if (user.name, user.discriminator) == (member_name, member_discrinimator):
            await ctx.guild.unban(user)
GGberry
  • 929
  • 5
  • 21
  • It doesn't work, it writes a bug that: `discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.unban' raised an error: TypeError: cogs must derive from Cog sys:1: RuntimeWarning: coroutine 'Command.__call__' was never awaited` –  Mar 30 '21 at 15:34