繁体   English   中英

Discord Bot Python 3.6 导入命令时出错

[英]Discord Bot Python 3.6 Error importing commands

import discord
from discord.ext.commands import commands,has_permissions, MissingPermissions
import json

with open('reports.json', encoding='utf-8') as f:
  try:
    report = json.load(f)
  except ValueError:
    report = {}
    report['users'] = []

client = discord.ext.commands.Bot(command_prefix = '?')

当我运行这个时,就会出现。 如果我从discord.ext import commands,has_permissions, MissingPermissions那么这就是ImportError: cannot import name 'has_permissions'

Traceback (most recent call last):
  File "F:\Rubayet\Discord Bots\Discord.py\Test.Bot\Test.Bot.py", line 2, in <module>
    from discord.ext.commands import commands,has_permissions, MissingPermissions
ImportError: cannot import name 'commands'

我不知道为什么。 请帮我解决这个问题。

MissingPermissions仅在重写分支中可用。 如果没有,则需要卸载discord.py然后运行

pip install -U git+https://github.com/Rapptz/discord.py@rewrite#egg=discord.py[voice]

然后您可以重新组织您的导入。 要么导入commands并通过该导入引用所有内容,要么导入您单独使用的所有内容。 不要两者都做。

from discord.ext import commands

bot = commands.Bot(command_prefix='!')

@bot.command()
@commands.has_permissions(...)
...

或者

from discord.ext.commands import Bot, has_permissions

bot = Bot(command_prefix='!')

@bot.command()
@has_permissions()
...

我假设您正在使用Rapptz/Discord.py您不能从命令文件夹中导入命令。 没有这样的事。

我相信您正在寻找核心类的has_permissions方法:

from discord.ext import commands
from discord.ext.commands import has_permissions

您需要为MissingPermissions定义自己的错误

class MissingPermissions(Exception):

#and here's a "custom" check example
def has_perms(**perms):
    if has_permissions(perms):
        return True
    else:
        raise MissingPermissions

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM