查看用户是否具有权限而没有discord.ext

时间:2020-05-12 09:49:45

标签: python discord.py

是否仍然可以查看用户是否有权在不使用discord.ext的情况下运行命令。我编写的代码是这样的:

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")

我要做的是使!clearlogs,!lockbothere和!unlock仅对管理员可用。

1 个答案:

答案 0 :(得分:0)

您可以获取其管理员权限的布尔值,如下所示:

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

使用list comprehension时发生了什么事我正在遍历消息作者的权限,即元组(例如('administrator', True)),并检查该元组的第一个元素-权限名称-和然后,如果它与指定的权限名称匹配,则返回布尔值。

请记住,这些是行会权限,可能与channel-specific permissions不同。

要查看您可以搜索的所有权限名称,请尝试运行:

perm_names = [p[0] for p in message.author.guild_permissions]
print(perm_names)