首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >发布实现组(invoked_subcommand总是返回None )

发布实现组(invoked_subcommand总是返回None )
EN

Stack Overflow用户
提问于 2018-03-23 12:01:13
回答 1查看 297关注 0票数 0

我的(相当可怕,尽管希望这不会是这里的问题)在运行Discord.py的不和谐机器人上的quotebot齿轮代码遇到了一些问题。我在过去尝试实现组时遇到了很多问题,之前我总是放弃了,但这一次我想做正确的事情,因为代码已经变得笨拙,否则就不能这么做了。但问题是,我真的不知道我在做什么。到目前为止我的代码如下:

代码语言:javascript
复制
import discord
import asyncio
from discord.ext import commands
import json
from . import utils
import random
import math

"""Module containing all you need for a quote bot"""

data = utils.load_json("quotes")

maxID = data["maxID"]

temp = data["quotes"]
quotes = []

curPage = 0

class Quote:
    def __init__(self, id, msg):
        self.id = id
        self.msg = msg

for i in range(0, len(temp[0])):
    quotes.append(Quote(temp[1][i], temp[0][i]))

class Quotes():

    def __init__(self, bot):
        self.bot = bot

    #All the "Quote" commands
    @commands.group(pass_context=True)
    async def quote(self, ctx, arg):
        print(ctx.message.content)
        if ctx.invoked_subcommand == None:
            global quotes
            global maxID

            try:
                if int(arg) <= maxID and int(arg) >= 1:
                    b = None
                    for s in quotes:
                        if int(arg) - 1 == s.id:
                            b = s
                            break

                    if (b == None):
                        await ctx.channel.send('No quote with this ID')
                        return
                    await ctx.channel.send('`Quote ' + str(int(arg)) +':` ' + str(b.msg))
                else:
                    await ctx.channel.send('No quote with this ID')
            except:
                await ctx.channel.send('Invalid input')

    @quote.command(pass_context=True)
    async def add(self, ctx, *args):
        global maxID
        global quotes

        quotes.append(Quote(maxID, s))
        maxID += 1
        await ctx.channel.send('`Added Quote ID: ' + str(maxID) + '`')

    @quote.command(pass_context=True)
    async def list(self, ctx):
        global quotes

        try:
            m = []
            for q in quotes:
                m.append(q.msg + " | id = " + str(q.id + 1))
            pages = utils.Pages(self.bot, message=ctx.message, entries=m, per_page=15)
            await pages.paginate(start_page=1)
        except utils.CannotPaginate as e:
            print(e)

    @quote.command(pass_context=True)
    async def random(self, ctx):
        global quotes

        i = random.randint(0,len(quotes) - 1)
        await ctx.channel.send('`Quote ' + str(quotes[i].id) +':` ' + str(quotes[i].msg))

    @quote.command(pass_context=True)
    async def remove(self, ctx, arg):
        global quotes
        global maxID

        try:
            i = int(arg) - 1
            if i < maxID and i >= 0:
                b = None
                for s in quotes:
                    if i == s.id:
                        b = s
                        break

                if b != None:
                    quotes.remove(b)
                    await ctx.channel.send('`Removed Quote ID: ' + str(i + 1) + '`')
                else:
                    await ctx.channel.send('No quote with this ID')
            else:
                await ctx.channel.send('No quote with this ID')
        except ValueError as e:
            await ctx.channel.send('Invalid input')

    def __unload():
        global quotes
        global maxID

        out = [[0 for x in range(len(quotes))] for y in range(2)]
        for i in range(0, len(quotes)):
            out[0][i] = quotes[i].msg
            out[1][i] = quotes[i].id

        data["quotes"] = out
        data["maxID"] = maxID

        utils.save_json(data, "quotes")
        utils.save_json(data, "Backups\quotes_backup")

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

这堆乱七八糟的代码就是我的报价机器人。我至少可以在开始将命令移动到组中之前运行得很好,这样就知道了很多。问题是在父函数quote()中,ctx.invoked_subcommand似乎总是返回None。我不知道我是否遗漏了什么或者我做错了什么,但我给机器人的确切输入是"->quote list“,据我所知,这应该是正确的。最后要注意的是,根据内置帮助,它确实将Quote识别为一个组,当我执行"->help quote“时,它会显示正确的帮助结构。提前谢谢你,我确信这里的问题并不是太难,我只是遗漏了一些明显的东西。

EN

回答 1

Stack Overflow用户

发布于 2018-03-29 22:31:03

正如Patrick Haugh所评论的,您的子命令将被传递到您的arg参数中。从async def quote(self, ctx, arg):中删除arg,它应该可以正常工作

(如果这有帮助,请给他的评论投赞成票)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49442152

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档