Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Discord bot check if user is admin

Hi i'm new to python coding with discord and I have tried to make a command that tells the user if they are a admin or not but well... its not working in the slightest

    @client.command(name="whoami",description="who are you?")
async def whoami():
    if message.author == client.user:
        return
    if context.message.author.mention == discord.Permissions.administrator:
        msg = "You're an admin {0.author.mention}".format(message)  
        await client.send_message(message.channel, msg)
    else:
        msg = "You're an average joe {0.author.mention}".format(message)  
        await client.send_message(message.channel, msg)

I then get this when I try to type whoami

Ignoring exception in command whoami
Traceback (most recent call last):
  File "/home/python/.local/lib/python3.6/site-packages/discord/ext/commands/core.py", line 50, in wrapped
    ret = yield from coro(*args, **kwargs)
  File "<stdin>", line 3, in whoami
NameError: name 'message' is not defined

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/python/.local/lib/python3.6/site-packages/discord/ext/commands/bot.py", line 846, in process_commands
    yield from command.invoke(ctx)
  File "/home/python/.local/lib/python3.6/site-packages/discord/ext/commands/core.py", line 374, in invoke
    yield from injected(*ctx.args, **ctx.kwargs)
  File "/home/python/.local/lib/python3.6/site-packages/discord/ext/commands/core.py", line 54, in wrapped
    raise CommandInvokeError(e) from e
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NameError: name 'message' is not defined
like image 226
Finlay Metcalfe Avatar asked Jul 09 '18 08:07

Finlay Metcalfe


3 Answers

If message.author.server_permissions.administrator doesn't work.

Change it to message.author.guild_permissions.administrator

Or try message.author.top_role.permissions.administrator, this will return you a bool.

One thing is, normally the server owner sets the administrator to the server top role, so this will work most of the time. But if they don't, the third sol won't work.

like image 185
Kouheng Avatar answered Sep 24 '22 17:09

Kouheng


You can use the has_permissions check to see if a user has the administrator privilege.

We can then handle the error that failing that check will throw in order to send a failure message.

from discord.ext.commands import Bot, has_permissions, CheckFailure

client = Bot("!")

@client.command(pass_context=True)
@has_permissions(administrator=True)
async def whoami(ctx):
    msg = "You're an admin {}".format(ctx.message.author.mention)  
    await ctx.send(msg)

@whoami.error
async def whoami_error(ctx, error):
    if isinstance(error, CheckFailure):
        msg = "You're an average joe {}".format(ctx.message.author.mention)  
        await ctx.send(msg)
like image 32
Patrick Haugh Avatar answered Sep 26 '22 17:09

Patrick Haugh


Change

@client.command(name="whoami",description="who are you?")
async def whoami():

to

@client.command(pass_context=True)
async def whoami(ctx):

Then you can use ctx to get all kinds of stuff like the user that wrote it, the message contents and so on

To see if a User is an administrator do
if ctx.message.author.server_permissions.administrator: which should return True if the user is an an Administator

Your code should look like this:

import discord
import asyncio
from discord.ext.commands import Bot

client = Bot(description="My Cool Bot", command_prefix="!", pm_help = False, )
@client.event
async def on_ready():
  print("Bot is ready!")
  return await client.change_presence(game=discord.Game(name='My bot'))

@client.command(pass_context = True)
async def whoami(ctx):
    if ctx.message.author.server_permissions.administrator:
        msg = "You're an admin {0.author.mention}".format(ctx.message)  
        await client.send_message(ctx.message.channel, msg)
    else:
        msg = "You're an average joe {0.author.mention}".format(ctx.message)  
        await client.send_message(ctx.message.channel, msg)
client.run('Your_Bot_Token')
like image 22
Tristo Avatar answered Sep 24 '22 17:09

Tristo