F
F
flurixxx2020-07-22 13:00:18
Python
flurixxx, 2020-07-22 13:00:18

Discord.py command disposability?

I wanted to make a !start command that creates the "muted" role on the server, but so that it works 1 time on one server. Here is the code itself:

@bot.command()
@has_permissions(manage_roles=True)
async def start(ctx,name="muted"):
    guild = ctx.guild
    for role in guild.roles:
      if name.lower() not in role.name.lower():
        perms = discord.Permissions(send_messages=False)
        await guild.create_role(name="muted", permissions=perms)
        await ctx.send(embed = discord.Embed(description = '''Первоначальная настройка бота завершена!
Удачного пользования :)''', color = 0x49FF33))
        return role
      else:			
        await ctx.send(embed = discord.Embed(description = 'Бот уже настроен!', color = 0x49FF33))
        return role


The command works and creates a role, but it can be used many times. How to make it disposable?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2020-07-22
@flurixxx

there are many ways.
for example, include a .json file.
after using the !start command, write the server ID and number of uses to this file.
create a condition: if the number of uses = 1, the bot sends some message to the chat

T
Taruu, 2020-07-24
@Taruu

You can create a role when you enter the server using the on_guild_join event, and already when you enter the server, the bot will make its own role (you need protection from the limit of roles on the server)
. Also, if there is a database, then simply enter the settings data on this server.
You can also use a decorator to check. Is there a role on the server. (Check against the role name)
An example from the documentation:

async def is_owner(ctx):
    return ctx.author.id == 316026178463072268

@bot.command(name='eval')
@commands.check(is_owner)
async def _eval(ctx, *, code):
    """A bad example of an eval command"""
    await ctx.send(eval(code))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question