Python Discord.py如何在另一个命令中调用另一个命令?
我希望我的机器人在使用已定义的函数(+play)键入+playtest时播放特定的歌曲 但我有一个错误,他说 “Discord.ext.commands.errors.CommandInvokeError:命令引发了错误 异常:TypeError:“命令”对象不可调用 除了这个命令之外,整个代码都工作得很好 我想知道ctx.invoke是否允许传递参数?或者我错过了什么 这是我的简短代码Python Discord.py如何在另一个命令中调用另一个命令?,python,discord,discord.py,Python,Discord,Discord.py,我希望我的机器人在使用已定义的函数(+play)键入+playtest时播放特定的歌曲 但我有一个错误,他说 “Discord.ext.commands.errors.CommandInvokeError:命令引发了错误 异常:TypeError:“命令”对象不可调用 除了这个命令之外,整个代码都工作得很好 我想知道ctx.invoke是否允许传递参数?或者我错过了什么 这是我的简短代码 import discord import wavelink from discord.ext import
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')
ctx.invoke确实允许传递参数,但它们需要以不同的方式处理,以适应(
function(params)
)
参数必须在调用中显式显示(例如,param='value'
),并且命令必须是命令对象。
这就是调用命令的方式:
@commands.command()
async def playtest(self, ctx):
await ctx.invoke(self.bot.get_command('play'), query='hi')