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?