首页 > 解决方案 > Discord 机器人扩展运行良好,但不起作用

问题描述

我能够很好地加载扩展程序,但是每当我在 Discord 上尝试使用 Qball/.Qball/_Qball 时,机器人都不会回答。

Quokkabot.py(主要)

import discord
import random
import os
from discord.ext import commands

client = commands.Bot(command_prefix = '.')

@client.command()
async def load(ctx, extension):
    client.load_extension('.cogs.Quokkabot2')

@client.event
async def on_ready():
    print('{0.user} JOINED THE PARTY!'.format(client))


client.run('token')

Quokkabot2.py (cog)

import discord
from discord.ext import commands

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

@commands.command(aliases=['Qball, test'])
async def _Qball(ctx):
    responses = [#answers]
    await ctx.send(f'{random.choice(responses)}')

def setup(client):
    client.add_command(Qball(client))

标签: pythondiscorddiscord.pybots

解决方案


cog 上的命令应该定义为该 cog 上的实例方法。

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

    @commands.command(aliases=['Qball, test'])
    async def _Qball(self, ctx):
        responses = [#answers]
        await ctx.send(f'{random.choice(responses)}')

然后,在你的主要

client = commands.Bot(command_prefix = '.')
client.add_cog(Commands(client))

推荐阅读