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'
我不知道为什么。请帮我解决这个问题。
答案 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