0
votes

I'm making an Administration cog for my discord bot and my code wouldn't identify 'ctx'. PyCharm suggested to replace 'ctx' with 'self' and I have no idea what 'self' does. And from what PyCharm is saying, There are millions of other stuff which I have to write down what it is. PyCharm couldn't identify guild, send, author and channel and it also says that return ctx.author.guild_permissions.manage_messages is an unreachable code. As a note if this seems to be a really stupid question, I am a beginner who started 2 weeks ago.

As for the code:

class Administration(commands.Cog):
    def __init__(self, client):
        self.client = client

    @commands.Cog.listener()
    async def on_ready(self):
        print("Admin cog ready")

    async def cog_check(self, ctx):
        admin = get(ctx.guild.roles, name="Admin")
        return admin in ctx.author.roles

        return ctx.author.guild_permissions.manage_messages

    @commands.command(aliases=["purge"])
    async def clear(ctx, amount=3):
        """Clears 3 messages"""
        await ctx.channel.purge(limit=amount)

    @commands.command(pass_context=True)
    async def giverole(ctx, user: discord.Member, role: discord.Role):
        """Gives a role to a user"""
        await user.add_roles(role)
        await ctx.send(f"hey {ctx.author.name}, {user.name} has been giving a role called: {role.name}")

    @commands.command(aliases=['make_role'])
    @commands.has_permissions(manage_roles=True)
    async def create_role(ctx, *, name):
        """Creates a role"""
        guild = ctx.guild
        await guild.create_role(name=name)
        await ctx.send(f'Role `{name}` has been created')


    @commands.command(name="slap", aliases=["warn"])
    async def slap(ctx, members: commands.Greedy[discord.Member], *, reason='no reason'):
        """Warns someone"""
        slapped = ", ".join(x.name for x in members)
        await ctx.send('{} just got slapped for {}'.format(slapped, reason))


def setup(client):
    client.add_cog(Administration(client))
1

1 Answers

0
votes

In classes, (unless it's a staticmethod or classmethod) you always pass self as the first argument.

@commands.command(aliases=["purge"])
async def clear(self, ctx, amount=3): # Note how I put `self` as the first arg, do the same in all commands in the cog
    """Clears 3 messages"""
    await ctx.channel.purge(limit=amount)

Also, this will never work

async def cog_check(self, ctx):
    admin = get(ctx.guild.roles, name="Admin")
    return admin in ctx.author.roles

    return ctx.author.guild_permissions.manage_messages

The function will end no matter what when it reaches the first return, you can simply use the AND or OR logical operator if you want to also evaluate the second return statement

async def cog_check(self, ctx):
    admin = get(ctx.guild.roles, name="Admin")
    return admin in ctx.author.roles and/or ctx.author.guild_permissions.manage_messages