TheOdinProject / odin-bot-v2

The bot that breathes life into our Discord community
ISC License
48 stars 77 forks source link

Deduplicate command info between old and new style commands #553

Closed Asartea closed 4 months ago

Asartea commented 4 months ago

Complete the following REQUIRED checkboxes:

The following checkbox is OPTIONAL:


1. Description of the Feature Request: Currently, commands for which there is both a text and a slash command version define their description, title, etc separately, even though these are usually the same. This means that to update a command one has to remember to change both instances separately or risk getting them out of sync.

It would be much easier if these commands had a single source of truth, from which both individually pull the required info. The easiest way to accomplish this in my opinion would be to create a JS file containing this info, and then import the required parts in each individual file.

Example

// config file
const commandContents = {
    command-name: {
        color: "#cc9543", // there is an argument that since color is always the same,
// it should be deduplicated to a single meta.color property, but that adds complexity when importing
        title: "some title",
        description: "some description",
        url: "some url",
    },
    top: {
        color: "#cc9543",
        title: "some title",
        description: "some description",
        // url is optional
    },
    // etc...
}

module.exports = commandContents;
// text command
const Discord = require('discord.js');
const { registerBotCommand } = require('../botEngine');
const { commandName } = require('../config-file-name');

const {color, title, description} = commandName;

const command = {
  regex: "command regex",
  cb: () => {
    const commandEmbed = new Discord.EmbedBuilder()
      .setColor(color)
      .setTitle(title)
      .setDescription(description);
    return { embeds: [commandEmbed] };
  },
};

registerBotCommand(command.regex, command.cb);

module.exports = command;
//slash command

const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const { commandName } = require('../config-file-name');

const {color, title, description} = commandName ;

module.exports = {
  data: new SlashCommandBuilder()
    .setName('command-name')
    .setDescription('description') // these could also be stored, but they are slash command unique, so they don't have to be
    .addUserOption((option) => option.setName('user').setDescription('user to ping')),
  execute: async (interaction) => {
    const userId = interaction.options.getUser('user');

    const commandEmbed = new EmbedBuilder()
      .setColor(color)
      .setTitle(title)
      .setDescription(description);

    await interaction.reply({
      content: userId ? `${userId}` : '',
      embeds: [commandEmbed],
    });
  },
};

2. Acceptance Criteria:

CouchofTomato commented 4 months ago

@TheOdinProject/odin-bot Can someone review this please

MaoShizhong commented 4 months ago

This makes sense to prevent discrepancies from changing only one command and not its mirror command.

Assigning you @Asartea. We'd of course need any tests for legacy commands to pass, and for screenshots showing the slash commands are also unchanged by this refactor.