Files
drb-server/discordBot/commands/rssRemove.mjs
Logan Cusano 117cbea67f
All checks were successful
release-tag / release-image (push) Successful in 1m52s
Lint JavaScript/Node.js / lint-js (push) Successful in 11s
DRB Tests / drb_mocha_tests (push) Successful in 29s
Linting
2024-08-11 15:57:46 -04:00

71 lines
2.5 KiB
JavaScript

import { SlashCommandBuilder } from "discord.js";
import { DebugBuilder } from "../../modules/debugger.mjs";
import { removeSource } from "../../rss-manager/sourceManager.mjs";
import {
getAllFeeds,
deleteFeedByTitle,
} from "../../modules/mongo-wrappers/mongoFeedsWrappers.mjs";
const log = new DebugBuilder("server", "discordBot.command.rssRemove");
// Exporting data property that contains the command structure for discord including any params
export const data = new SlashCommandBuilder()
.setName("rss-remove")
.setDescription("Add RSS Source")
.addStringOption((option) =>
option
.setName("title")
.setDescription("The title of the RSS feed")
.setRequired(true)
.setAutocomplete(true),
);
// Exporting other properties
export const example = "/rss-remove [title]"; // An example of how the command would be run in discord chat, this will be used for the help command
export const deferInitialReply = false; // If we the initial reply in discord should be deferred. This gives extra time to respond, however the method of replying is different.
/**
* Function to give the user auto-reply suggestions
* @param {any} nodeIo The nodeIO server for manipulation of sockets
* @param {any} interaction The interaction object
*/
export async function autocomplete(nodeIo, interaction) {
const focusedValue = interaction.options.getFocused();
const choices = (await getAllFeeds()) ?? [];
log.INFO("RSS Remove Choices:", choices);
const filtered = choices.filter((choice) =>
choice.title.startsWith(focusedValue),
);
log.DEBUG(focusedValue, choices, filtered);
await interaction.respond(
filtered.map((choice) => ({ name: choice.title, value: choice.title })),
);
}
/**
* The function to run when the command is called by a discord user
* @param {any} nodeIo The nodeIO server for manipulation of sockets
* @param {any} interaction The interaction object
*/
export const execute = async (nodeIo, interaction) => {
try {
var title = interaction.options.getString("title");
await interaction.reply(
`Removing ${title} from the list of RSS sources, please wait...`,
);
const results = await deleteFeedByTitle(title);
if (!results) {
log.WARN(`Failed to remove source: ${title}`);
await interaction.editReply(`Failed to remove source: '${title}'`);
return;
}
await interaction.editReply(
`${title} was successfully removed from the RSS sources.`,
);
} catch (err) {
log.ERROR(err);
await interaction.editReply(err.toString());
}
};