1

Is there anyway to see if a user has permission to run a command without using discord.ext. The code I made was this:

from time import gmtime, strftime
import discord
lockedChannelName = ""
lockedChannel = 0
lockedTime = ""
locker = ""
class MyClient(discord.Client):
    async def on_ready(self):
        print('Logged on as', self.user)
    async def on_message(self, message):
        global lockedChannelName
        global lockedChannel
        global lockedTime
        global locker
        if message.content == "!lockbothere":
            lockedTime = strftime("%H:%M:%S", gmtime())
            lockedChannel = message.channel.id
            lockedChannelName = message.channel.name
            locker = message.author.name
        if message.content == "!unlock":
            lockedChannel = 0
        file = open("logs.txt", "a", encoding="utf-8")
        if message.author != self.user and True if lockedChannel == message.channel.id or lockedChannel == 0 else False:
            file.write(str(message.author) + ": " + message.content + "\n\n")
        file.close()
        if message.content == "!logs":
            file = open("logs.txt", encoding="utf-8")
            if message.channel.id != lockedChannel and lockedChannel != 0:
                await message.channel.send("Note: The logs were locked by " + locker + ". The text channel that was locked was " + lockedChannelName + ", it was locked at " + lockedTime + " GMT")
            if lockedChannel != 0:
                embed = discord.Embed(title="Logs in this channel", description="All Messages In " + lockedChannelName)
            else:
                embed = discord.Embed(title="Logs in this channel", description="All Messages In This Channel")
            embed.add_field(name='The Logs!', value=file.read())
            await message.channel.send(content=None, embed=embed)
            file.close()
        if message.content == "!clearlogs":
            with open("logs.txt", "w", encoding="utf-8"):
                pass
            await message.channel.send("Succsessfully Deleted Logs")
bot = MyClient()
bot.run("lol nope")

What I am going for is to make !clearlogs, !lockbothere, and !unlock only available to admins.

1 Answers1

0

You can get the boolean value of their administrator permission as follows:

administrator = [p for p in message.author.guild_permissions if p[0] == "administrator"][0][1]
if administrator:
    #do some stuff

What's happening there is using list comprehension I'm iterating through the message author's permissions, which are tuples (e.g. ('administrator', True)), and it checks the first element of the tuple - the permission name - and then returns the boolean value if it matches the specified permission name.

Keep in mind that these are guild permissions, and may differ from channel-specific permissions.

To see all the permission names you can search for, try running:

perm_names = [p[0] for p in message.author.guild_permissions]
print(perm_names)
Diggy.
  • 6,744
  • 3
  • 19
  • 38
  • @1BadAtPython No worries, I'm always around if you need anything! Good luck with the bot :) – Diggy. May 12 '20 at 12:25