diff --git a/commands/add.js b/commands/add.js index ec443f4..cc566af 100644 --- a/commands/add.js +++ b/commands/add.js @@ -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 = { - name: 'add', - description: 'Add RSS Source', - execute(message, args) { - try { - if (args.length < 3) { - message.reply(`Please use in !add [title] [https://domain.com/feed.xml] [category] format`); - return; - } - var title = args[0]; - var link = args[1]; - var category = args[2]; + data: new SlashCommandBuilder() + .setName('add') + .setDescription('Add RSS Source') + .addStringOption(option => + option.setName('title') + .setDescription('The title of the RSS feed') + .setRequired(true)) + .addStringOption(option => + option.setName('link') + .setDescription('The link to the RSS feed') + .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) => { console.log("Result from adding entry", result); if (result) { - message.reply(`Adding ${title} to the list of RSS sources`); + interaction.reply(`Adding ${title} to the list of RSS sources`); } 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(); }); - } catch (err) { - console.log(err); - message.reply(err.toString()); - } - } + }catch(err){ + log.ERROR(err) + await message.reply(err.toString()); + } + } }; \ No newline at end of file diff --git a/commands/category.js b/commands/category.js index 8a5429c..88e3c01 100644 --- a/commands/category.js +++ b/commands/category.js @@ -1,13 +1,19 @@ var libCore = require("../libCore.js"); +const { SlashCommandBuilder } = require('discord.js'); +const { DebugBuilder } = require("../utilities/debugBuilder"); +const log = new DebugBuilder("server", "categories"); module.exports = { - name: 'categories', - description: 'Categories', - execute(message) { - var cats = libCore.getCategories(); + data: new SlashCommandBuilder() + .setName('categories') + .setDescription('Return all categories'), + example: "categories", + isPrivileged: false, + async execute(interaction) { + var categories = libCore.getCategories(); - message.reply( - `Categories: [${cats.join(', ')}]` + await interaction.reply( + `Categories: [${categories.join(', ')}]` ); } }; \ No newline at end of file diff --git a/commands/chat.js b/commands/chat.js index 1e72dc5..ef59513 100644 --- a/commands/chat.js +++ b/commands/chat.js @@ -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 = { - name: 'chat', - description: 'Chat', - async execute(message, args) { - if (args.length < 1) { - message.reply(`Please use in !chat [chat query]`); - return; - } + data: new SlashCommandBuilder() + .setName('chat') + .setDescription('Send a text prompt to ChatGPT') + .addStringOption(option => + option.setName('prompt') + .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 { - 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); - message.reply(`${message.author.username} ${response}`); - - } catch (err) { - //message.reply(err.toString()); - } - } + var gptResponse = await prompt.libCore.getChat(prompt, params); + await interaction.reply(`${interaction.author.username} ${gptResponse}`); + + // Needs reply code to reply to the generation + }catch(err){ + log.ERROR(err) + //await interaction.reply(err.toString()); + } + } }; \ No newline at end of file diff --git a/commands/exit.js b/commands/exit.js index ba60afd..032ebe0 100644 --- a/commands/exit.js +++ b/commands/exit.js @@ -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 = { - name: 'exit', - description: 'Exit the current application.', + data: new SlashCommandBuilder() + .setName('exit') + .setDescription('Exit the current application.'), example: "exit", - isPrivileged: true, - async execute(message) { + isPrivileged: true, + async execute(interaction) { // 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.`); - message.reply( - `Goodbye world - Disconnection imminent.` - ); - await libUtils.sleep(5000); - await new Promise(resolve => setTimeout(process.exit(), 5000)); + discordAuth.authorizeCommand(interaction, this.data.name, async () => { + await interaction.reply( + `Goodbye world - Disconnection imminent.` + ); + libUtils.runAfter(process.exit, 5000); + }) } }; \ No newline at end of file diff --git a/commands/find.js b/commands/find.js deleted file mode 100644 index b5d1453..0000000 --- a/commands/find.js +++ /dev/null @@ -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()); - } - - } -}; \ No newline at end of file diff --git a/commands/food.js b/commands/food.js index 65cfab2..b5b9c7a 100644 --- a/commands/food.js +++ b/commands/food.js @@ -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 = { - name: 'food', - description: 'Food', - async execute(message, args) { + data: new SlashCommandBuilder() + .setName('food') + .setDescription('Gets a random recipe and gives it to you'), + example: "food", + isPrivileged: false, + requiresTokens: false, + async execute(interaction) { try { var resultArray = await libCore.getFood(); - message.reply(` + await interaction.reply(` [**${resultArray.strMeal}** - *${resultArray.strCategory}*] ${resultArray.strSource} @@ -16,8 +23,9 @@ module.exports = { ${resultArray.strMealThumb} `); - } catch (err) { - message.reply(err.toString()); - } - } -}; \ No newline at end of file + }catch(err){ + log.ERROR(err) + //await interaction.reply(err.toString()); + } + } +}; diff --git a/commands/get.js b/commands/get.js index 63edc77..e1b9e52 100644 --- a/commands/get.js +++ b/commands/get.js @@ -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 = { - name: 'get', - description: 'Get RSS Source Link', - execute(message, args) { - try { - - if (args.length < 1) { - message.reply(`Use !get [number] Ex: !get 25`); - return; - } + data: new SlashCommandBuilder() + .setName('get') + .setDescription('Get RSS link by number') + .addNumberOption(option => + option.setName('position') + .setDescription('The index position of the RSS link') + .setRequired(true)), + example: "get [1]", + isPrivileged: false, + requiresTokens: false, + async execute(interaction) { + try { var search = args[0]; var catName = "All"; var feedArray = libCore.getFeeds(); - message.reply(`**Retrieving**: [${catName}] (${feedArray[search].link})`); - } catch (err) { - message.reply(err.toString()); - } - } - + await interaction.reply(`**Retrieving**: [${catName}] (${feedArray[search].link})`); + }catch(err){ + log.ERROR(err) + //await interaction.reply(err.toString()); + } + } }; \ No newline at end of file diff --git a/commands/help.js b/commands/help.js index e32eb04..e714c3f 100644 --- a/commands/help.js +++ b/commands/help.js @@ -1,30 +1,43 @@ const fs = require('fs'); 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; -module.exports = { - name: 'help', - 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); +const commandsPath = path.resolve(__dirname, '../commands'); // Resolves from either working dir or __dirname +const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js')); - if (!command.isPrivileged){ // TODO - Need to add middleware for admins - if (messageText.length > 1 && messageText.slice(-2) != `\n`){ - messageText += `\n`; - } - - messageText += `**${prefix}${command.name}** - *${command.description}*`; - - if (command.example) messageText += `\n\t\t***Usage:*** \`${command.example}\`` - } - } - message.reply(messageText); +module.exports = { + data: new SlashCommandBuilder() + .setName('help') + .setDescription('Display this help message'), + example: "help", + isPrivileged: false, + async execute(interaction) { + try{ + 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()); + } } }; \ No newline at end of file diff --git a/commands/ping.js b/commands/ping.js index f0475be..8fe0b24 100644 --- a/commands/ping.js +++ b/commands/ping.js @@ -1,6 +1,7 @@ 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 = { data: new SlashCommandBuilder() .setName('ping') @@ -8,11 +9,19 @@ module.exports = { /* .addStringOption(option => option.setName('input') - .setDescription('The input to echo back')), + .setDescription('The input to echo back') + .setRequired(false) + .addChoices()), */ example: "ping", - isPrivileged: false, - async execute(message) { - await message.channel.send('**Pong.**'); + isPrivileged: false, + requiresTokens: false, + 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()); + } } }; \ No newline at end of file diff --git a/commands/quotes.js b/commands/quotes.js index a0f7bfa..1735311 100644 --- a/commands/quotes.js +++ b/commands/quotes.js @@ -1,15 +1,26 @@ var libCore = require("../libCore.js"); 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 = { - name: 'quote', - description: 'Quote!', - async execute(message) { + data: new SlashCommandBuilder() + .setName('quote') + .setDescription('Get a random quote'), + example: "quote", + isPrivileged: false, + requiresTokens: false, + async execute(interaction) { try { var quotes = await libCore.getQuotes(quote_url); var selectedQuote = Math.floor(Math.random() * quotes.length); - message.reply(quotes[selectedQuote].quoteText + " - " + quotes[selectedQuote].quoteAuthor); - } catch (e) { - message.reply(e.toString()); + + interaction.reply(quotes[selectedQuote].quoteText + " - " + quotes[selectedQuote].quoteAuthor); + } catch(err){ + log.ERROR(err) + //await interaction.reply(err.toString()); } } }; \ No newline at end of file diff --git a/commands/random.js b/commands/random.js index 4fc6b70..4146711 100644 --- a/commands/random.js +++ b/commands/random.js @@ -1,24 +1,32 @@ var libCore = require("../libCore.js"); +const { SlashCommandBuilder } = require('discord.js'); +const { DebugBuilder } = require("../utilities/debugBuilder"); +const log = new DebugBuilder("server", "random"); + module.exports = { - name: 'random', - description: 'Get a random link from one of the RSS feeds.', - example: "random [category]", - execute(message, args) { - try { - var category = ""; - var catName = "All"; - if (args.length == 1) { - category = args[0]; - catName = category; - } + data: new SlashCommandBuilder() + .setName('random') + .setDescription('Get a random link from one of the RSS feeds.') + .addStringOption(option => + option.setName('category') + .setDescription('Select the category to grab from *(default is "ALL")*') + .setRequired(false)), + example: "random [category]", + isPrivileged: false, + requiresTokens: false, + async execute(interaction) { + try { + let category = interaction.options.getString('category'); + if (!category) category = "ALL"; var feedArray = libCore.getFeeds(category); var i = Math.floor(Math.random() * (feedArray.length - 0) + 0); - message.reply(`**Retrieved**: [${catName}](${feedArray[i].link})`); - } catch (err) { - message.reply(err.toString()); + await message.reply(`**Retrieved**: [${category}](${feedArray[i].link})`); + } catch (err) { + log.ERROR(err) + //await interaction.reply(err.toString()); } - } + } }; \ No newline at end of file diff --git a/commands/remove.js b/commands/remove.js index 7acdb23..69bf6b6 100644 --- a/commands/remove.js +++ b/commands/remove.js @@ -1,31 +1,37 @@ var libCore = require("../libCore.js"); +const { SlashCommandBuilder } = require('discord.js'); +const { DebugBuilder } = require("../utilities/debugBuilder"); +const log = new DebugBuilder("server", "remove"); + module.exports = { - name: 'remove', - description: 'Remove RSS Source', - execute(message, args) { - try { - if (args.length < 1) { - message.reply(`Please use in '${process.env.prefix}remove [title]' format`); - return; - } - var title = args[0]; + data: new SlashCommandBuilder() + .setName('remove') + .setDescription('Remove an RSS source by it\' title') + .addStringOption(option => + option.setName('title') + .setDescription('The title of the source to remove') + .setRequired(true)), + example: "remove ['Leafly']", + isPrivileged: false, + requiresTokens: false, + async execute(interaction) { + try{ + var title = interaction.options.getString("title"); libCore.deleteSource(title, (err, result) => { console.log("Result from removing entry", result); if (result) { - message.reply(`Removing ${title} from the list of RSS sources`); + interaction.reply(`Removing ${title} from the list of RSS sources`); } 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(); - }); - } catch (err) { - console.log(err); - message.reply(err.toString()); - } - } + }); + }catch(err){ + log.ERROR(err) + interaction.reply(err.toString()); + } + } }; \ No newline at end of file diff --git a/commands/slang.js b/commands/slang.js index a2bca4c..031d9a6 100644 --- a/commands/slang.js +++ b/commands/slang.js @@ -1,5 +1,33 @@ 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 = { name: 'slang', description: 'Search Urban Dictionary for a phrase.', diff --git a/commands/sources.js b/commands/sources.js index c6d5dd4..7ed4b3c 100644 --- a/commands/sources.js +++ b/commands/sources.js @@ -1,5 +1,33 @@ 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 = { name: 'sources', description: 'List RSS Sources', diff --git a/commands/update.js b/commands/update.js index bc802db..defb88b 100644 --- a/commands/update.js +++ b/commands/update.js @@ -1,16 +1,23 @@ var libCore = require("../libCore.js"); -module.exports = { - name: 'update', - description: 'Get RSS Source Link', - execute(message, args) { - message.reply(`Loading Feeds from Sources`); - try { - libCore.loadFeeds(); - } catch (error) { - console.log(error); - } - feedArray = libCore.getFeeds(); +const { SlashCommandBuilder } = require('discord.js'); +const { DebugBuilder } = require("../utilities/debugBuilder"); +const log = new DebugBuilder("server", "update"); - } -}; \ No newline at end of file +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()); + } + } +}; diff --git a/commands/weather.js b/commands/weather.js index 0183732..0eabfc7 100644 --- a/commands/weather.js +++ b/commands/weather.js @@ -1,5 +1,41 @@ 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 = { name: 'alert', description: 'Get any current weather alerts in the state specified.',