首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Discord.py权限系统

Discord.py权限系统
EN

Stack Overflow用户
提问于 2020-04-23 17:42:50
回答 5查看 922关注 0票数 1

我正在用discord.py制作一个公共机器人,我想通过sqlite3数据库检查我是用户是管理员还是不是。它返回如下所示的错误:

代码语言:javascript
复制
Traceback (most recent call last):
  File "C:\Users\me\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 83, in wrapped
    ret = await coro(*args, **kwargs)
  File "", line 143, in permission
    isadmin = conn.cursor().execute('''SELECT rp.GuildID, rp.Permission
sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type.

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

Traceback (most recent call last):
  File "C:\Users\me\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\me\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 797, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\me\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 92, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: InterfaceError: Error binding parameter 0 - probably unsupported type.

以下是我的代码的一部分:

代码语言:javascript
复制
    admin = 0
    while admin == 0:
        for y in ctx.message.author.roles:
            print(y)
            isadmin = conn.cursor().execute('''SELECT rp.GuildID, rp.Permission
            FROM rolePermissions AS rp
            WHERE rp.roleID = ? AND rp.GuildID = ? AND rp.Permission = "Admin"''', (y, ctx.message.guild.id))
            print(str(isadmin.fetchall()))
            if str(isadmin.fetchone()) != "None":
                admin = 1
            else:
                admin = 0

    if admin == 1 or ctx.message.author.has_permisions(administrator=True):
        print("User Is Admin")
    else:
        await ctx.channel.send(embed=NoPerms)
        return
EN

回答 5

Stack Overflow用户

发布于 2020-05-04 13:52:07

有一种更简单的方法可以做到这一点。

discord.py有一个内置的@commands.hasrole(role)特性。

代码语言:javascript
复制
import discord
from discord.ext import commands

role = 'rolenamegoeshere'

@bot.command(name='test', help='testing')
@commands.has_role(role)
async def test(ctx):
    #stuff goes here

您绝对不需要使用数据库或其他任何东西,因为discord.py有一种简单的方法可以做到这一点!

票数 3
EN

Stack Overflow用户

发布于 2020-04-23 23:32:38

凭直觉:rolePermissions.roleID是一个整数主键,而y是一个字符串。Sqlite使用动态类型,但有一个例外(来自sqlite FAQ (3)):

integer PRIMARY KEY类型的

列只能包含64位有符号整数。如果您尝试将整数以外的任何内容放入整数主键列中,将会出现错误。

票数 0
EN

Stack Overflow用户

发布于 2020-05-24 10:16:49

我用这种方法解决了这个问题

代码语言:javascript
复制
import discord
from discord.ext import commands


token = ""
client = commands.Bot(command_prefix="|")

@client.event
async def on_ready():
    print("Bot is running")

@client.event
async def on_message(message):
    if message.author == client.user:
        pass
    else:
        manage_messages_permission  = False
        message_len = 0
        while message_len < len(message.author.roles):
            if discord.Permissions(message.author.roles[message_len]._permissions).manage_messages:
                manage_messages_permission = True
                break
            else:
                message_len += 1

        print(manage_messages_permission)

client.run(token)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61384081

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档