python discordpy中的加载扩展

python discordpy中的加载扩展,python,discord,discord.py,Python,Discord,Discord.py,我如何解决导入另一个文件的问题,该文件在discordpy中使用了cog import discord from random import randint, choice from discord.ext import commands from discord.utils import get from discord import utils from . import config class CommandErrors(commands.Cog): def __init__(se

我如何解决导入另一个文件的问题,该文件在discordpy中使用了cog

import discord 
from random import randint, choice
from discord.ext import commands
from discord.utils import get
from discord import utils

from . import config

class CommandErrors(commands.Cog):
def __init__(self, client):
    self.client = client

#Error solving
@commands.Cog.listener()
async def on_command_error( self, ctx, error ):
    if isinstance( error, commands.CommandNotFound ):
        await ctx.send( f'{ ctx.author.name } something went wrong, please write command ``.help`` to see all commands of server.' )
    if isinstance( error, commands.errors.CommandInvokeError ):
        await ctx.send( f'{ ctx.author.name } something went wrong, please tell GrizzlyXGod_ to solve the problem.' )

@clear.error
async def clear_error( self, ctx, error ):
    if isinstance( error, commands.MissingRequiredArgument ):
        await ctx.send( f'{ ctx.author.name } please specify the number of messages to clear ``ex: .clear 100``' )
    if isinstance( error, commands.MissingPermissions ):
        await ctx.send( f'{ ctx.author.name } you don''t'' have permission to use this command.' )

@mute.error
async def mute_error( self, ctx, error ):
    if isinstance( error, commands.MissingRequiredArgument ):
        await ctx.send( f'{ ctx.author.name } please type correct ``ex: .mute @name``' )
    if isinstance( error, commands.MissingPermissions ):
        await ctx.send( f'{ ctx.author.name } you don''t'' have permission to use this command.' )
    if isinstance( error, commands.errors.BadArgument ):
        await ctx.send( f'{ ctx.author.name } you wrote something wrong please try again ``ex: .mute @name``' )

@unmute.error
async def unmute_error( self, ctx, error ):
    if isinstance( error, commands.MissingRequiredArgument ):
        await ctx.send( f'{ ctx.author.name } please type correct ``ex: .unmute @name``' )
    if isinstance( error, commands.MissingPermissions ):
        await ctx.send( f'{ ctx.author.name } you don''t'' have permission to use this command.' )
    if isinstance( error, commands.errors.BadArgument ):
        await ctx.send( f'{ ctx.author.name } you wrote something wrong please try again ``ex: .unmute @name``' )

@kick.error
async def kick_error( self, ctx, error ):
    if isinstance( error, commands.MissingRequiredArgument ):
        await ctx.send( f'{ ctx.author.name } please type correct ``ex: .kick @name``' )
    if isinstance( error, commands.MissingPermissions ):
        await ctx.send( f'{ ctx.author.name } you don''t'' have permission to use this command.' )
    if isinstance( error, commands.errors.BadArgument ):
        await ctx.send( f'{ ctx.author.name } you wrote something wrong please try again ``ex: .kick @name``' )

@ban.error
async def ban_error( self, ctx, error ):
    if isinstance( error, commands.MissingRequiredArgument ):
        await ctx.send( f'{ ctx.author.name } please type correct ``ex: .ban @name``' )
    if isinstance( error, commands.MissingPermissions ):
        await ctx.send( f'{ ctx.author.name } you don''t'' have permission to use this command.' )
    if isinstance( error, commands.errors.BadArgument ):
        await ctx.send( f'{ ctx.author.name } you wrote something wrong please try again ``ex: .ban @name``' )

@unban.error
async def unban_error( self, ctx, error ):
    if isinstance( error, commands.MissingRequiredArgument ):
        await ctx.send( f'{ ctx.author.name } please type correct ``ex: .unban @name``' )
    if isinstance( error, commands.MissingPermissions ):
        await ctx.send( f'{ ctx.author.name } you don''t'' have permission to use this command.' )
    if isinstance( error, commands.errors.BadArgument ):
        await ctx.send( f'{ ctx.author.name } you wrote something wrong please try again ``ex: .unban @name``' )

@addrole.error
async def add_role_error( self, ctx, error ):
    if isinstance( error, commands.errors.MissingRequiredArgument ):
        await ctx.send( f'{ ctx.author.name } please type correct ``ex: .addrole @name @role``' )
    if isinstance( error, commands.MissingPermissions ):
        await ctx.send( f'{ ctx.author.name } you don''t'' have permission to use this command.' )
    if isinstance( error, commands.errors.BadArgument ):
        await ctx.send( f'{ ctx.author.name } please type correct ``ex: .addrole @name @role``' )

@removerole.error
async def remove_role_error( self, ctx, error ):
    if isinstance( error, commands.errors.MissingRequiredArgument ):
        await ctx.send( f'{ ctx.author.name } please type correct ``ex: .addrole @name @role``' )
    if isinstance( error, commands.MissingPermissions ):
        await ctx.send( f'{ ctx.author.name } you don''t'' have permission to use this command.' )
    if isinstance( error, commands.errors.BadArgument ):
        await ctx.send( f'{ ctx.author.name } please type correct ``ex: .addrole @name @role``' )

@userinfo.error
async def userinfo_error( self, ctx, error ):
    if isinstance( error, commands.errors.MissingRequiredArgument ):
        await ctx.send( f'{ ctx.author.name } please type correct ``ex: .userinfo @name`` ' )
    if isinstance( error, commands.errors.BadArgument ):
        await ctx.send( f'{ ctx.author.name } something gone wrong make sure that you have wrote the name correctly ``ex: .userinfo @name`` or make sure that user is the part of this server.' )

@commands.Cog.listener()
async def on_command_completion( self, ctx ):
    print(ctx.command.name + " was invoked successfully.")

#Connect
def setup(client):
client.remove_command("help")
client.add_cog(CommandErrors(client))

extensions = ['CommandCommands']
if __name__ == '__main__':
    for ext in extensions:
        client.load_extension(ext)
我尝试导入另一个cog文件,如果您需要该文件中的代码,我可以编辑此问题并添加该部分。非常感谢你的帮助。 我想尽快得到答案! 还有一件事我正在尝试创建bot,有人能告诉我如何编写代码来更改服务器的前缀吗。

在启动bot的主文件中,应该加载扩展名。它应包括:

来自discord.ext导入命令
bot=commands.bot(…)#或client,具体取决于您对它的称呼
#这些元素只是.py文件的名称
cogs=[“事件”、“成员”、“所有者”]
#如果您愿意,也可以像往常一样在此处添加on_ready事件
#确保bot仅在以下情况下加载扩展
#文件直接执行,未导入
如果名称=“\uuuuu main\uuuuuuuu”:
对于齿轮中的齿轮:
加载扩展名(cog)#加载列表中定义的每个文件cog
然后,各个齿轮应遵循此布局(但也可以遵循其他布局):

来自discord.ext导入命令
类CogName(commands.Cog):
def uuu init uuuu(自我,机器人):
self.bot=bot#现在在代码中引用bot时将使用self.bot而不仅仅是bot
#您将使用以下命令代替bot.command():
@commands.command()
async def cmd(self,ctx):#self是第一个参数,因为它在类中
#一些代码
@commands.command()
异步def othercmd(self、ctx、arg1、*、arg2):#例如args正常工作
#其他代码
#这是注册事件的方式,而不是使用@bot.event
@commands.Cog.listener()
_消息上的异步定义(self,message):
#一些代码
#此设置功能需要位于每个cog中,以便机器人能够加载它
def设置(机器人):
bot.add_cog(CogName(bot))

参考文献:

  • -除了查看COG外,本示例还深入了解了如何让服务器配置自己的前缀。考虑一个DB,如JSON、SQLite等
  • -从异步更改为重写
  • -更深入地研究它们,并举例说明它们是如何工作的

在启动bot的主文件中,应该加载扩展名。它应包括:

来自discord.ext导入命令
bot=commands.bot(…)#或client,具体取决于您对它的称呼
#这些元素只是.py文件的名称
cogs=[“事件”、“成员”、“所有者”]
#如果您愿意,也可以像往常一样在此处添加on_ready事件
#确保bot仅在以下情况下加载扩展
#文件直接执行,未导入
如果名称=“\uuuuu main\uuuuuuuu”:
对于齿轮中的齿轮:
加载扩展名(cog)#加载列表中定义的每个文件cog
然后,各个齿轮应遵循此布局(但也可以遵循其他布局):

来自discord.ext导入命令
类CogName(commands.Cog):
def uuu init uuuu(自我,机器人):
self.bot=bot#现在在代码中引用bot时将使用self.bot而不仅仅是bot
#您将使用以下命令代替bot.command():
@commands.command()
async def cmd(self,ctx):#self是第一个参数,因为它在类中
#一些代码
@commands.command()
异步def othercmd(self、ctx、arg1、*、arg2):#例如args正常工作
#其他代码
#这是注册事件的方式,而不是使用@bot.event
@commands.Cog.listener()
_消息上的异步定义(self,message):
#一些代码
#此设置功能需要位于每个cog中,以便机器人能够加载它
def设置(机器人):
bot.add_cog(CogName(bot))

参考文献:

  • -除了查看COG外,本示例还深入了解了如何让服务器配置自己的前缀。考虑一个DB,如JSON、SQLite等
  • -从异步更改为重写
  • -更深入地研究它们,并举例说明它们是如何工作的