最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

discord - Calling a slash command function that is inside a cog - Stack Overflow

programmeradmin3浏览0评论

So I am basically making a discord music bot, and I have pause command working. However I am trying to add a UI button panel to click on and pause the music. I know I could just make the pausing functionality outside of the pause command and have the music control UI call that function and same with the command, but I really do not want to do this.

I have tried everything to be able to call the command. I know you used to be able to but I guess switch to slash commands broke this.

UI Pause button code:

class MusicControls(discord.ui.View):
    def __init__(self, bot):
        super().__init__(timeout=None)
        self.bot = bot

    @discord.ui.button(label="Pause", style=discord.ButtonStyle.success, emoji="⏸️")
    async def pause(self, interaction: discord.Interaction, button: discord.ui.Button):
        print("Pause button clicked") 
        musicCog = self.bot.get_cog('AudioCommands')

        await musicCog.pause(interaction)

        await interaction.response.send_message("Skipped the song!", ephemeral=True)

Pause Command:

 @discord.app_commandsmand(name="pause", description="Pause the currently playing song.")
 async def pause(self, interaction: discord.Interaction):
    print("Ran Pause")

Obviously the error is: TypeError: 'Command' object is not callable

So I am basically making a discord music bot, and I have pause command working. However I am trying to add a UI button panel to click on and pause the music. I know I could just make the pausing functionality outside of the pause command and have the music control UI call that function and same with the command, but I really do not want to do this.

I have tried everything to be able to call the command. I know you used to be able to but I guess switch to slash commands broke this.

UI Pause button code:

class MusicControls(discord.ui.View):
    def __init__(self, bot):
        super().__init__(timeout=None)
        self.bot = bot

    @discord.ui.button(label="Pause", style=discord.ButtonStyle.success, emoji="⏸️")
    async def pause(self, interaction: discord.Interaction, button: discord.ui.Button):
        print("Pause button clicked") 
        musicCog = self.bot.get_cog('AudioCommands')

        await musicCog.pause(interaction)

        await interaction.response.send_message("Skipped the song!", ephemeral=True)

Pause Command:

 @discord.app_commandsmand(name="pause", description="Pause the currently playing song.")
 async def pause(self, interaction: discord.Interaction):
    print("Ran Pause")

Obviously the error is: TypeError: 'Command' object is not callable

Share Improve this question edited Mar 29 at 22:09 Clayton asked Mar 29 at 21:42 ClaytonClayton 475 bronze badges 2
  • why not just create a function called pause_music and call it in both commands/buttons? – Łukasz Kwieciński Commented Mar 29 at 22:03
  • Because it feels kinda messy. I just wanted to see if I could call it directly. – Clayton Commented Mar 29 at 22:07
Add a comment  | 

1 Answer 1

Reset to default 0

It's messy, but you can use callback, which is the coroutine that is executed when the slash command is called.

The below example code will call the bar command if you press the "Pause" button. Note that None is passed as an interaction, but since bar only does a print it isn't needed.

import discord
from discord.ext import commands


class MusicControls(discord.ui.View):
    def __init__(self, bot):
        super().__init__(timeout=None)
        self.bot = bot

    @discord.ui.button(label="Pause", style=discord.ButtonStyle.success, emoji="⏸️")
    async def pause(self, interaction: discord.Interaction, button: discord.ui.Button):
        print("Pause button clicked") 
        bar_cmd = self.bot.tree.get_command('bar')
        await bar_cmd.callback(None)


client = commands.Bot(intents=discord.Intents.all(), command_prefix='!')


@clientmand()
async def sync(ctx):
    await ctx.bot.tree.sync()


@client.treemand(name='foo')
async def foo(interaction: discord.Interaction):
    await interaction.response.send_message('test', view=MusicControls(client))


@client.treemand(name='bar')
async def bar(interaction: discord.Interaction):
    print("called")


client.run('token')

发布评论

评论列表(0)

  1. 暂无评论