T
T
Telmor2021-09-24 20:19:10
Python
Telmor, 2021-09-24 20:19:10

How to work with discord.py cogs?

I decided to remake my code for cogi. And now I understand how to work with them.
I understood how to make an event or a regular command.
But now I'm interested in:
1) How to make a check for rights in a command
2) How to make a slash (/) command in cogs? And what needs to be changed. (I use dislash for slash commands)
3) How to make an optional argument in slash (/) commands (when this menu is displayed, so that any argument can be left empty and everything would work).
4) How to use json files to store data in them in cogs Code

examples:
1)

@commands.command()
    async def mute(self, ctx, member:discord.Member, time, reason):
        emb = discord.Embed(title='Мут', description=f'{ctx.author.mention}, вы хотите выдать мут {member.mention} по причине {reason} на {time}')
        await ctx.send(embed = emb)


2), 3)
@slash.command(
    guild_ids=test_guilds,
    name="ban",
    description="ban user on this server",
    options=[
        Option("user", "Intruder", Type.USER),
        Option("time", "Mut Time", Type.STRING),
        Option("reason", "Clause of the Rules", Type.STRING),
    ]
)
@slash_commands.has_any_role(890220161943097404)
async def ban(inter,user,time,reason):
....


4)
@commands.Cog.listener()
    async def on_message(self,message):
        if message.author.bot == False:
            with open('warns.json', 'r') as f:
                warn = json.load(f)

            await update_warn(warn, message.author)

            with open('warns.json', 'w') as f:
                json.dump(warn, f)

        await bot.process_commands(message)


    async def update_warn(warn, member):
        if not f'{member.id}' in warn:
            warn[f'{member.id}'] = {}
            warn[f'{member.id}']['warn'] = 0

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question