From 1a5a7d1ac14e460eaefd4b134a705edb39f633bb Mon Sep 17 00:00:00 2001 From: Abheek Dhawan Date: Fri, 22 Oct 2021 14:06:39 -0500 Subject: [PATCH] Fix config and add slash commands --- config/authors.json | 5 +---- config/slash.json | 37 +++++++++++++++++++++++++++++++++++++ index.js | 32 +++++++++++++++++++++++++++++++- package-lock.json | 43 ++++++++++++++++++++++++++++++++++++++++++- package.json | 1 + 5 files changed, 112 insertions(+), 6 deletions(-) diff --git a/config/authors.json b/config/authors.json index b8c421f..e65e7e4 100644 --- a/config/authors.json +++ b/config/authors.json @@ -1,6 +1,3 @@ [ - { - "tag": "ADawesomeguy#3602", - "id": "745063586422063214" - } + "745063586422063214" ] diff --git a/config/slash.json b/config/slash.json index e69de29..3fa2960 100644 --- a/config/slash.json +++ b/config/slash.json @@ -0,0 +1,37 @@ +[ + { + "name": "joke", + "description": "Returns a joke with a given category", + "options": [ + { + "type": "STRING", + "name": "category", + "description": "The category for the joke (none for any category)", + "default": "false", + "required": "false", + "choices": [ + { + "name": "programming", + "value": "Programming" + }, + { + "name": "miscellaneous", + "value": "Miscellaneous" + }, + { + "name": "dark", + "value": "Dark" + }, + { + "name": "spooky", + "value": "Spooky" + }, + { + "name": "christmas", + "value": "Christmas" + } + ] + } + ] + } +] diff --git a/index.js b/index.js index 49c7896..7b8c87a 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,8 @@ require('dotenv').config(); const Discord = require('discord.js'); const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] }); +const axios = require('axios'); + const slashcommands = require('./config/slash.json'); const authors = require('./config/authors.json'); @@ -12,7 +14,9 @@ client.on("ready", () => { client.on("messageCreate", async message => { if (message.author.bot) return; - if (authors.includes(message.author.tag) && message.content.toLowerCase === "!deploy") { + if (message.channel.type === "DM") return; + if (authors.includes(message.author.id) && message.content.toLowerCase() === "j.deploy") { + console.log("Updating slash commands..."); client.application.commands.set(slashcommands); } if (message.content.toLowerCase().includes("-ass ")) { @@ -26,4 +30,30 @@ client.on("messageCreate", async message => { } }); +client.on("interactionCreate", async interaction => { + if (interaction.isCommand()) { + if (interaction.commandName === "joke") { + jokeCommand(interaction); + } + } +}); + +const jokeCommand = async interaction => { + interaction.deferReply(); + let category; + interaction.options.get("category") ? category = interaction.options.get("category").value : category = "Any"; + axios.get(`https://v2.jokeapi.dev/joke/${category}?blacklistFlags=nsfw,religious,political,racist,sexist,explicit`) + .then(async response => { + const data = response.data; + const jokeEmbed = new Discord.MessageEmbed().setAuthor(`Category: ${data.category}`); + if (data.type === "single") { + jokeEmbed.setDescription(data.joke); + } else if (data.type === "twopart") { + jokeEmbed.setTitle(data.setup); + jokeEmbed.setDescription(`||${data.delivery}||`); + } + await interaction.followUp({ embeds: [jokeEmbed] }); + }); +} + client.login(process.env.TOKEN); diff --git a/package-lock.json b/package-lock.json index 8b91aa6..8646d36 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,8 +7,9 @@ "": { "name": "joker-bot", "version": "1.0.0", - "license": "ISC", + "license": "Apache-2.0", "dependencies": { + "axios": "^0.23.0", "discord.js": "^13.2.0", "dotenv": "^10.0.0" } @@ -96,6 +97,14 @@ "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=" }, + "node_modules/axios": { + "version": "0.23.0", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.23.0.tgz", + "integrity": "sha512-NmvAE4i0YAv5cKq8zlDoPd1VLKAqX5oLuZKs8xkJa4qi6RGn0uhCYFjWtHHC9EM/MwOwYWOs53W+V0aqEXq1sg==", + "dependencies": { + "follow-redirects": "^1.14.4" + } + }, "node_modules/callsites": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/callsites/-/callsites-3.1.0.tgz", @@ -172,6 +181,25 @@ "node": ">=10" } }, + "node_modules/follow-redirects": { + "version": "1.14.4", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.4.tgz", + "integrity": "sha512-zwGkiSXC1MUJG/qmeIFH2HBJx9u0V46QGUe3YR1fXG8bXQxq7fLj0RjLZQ5nubr9qNJUZrH+xUcwXEoXNpfS+g==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, "node_modules/is-obj": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/is-obj/-/is-obj-2.0.0.tgz", @@ -366,6 +394,14 @@ "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=" }, + "axios": { + "version": "0.23.0", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.23.0.tgz", + "integrity": "sha512-NmvAE4i0YAv5cKq8zlDoPd1VLKAqX5oLuZKs8xkJa4qi6RGn0uhCYFjWtHHC9EM/MwOwYWOs53W+V0aqEXq1sg==", + "requires": { + "follow-redirects": "^1.14.4" + } + }, "callsites": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/callsites/-/callsites-3.1.0.tgz", @@ -417,6 +453,11 @@ "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-10.0.0.tgz", "integrity": "sha512-rlBi9d8jpv9Sf1klPjNfFAuWDjKLwTIJJ/VxtoTwIR6hnZxcEOQCZg2oIL3MWBYw5GpUDKOEnND7LXTbIpQ03Q==" }, + "follow-redirects": { + "version": "1.14.4", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.4.tgz", + "integrity": "sha512-zwGkiSXC1MUJG/qmeIFH2HBJx9u0V46QGUe3YR1fXG8bXQxq7fLj0RjLZQ5nubr9qNJUZrH+xUcwXEoXNpfS+g==" + }, "is-obj": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/is-obj/-/is-obj-2.0.0.tgz", diff --git a/package.json b/package.json index c90b247..24a8539 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "author": "", "license": "Apache-2.0", "dependencies": { + "axios": "^0.23.0", "discord.js": "^13.2.0", "dotenv": "^10.0.0" }