Update commands to slash commands

This commit is contained in:
Logan Cusano
2023-02-25 03:25:07 -05:00
parent 49c80b7f5e
commit 02bf0ebda2
16 changed files with 370 additions and 223 deletions

View File

@@ -1,33 +1,49 @@
var libCore = require("../libCore.js"); const libCore = require("../libCore.js");
const { SlashCommandBuilder } = require('discord.js');
const { DebugBuilder } = require("../utilities/debugBuilder");
const log = new DebugBuilder("server", "add");
module.exports = { module.exports = {
name: 'add', data: new SlashCommandBuilder()
description: 'Add RSS Source', .setName('add')
execute(message, args) { .setDescription('Add RSS Source')
try { .addStringOption(option =>
if (args.length < 3) { option.setName('title')
message.reply(`Please use in !add [title] [https://domain.com/feed.xml] [category] format`); .setDescription('The title of the RSS feed')
return; .setRequired(true))
} .addStringOption(option =>
var title = args[0]; option.setName('link')
var link = args[1]; .setDescription('The link to the RSS feed')
var category = args[2]; .setRequired(true))
.addStringOption(option =>
option.setName('category')
.setDescription('The category for the RSS feed *("ALL" by default")*')
.setRequired(false)),
example: "add [title] [https://domain.com/feed.xml] [category]",
isPrivileged: false,
async execute(interaction, args) {
try {
var title = interaction.options.getString('title');
var link = interaction.options.getString('link');
var category = interaction.options.getString('category');
if (!category) category = "ALL";
libCore.addSource(title, link, category, (err, result) => { libCore.addSource(title, link, category, (err, result) => {
console.log("Result from adding entry", result); console.log("Result from adding entry", result);
if (result) { if (result) {
message.reply(`Adding ${title} to the list of RSS sources`); interaction.reply(`Adding ${title} to the list of RSS sources`);
} else { } else {
message.reply(`${title} already exists in the list of RSS sources`); interaction.reply(`${title} already exists in the list of RSS sources`);
} }
var sources = libCore.getSources();
libCore.loadFeeds(); libCore.loadFeeds();
}); });
} catch (err) { }catch(err){
console.log(err); log.ERROR(err)
message.reply(err.toString()); await message.reply(err.toString());
} }
} }
}; };

View File

@@ -1,13 +1,19 @@
var libCore = require("../libCore.js"); var libCore = require("../libCore.js");
const { SlashCommandBuilder } = require('discord.js');
const { DebugBuilder } = require("../utilities/debugBuilder");
const log = new DebugBuilder("server", "categories");
module.exports = { module.exports = {
name: 'categories', data: new SlashCommandBuilder()
description: 'Categories', .setName('categories')
execute(message) { .setDescription('Return all categories'),
var cats = libCore.getCategories(); example: "categories",
isPrivileged: false,
async execute(interaction) {
var categories = libCore.getCategories();
message.reply( await interaction.reply(
`Categories: [${cats.join(', ')}]` `Categories: [${categories.join(', ')}]`
); );
} }
}; };

View File

@@ -1,21 +1,47 @@
var libCore = require("../libCore.js"); const libCore = require("../libCore.js");
const { SlashCommandBuilder } = require('discord.js');
const { DebugBuilder } = require("../utilities/debugBuilder");
const log = new DebugBuilder("server", "chat");
module.exports = { module.exports = {
name: 'chat', data: new SlashCommandBuilder()
description: 'Chat', .setName('chat')
async execute(message, args) { .setDescription('Send a text prompt to ChatGPT')
if (args.length < 1) { .addStringOption(option =>
message.reply(`Please use in !chat [chat query]`); option.setName('prompt')
return; .setDescription('The prompt to be sent to ChatGPT')
} .setRequired(true))
.addNumberOption(option =>
option.setName('temperature')
.setDescription('Set the temperature, 0 = repetitive, 1 = random')
.setRequired(false))
.addNumberOption(option =>
option.setName('maxTokens')
.setDescription('The max amount of tokens to be spent')
.setRequired(false)),
example: "chat [tell me a story] [0.07] []", // Need to figure out the tokens
isPrivileged: false,
requiresTokens: true,
async execute(interaction) {
// Needs middleware for payment
try { try {
var question = encodeURIComponent(args.join(" ")); var params = {};
var prompt = encodeURIComponent(interaction.options.getString('prompt').join(" "));
var temperature = interaction.options.getNumber('temperature');
var maxTokens = interaction.options.getNumber('maxTokens');
if (temperature) params._temperature = temperature;
if (maxTokens) params._max_tokens = maxTokens;
var response = await libCore.getChat(question); var gptResponse = await prompt.libCore.getChat(prompt, params);
message.reply(`${message.author.username} ${response}`); await interaction.reply(`${interaction.author.username} ${gptResponse}`);
} catch (err) { // Needs reply code to reply to the generation
//message.reply(err.toString()); }catch(err){
} log.ERROR(err)
} //await interaction.reply(err.toString());
}
}
}; };

View File

@@ -1,17 +1,20 @@
var libUtils = require("../libUtils.js"); const libUtils = require("../libUtils.js");
const discordAuth = require("../utilities/discordAuthorization");
const { SlashCommandBuilder } = require('discord.js');
module.exports = { module.exports = {
name: 'exit', data: new SlashCommandBuilder()
description: 'Exit the current application.', .setName('exit')
.setDescription('Exit the current application.'),
example: "exit", example: "exit",
isPrivileged: true, isPrivileged: true,
async execute(message) { async execute(interaction) {
// TODO - Need to add middleware for admins // TODO - Need to add middleware for admins
if (message.member.user.tag !== "Logan#3331") message.reply(`Sorry ${message.member.user.tag}, you are not allowed to use this command.`); discordAuth.authorizeCommand(interaction, this.data.name, async () => {
message.reply( await interaction.reply(
`Goodbye world - Disconnection imminent.` `Goodbye world - Disconnection imminent.`
); );
await libUtils.sleep(5000); libUtils.runAfter(process.exit, 5000);
await new Promise(resolve => setTimeout(process.exit(), 5000)); })
} }
}; };

View File

@@ -1,64 +0,0 @@
var libCore = require("../libCore.js");
module.exports = {
name: 'find',
description: 'Find RSS Sources',
execute(message, args) {
try {
if (args.length < 1) {
message.reply(`Missing arguments`);
return;
}
var search = args.join(" ");
var found = false;
let i = 0;
let iSave = 0
let count = 0;
var feedArray = libCore.getFeeds();
var searchString = "";
var foundError = false;
feedArray.forEach(linkFlay => {
try {
if (linkFlay.title.toLowerCase().indexOf(search.toLowerCase()) > -1) {
iSave = i;
found = true;
console.log(linkFlay.title);
searchString += `Use !get ${i} to view: ${linkFlay.title} \n`;
count++;
if (count > 5) {
message.reply(searchString);
searchString = "";
}
}
i++;
} catch (error) {
foundError = true;
console.log(error);
}
});
if (foundError) {
message.reply("Error in search");
return;
} else {
message.reply('-' + searchString);
}
if (count == 1) {
//message.channel.send('Displaying 1 result');
//message.channel.send('!get '+iSave);
}
if (!found) {
message.reply(`No results found for: ${search}`);
}
} catch (error) {
message.reply(error.toString());
}
}
};

View File

@@ -1,14 +1,21 @@
var libCore = require("../libCore.js"); const libCore = require("../libCore.js");
const { SlashCommandBuilder } = require('discord.js');
const { DebugBuilder } = require("../utilities/debugBuilder");
const log = new DebugBuilder("server", "food");
module.exports = { module.exports = {
name: 'food', data: new SlashCommandBuilder()
description: 'Food', .setName('food')
async execute(message, args) { .setDescription('Gets a random recipe and gives it to you'),
example: "food",
isPrivileged: false,
requiresTokens: false,
async execute(interaction) {
try { try {
var resultArray = await libCore.getFood(); var resultArray = await libCore.getFood();
message.reply(` await interaction.reply(`
[**${resultArray.strMeal}** - *${resultArray.strCategory}*] [**${resultArray.strMeal}** - *${resultArray.strCategory}*]
${resultArray.strSource} ${resultArray.strSource}
@@ -16,8 +23,9 @@ module.exports = {
${resultArray.strMealThumb} ${resultArray.strMealThumb}
`); `);
} catch (err) { }catch(err){
message.reply(err.toString()); log.ERROR(err)
} //await interaction.reply(err.toString());
} }
}; }
};

View File

@@ -1,22 +1,28 @@
var libCore = require("../libCore.js"); const libCore = require("../libCore.js");
const { SlashCommandBuilder } = require('discord.js');
const { DebugBuilder } = require("../utilities/debugBuilder");
const log = new DebugBuilder("server", "get");
module.exports = { module.exports = {
name: 'get', data: new SlashCommandBuilder()
description: 'Get RSS Source Link', .setName('get')
execute(message, args) { .setDescription('Get RSS link by number')
try { .addNumberOption(option =>
option.setName('position')
if (args.length < 1) { .setDescription('The index position of the RSS link')
message.reply(`Use !get [number] Ex: !get 25`); .setRequired(true)),
return; example: "get [1]",
} isPrivileged: false,
requiresTokens: false,
async execute(interaction) {
try {
var search = args[0]; var search = args[0];
var catName = "All"; var catName = "All";
var feedArray = libCore.getFeeds(); var feedArray = libCore.getFeeds();
message.reply(`**Retrieving**: [${catName}] (${feedArray[search].link})`); await interaction.reply(`**Retrieving**: [${catName}] (${feedArray[search].link})`);
} catch (err) { }catch(err){
message.reply(err.toString()); log.ERROR(err)
} //await interaction.reply(err.toString());
} }
}
}; };

View File

@@ -1,30 +1,43 @@
const fs = require('fs'); const fs = require('fs');
const path = require('node:path'); const path = require('node:path');
const { SlashCommandBuilder } = require('discord.js');
const { DebugBuilder } = require("../utilities/debugBuilder");
const log = new DebugBuilder("server", "help");
const prefix = process.env.PREFIX; const prefix = process.env.PREFIX;
module.exports = { const commandsPath = path.resolve(__dirname, '../commands'); // Resolves from either working dir or __dirname
name: 'help', const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
description: 'Display this help message',
example: "help",
execute(message) {
messageText = "";
const commandsPath = path.resolve('./commands'); // Resolves from either working dir or __dirname
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
if (!command.isPrivileged){ // TODO - Need to add middleware for admins module.exports = {
if (messageText.length > 1 && messageText.slice(-2) != `\n`){ data: new SlashCommandBuilder()
messageText += `\n`; .setName('help')
} .setDescription('Display this help message'),
example: "help",
messageText += `**${prefix}${command.name}** - *${command.description}*`; isPrivileged: false,
async execute(interaction) {
if (command.example) messageText += `\n\t\t***Usage:*** \`${command.example}\`` try{
} messageText = "";
}
message.reply(messageText); for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
if (!command.isPrivileged){ // TODO - Need to add middleware for admins
if (messageText.length > 1 && messageText.slice(-2) != `\n`){
messageText += `\n`;
}
messageText += `**/${command.data.name}** - *${command.data.description}*`;
if (command.example) messageText += `\n\t\t***Usage:*** \`${command.example}\``
}
}
await interaction.reply(messageText);
}catch(err){
log.ERROR(err)
//await interaction.reply(err.toString());
}
} }
}; };

View File

@@ -1,6 +1,7 @@
const { SlashCommandBuilder } = require('discord.js'); const { SlashCommandBuilder } = require('discord.js');
const { DebugBuilder } = require("../utilities/debugBuilder");
const log = new DebugBuilder("server", "ping");
// TODO - Add insults as the response to this command
module.exports = { module.exports = {
data: new SlashCommandBuilder() data: new SlashCommandBuilder()
.setName('ping') .setName('ping')
@@ -8,11 +9,19 @@ module.exports = {
/* /*
.addStringOption(option => .addStringOption(option =>
option.setName('input') option.setName('input')
.setDescription('The input to echo back')), .setDescription('The input to echo back')
.setRequired(false)
.addChoices()),
*/ */
example: "ping", example: "ping",
isPrivileged: false, isPrivileged: false,
async execute(message) { requiresTokens: false,
await message.channel.send('**Pong.**'); async execute(interaction) {
try{
await interaction.channel.send('**Pong.**'); // TODO - Add insults as the response to this command
}catch(err){
log.ERROR(err)
//await interaction.reply(err.toString());
}
} }
}; };

View File

@@ -1,15 +1,26 @@
var libCore = require("../libCore.js"); var libCore = require("../libCore.js");
const quote_url = "https://zenquotes.io/api/quotes/"; const quote_url = "https://zenquotes.io/api/quotes/";
const { SlashCommandBuilder } = require('discord.js');
const { DebugBuilder } = require("../utilities/debugBuilder");
const log = new DebugBuilder("server", "quotes");
module.exports = { module.exports = {
name: 'quote', data: new SlashCommandBuilder()
description: 'Quote!', .setName('quote')
async execute(message) { .setDescription('Get a random quote'),
example: "quote",
isPrivileged: false,
requiresTokens: false,
async execute(interaction) {
try { try {
var quotes = await libCore.getQuotes(quote_url); var quotes = await libCore.getQuotes(quote_url);
var selectedQuote = Math.floor(Math.random() * quotes.length); var selectedQuote = Math.floor(Math.random() * quotes.length);
message.reply(quotes[selectedQuote].quoteText + " - " + quotes[selectedQuote].quoteAuthor);
} catch (e) { interaction.reply(quotes[selectedQuote].quoteText + " - " + quotes[selectedQuote].quoteAuthor);
message.reply(e.toString()); } catch(err){
log.ERROR(err)
//await interaction.reply(err.toString());
} }
} }
}; };

View File

@@ -1,24 +1,32 @@
var libCore = require("../libCore.js"); var libCore = require("../libCore.js");
const { SlashCommandBuilder } = require('discord.js');
const { DebugBuilder } = require("../utilities/debugBuilder");
const log = new DebugBuilder("server", "random");
module.exports = { module.exports = {
name: 'random', data: new SlashCommandBuilder()
description: 'Get a random link from one of the RSS feeds.', .setName('random')
example: "random [category]", .setDescription('Get a random link from one of the RSS feeds.')
execute(message, args) { .addStringOption(option =>
try { option.setName('category')
var category = ""; .setDescription('Select the category to grab from *(default is "ALL")*')
var catName = "All"; .setRequired(false)),
if (args.length == 1) { example: "random [category]",
category = args[0]; isPrivileged: false,
catName = category; requiresTokens: false,
} async execute(interaction) {
try {
let category = interaction.options.getString('category');
if (!category) category = "ALL";
var feedArray = libCore.getFeeds(category); var feedArray = libCore.getFeeds(category);
var i = Math.floor(Math.random() * (feedArray.length - 0) + 0); var i = Math.floor(Math.random() * (feedArray.length - 0) + 0);
message.reply(`**Retrieved**: [${catName}](${feedArray[i].link})`); await message.reply(`**Retrieved**: [${category}](${feedArray[i].link})`);
} catch (err) { } catch (err) {
message.reply(err.toString()); log.ERROR(err)
//await interaction.reply(err.toString());
} }
} }
}; };

View File

@@ -1,31 +1,37 @@
var libCore = require("../libCore.js"); var libCore = require("../libCore.js");
const { SlashCommandBuilder } = require('discord.js');
const { DebugBuilder } = require("../utilities/debugBuilder");
const log = new DebugBuilder("server", "remove");
module.exports = { module.exports = {
name: 'remove', data: new SlashCommandBuilder()
description: 'Remove RSS Source', .setName('remove')
execute(message, args) { .setDescription('Remove an RSS source by it\' title')
try { .addStringOption(option =>
if (args.length < 1) { option.setName('title')
message.reply(`Please use in '${process.env.prefix}remove [title]' format`); .setDescription('The title of the source to remove')
return; .setRequired(true)),
} example: "remove ['Leafly']",
var title = args[0]; isPrivileged: false,
requiresTokens: false,
async execute(interaction) {
try{
var title = interaction.options.getString("title");
libCore.deleteSource(title, (err, result) => { libCore.deleteSource(title, (err, result) => {
console.log("Result from removing entry", result); console.log("Result from removing entry", result);
if (result) { if (result) {
message.reply(`Removing ${title} from the list of RSS sources`); interaction.reply(`Removing ${title} from the list of RSS sources`);
} else { } else {
message.reply(`${title} does not exist in the list of RSS sources`); interaction.reply(`${title} does not exist in the list of RSS sources`);
} }
var sources = libCore.getSources();
libCore.loadFeeds(); libCore.loadFeeds();
}); });
} catch (err) { }catch(err){
console.log(err); log.ERROR(err)
message.reply(err.toString()); interaction.reply(err.toString());
} }
} }
}; };

View File

@@ -1,5 +1,33 @@
var libCore = require("../libCore.js"); var libCore = require("../libCore.js");
const { SlashCommandBuilder } = require('discord.js');
const { DebugBuilder } = require("../utilities/debugBuilder");
const log = new DebugBuilder("server", "slang");
module.exports = {
data: new SlashCommandBuilder()
.setName('slang')
.setDescription('Search Urban Dictionary for a phrase.')
.addStringOption(option =>
option.setName('phrase')
.setDescription('The phrase to search')
.setRequired(true)),
example: "slang \"[phrase to search]\"",
isPrivileged: false,
requiresTokens: false,
async execute(interaction) {
try{
var question = encodeURIComponent(interaction.options.getString('phrase').join(" "));
var slangData = await libCore.getSlang(question);
await message.reply(`**Term**: ${decodeURIComponent(question)}\n\n**Answer**: ${slangData.definition}\n\n**Example**: ${slangData.example}`);
}catch(err){
log.ERROR(err)
//await interaction.reply(err.toString());
}
}
};
module.exports = { module.exports = {
name: 'slang', name: 'slang',
description: 'Search Urban Dictionary for a phrase.', description: 'Search Urban Dictionary for a phrase.',

View File

@@ -1,5 +1,33 @@
var libCore = require("../libCore.js"); var libCore = require("../libCore.js");
const { SlashCommandBuilder } = require('discord.js');
const { DebugBuilder } = require("../utilities/debugBuilder");
const log = new DebugBuilder("server", "sources");
module.exports = {
data: new SlashCommandBuilder()
.setName('sources')
.setDescription('Replies with your input!'),
example: "sources",
isPrivileged: false,
requiresTokens: false,
async execute(interaction) {
try{
var sourceArray = libCore.getSources();
var sourceString = "";
sourceArray.forEach(source => {
sourceString +=`[${source.title}](${source.link}) \n`;
});
await interaction.reply(sourceString);
}catch(err){
log.ERROR(err)
//await interaction.reply(err.toString());
}
}
};
module.exports = { module.exports = {
name: 'sources', name: 'sources',
description: 'List RSS Sources', description: 'List RSS Sources',

View File

@@ -1,16 +1,23 @@
var libCore = require("../libCore.js"); var libCore = require("../libCore.js");
module.exports = { const { SlashCommandBuilder } = require('discord.js');
name: 'update', const { DebugBuilder } = require("../utilities/debugBuilder");
description: 'Get RSS Source Link', const log = new DebugBuilder("server", "update");
execute(message, args) {
message.reply(`Loading Feeds from Sources`);
try {
libCore.loadFeeds();
} catch (error) {
console.log(error);
}
feedArray = libCore.getFeeds();
} module.exports = {
}; data: new SlashCommandBuilder()
.setName('update')
.setDescription('Reloads all RSS feeds'),
example: "update",
isPrivileged: false,
requiresTokens: false,
async execute(interaction) {
try{
libCore.loadFeeds();
await interaction.reply("Reloaded all RSS feeds");
}catch(err){
log.ERROR(err)
//await interaction.reply(err.toString());
}
}
};

View File

@@ -1,5 +1,41 @@
var libCore = require("../libCore.js"); var libCore = require("../libCore.js");
const { SlashCommandBuilder } = require('discord.js');
const { DebugBuilder } = require("../utilities/debugBuilder");
const log = new DebugBuilder("server", "alert");
module.exports = {
data: new SlashCommandBuilder()
.setName('alert')
.setDescription('Get any current weather alerts in the state specified.')
.addStringOption(option =>
option.setName('state')
.setDescription('The state to get any current weather alerts from')
.setRequired(false)
.addChoices()),
example: "alert [state]",
isPrivileged: false,
requiresTokens: false,
async execute(interaction) {
try{
var question = encodeURIComponent(interaction.options.getString("state").join(" "));
var answerData = await libCore.weatherAlert(question);
answerData.forEach(feature => {
interaction.reply(`
${feature.properties.areaDesc}
${feature.properties.headline}
${feature.properties.description}
`);
});
}catch(err){
log.ERROR(err)
//await interaction.reply(err.toString());
}
}
};
module.exports = { module.exports = {
name: 'alert', name: 'alert',
description: 'Get any current weather alerts in the state specified.', description: 'Get any current weather alerts in the state specified.',