Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
315 views
in Technique[技术] by (71.8m points)

python - Discord.py how to invoke another command inside another one?

I want my bot to play a specific song when typing +playtest using already defined function (+play) but i got an error says

"Discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: 'Command' object is not callable"

an entire code work perfectly fine except for this command i wonder does ctx.invoke enable passing arguments? or i just missed something

here is my brief code

import discord
import wavelink
from discord.ext import commands

import asyncio
from bs4 import BeautifulSoup
import requests
import datetime

queue = []



class Bot(commands.Bot):

    def __init__(self):
        super(Bot, self).__init__(command_prefix=['+'])

        self.add_cog(Music(self))

    async def on_ready(self):
        print(f'Logged in as {self.user.name} | {self.user.id}')


class Music(commands.Cog):

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

        if not hasattr(bot, 'wavelink'):
            self.bot.wavelink = wavelink.Client(bot=self.bot)

        self.bot.loop.create_task(self.start_nodes())
        self.bot.remove_command("help")

    async def start_nodes(self):
        await self.bot.wait_until_ready()

        await self.bot.wavelink.initiate_node(host='127.0.0.1',
                                              port=80,
                                              rest_uri='http://127.0.0.1:80',
                                              password='testing',
                                              identifier='TEST',
                                              region='us_central')

    @commands.command(name='connect')
    async def connect_(self, ctx, *, channel: discord.VoiceChannel = None):


    @commands.command()
    async def help(self, ctx):

    @commands.command()
    async def play(self, ctx, *, query: str):

    @commands.command(aliases=['sc'])
    async def soundcloud(self, ctx, *, query: str):


    @commands.command()
    async def leave(self, ctx):

    @commands.command(aliases=['queue', 'q'])
    async def check_queue(self, ctx):

    @commands.command(aliases=['clearq', 'clearqueue'])
    async def clear_queue(self, ctx):


    @commands.command(aliases=['removequeue', 'removeq', 'req'])
    async def remove_queue(self, ctx, num: int):

    @commands.command()
    async def skip(self, ctx):


    @commands.command(aliases=['eq'])
    async def equalizer(self, ctx: commands.Context, *, equalizer: str):

    @commands.command()
    async def playtest(self,ctx):
       await ctx.invoke(self.play('hi'))

bot = Bot()
bot.run('sd')
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

ctx.invoke does allow passing arguments, but they need to be handled in a different way to how you may be used to ( function(params) )

The parameters must be explicitly shown in the invoke (e.g. param = 'value') and the command must be a command object. This would be how you could invoke a command:

@commands.command()
async def playtest(self, ctx):
   await ctx.invoke(self.bot.get_command('play'), query='hi')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...