简体   繁体   中英

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'

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. If you don't have that, you need to uninstall discord.py then run

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. 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. No such thing.

I believe you you're looking for the core class's has_permissions method:

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

You will need to define your own error for MissingPermissions

class MissingPermissions(Exception):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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