Cog 正在加载但命令不起作用

时间:2021-03-09 03:52:45

标签: python-3.x discord.py

我一直在阅读文档和以前的 Stack Overflow 问题,但似乎没有一个解决方案适合我。 我的齿轮在启动时加载良好,但是当我使用带有 .8ball [问题] 的命令时没有任何反应,甚至没有错误。关于我做错了什么的任何想法?

import discord
from discord.ext import commands
from discord.utils import get
import random

class Fun(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command(name = "8ball")
    async def _8ball(ctx , * , question):
        startbool = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21]
        positiveResponses = ["It is certain." , "It is decidedly so." , "Without a doubt." , "Yes - definitely." , "You may rely on it." , "As I see it, yes." , "Most likely." , "Outlook good." , "Yes." ,
        "Signs point to yes."]
        negativeResponses = ["Don't count on it." , "My reply is no." , "My sources say no." , "Outlook not so good." , "Very doubtful."]

        result = random.choice(startbool)

        if result <= 10:
            embed = discord.Embed(colour = (discord.Colour.red()), title = 'Answer', description = f'Question: {question}\nAnswer: {random.choice(negativeResponses)}')

        if result >= 11:
            embed = discord.Embed(colour = (discord.Colour.green()), title = 'Answer', description = f'Question: {question}\nAnswer: {random.choice(positiveResponses)}')

        embed.set_thumbnail(url = "https://media4.giphy.com/media/l1IBiAKtFRpR9v9O8/giphy.gif")
        await ctx.send(embed = embed)
        member = ctx.message.author
        time = datetime.now()
        command = ctx.message.content
        print(f'{member} used {command} at {time}')



def setup(bot):
    bot.add_cog(Fun(bot))

2 个答案:

答案 0 :(得分:0)

正如 Łukasz Kwieciński 所说,由于您通过创建类从 commands.Cog 继承,因此方法定义应包含 self 作为第一个参数(在 ctx 之前),就像在您的 {{1} }} 方法。这是更正的方法

__init__

答案 1 :(得分:0)

这是行不通的,因为对于 cogs,您必须首先指定 'self',因为它是在类内部,您的代码应该如下所示 进口不和谐 from discord.ext 导入命令 从 discord.utils 导入获取 随机导入

class Fun(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command(name = "8ball")
    async def 8ball(self, ctx , * , question):
        startbool = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 15 , 16 , 17 , 18 , 19 , 20 , 21]
        positiveResponses = ["It is certain." , "It is decidedly so." , "Without a doubt." , "Yes - definitely." , "You may rely on it." , "As I see it, yes." , "Most likely." , "Outlook good." , "Yes." ,
        "Signs point to yes."]
        negativeResponses = ["Don't count on it." , "My reply is no." , "My sources say no." , "Outlook not so good." , "Very doubtful."]

        result = random.choice(startbool)

        if result <= 10:
            embed = discord.Embed(colour = (discord.Colour.red()), title = 'Answer', description = f'Question: {question}\nAnswer: {random.choice(negativeResponses)}')

        if result >= 11:
            embed = discord.Embed(colour = (discord.Colour.green()), title = 'Answer', description = f'Question: {question}\nAnswer: {random.choice(positiveResponses)}')

        embed.set_thumbnail(url = "https://media4.giphy.com/media/l1IBiAKtFRpR9v9O8/giphy.gif")
        await ctx.send(embed = embed)
        print(f'{ctx.message.author} used {ctx.message.content} at {datetime.now()}')



def setup(bot):
    bot.add_cog(Fun(bot))