简体   繁体   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 = '?')

When I run this this comes.当我运行这个时,就会出现。 if I give from discord.ext import commands,has_permissions, MissingPermissions Then this come ImportError: cannot import name 'has_permissions'如果我从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'

I dont know know why.我不知道为什么。 Plz help me out with this.请帮我解决这个问题。

MissingPermissions is available only in the rewrite branch. MissingPermissions仅在重写分支中可用。 If you don't have that, you need to uninstall discord.py then run如果没有,则需要卸载discord.py然后运行

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

Then you can reorganize your imports.然后您可以重新组织您的导入。 Either import commands and reference everything through that import, or import everything you use individually.要么导入commands并通过该导入引用所有内容,要么导入您单独使用的所有内容。 Don't do both.不要两者都做。

from discord.ext import commands

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

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

or或者

from discord.ext.commands import Bot, has_permissions

bot = Bot(command_prefix='!')

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

I'm assuming you're using Rapptz/Discord.py You cannot import commands from commands folder.我假设您正在使用Rapptz/Discord.py您不能从命令文件夹中导入命令。 No such thing.没有这样的事。

I believe you you're looking for the core class's has_permissions method:我相信您正在寻找核心类的has_permissions方法:

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

You will need to define your own error for MissingPermissions您需要为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