Discord Bot Python 3.6导入命令时出错

时间:2018-10-10 10:33:20

标签: python python-3.x discord discord.py

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'

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

2 个答案:

答案 0 :(得分:1)

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()
...

答案 1 :(得分:0)

我假设您正在使用Rapptz/Discord.py,但无法从commands文件夹导入命令。没有这样的事情。

我相信您正在寻找core类的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