3
votes

I want to create a Discord bot command.

For example, I would like to create a warning command. So the admin would enter the following command:

?Warn @user "<A warning message>"

The command will then check if the targeted user (@user) has certain roles (for example, "A", "B", "C" and "D"). If he doesn't have the "A" role he will get it, if he has A, he gets B and so on.

The admin must have the "Staff Team" role to use the command.

I tried this but it didn't work:

#Warning
@bot.command(name="Warn", pass_context=True)
@commands.has_role("Staff Team")
async def addrole(ctx,arg):
    user = arg
    if user.role.name != "Warning 1" or "Warning 2" or "Warning 3":
        role = get(user.server.roles, name="Warning 1")
        await bot.add_roles(user, role)
    elif user.role.name == "Warning 1":
        role = get(user.server.roles, name="Warning 2")
        role_last = get(user.server.roles, name="Warning 1")
        await bot.add_roles(user, role)
        await bot.remove_roles(user,role_last)
    elif user.role.name == "Warning 2":
        role = get(user.server.roles, name="Warning 3")
        role_last = get(user.server.roles, name="Warning 2")
        await bot.add_roles(user, role)
        await bot.remove_roles(user,role_last)

Then the bot DMs the user with text and then the message.

1
A user can have several roles, so you can't just check for user.role, but something like role_object in user.roleschluebi
So how I can do that? However how I can ascribe mentioned user to variable? Cant I ascribe users roles to temp list?NORXND
You have a common Python gotcha on line 5 (you have to have complete instructions on both sides of the logical operator)BrainDead
At least somebody will tell me how to assign it mentioned user to variable?NORXND

1 Answers

2
votes

This is anwser:

  1. async def Warn(ctx, args1: discord.Member, args2 = "no reason"):

                      ^                         ^
                 User Mention              Optional
    
  2. Member = args1

  3. Role = ctx.guild.get_role(ROLE ID)

  4. for roles in Member.roles:

  5. if roles != Role: or if roles == Role:

  6. await Member.add_roles(Role)