How can I integrate both prefix commands and slash commands in a single Discord bot implementation?

I am developing a Discord bot and I want to eliminate redundant code. At present, I have distinct codes for prefix commands and slash commands, which essentially perform similar functions.

Here’s my prefix command code:

import discord
from discord.ext import commands

class CheckStatus(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command()
    async def check_status(self, ctx):
        response_msg = await ctx.send("Checking the status...")
        response_time = self.bot.latency * 1000  
        await response_msg.edit(content=f'The bot is online! Response time: **{response_time:.2f} ms**')
      
async def setup(bot):
    await bot.add_cog(CheckStatus(bot))

And for the slash command:

import discord
from discord import app_commands
from discord.ext import commands

class SlashCheck(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @app_commands.command()
    async def check_status(self, interaction: discord.Interaction):
        response_time = self.bot.latency * 1000  
        await interaction.response.send_message(content='Checking the status...')
        original_msg = await interaction.original_response()
        await original_msg.edit(content=f'The bot is online! Response time: **{response_time:.2f} ms**')

async def setup(bot):
    await bot.add_cog(SlashCheck(bot))

Is it feasible to consolidate both command types into a single class or function to minimize code duplication?

Yeah, just use @commands.hybrid_command() instead of separate decorators. It automatically works as both prefix and slash command - no need for duplicate code. Replace @commands.command() with @commands.hybrid_command() and stick with ctx.send() for responses. Discord.py handles all the interaction stuff behind the scenes. I’ve used this for months and it works great for most commands. You’ll only need separate implementations if you’re doing something complex that needs interaction-specific features.

yup! combine them by making a single method for handling the logic. then call this from both commands. just use ctx.send() for the prefix ones and interaction.response.send_message() for slash commands. easy peasy!

Had this exact problem last year - maintaining multiple command implementations was a pain. Here’s what worked: create a shared core function for the business logic, then use lightweight wrappers for each command type that just handle response formatting. Keep your CheckStatus cog but add a private _get_status_info() method that returns the response time calculation. Both prefix and slash commands call this method and format output however they need. You’re not duplicating logic, just the response mechanism - which you need anyway since they handle responses differently. I found this more reliable than hybrid commands when you need precise control over each command type, especially for editing commands like yours.