From bdcbb7f82410d6b1ebe33b79a97ec17ad15d1f09 Mon Sep 17 00:00:00 2001 From: Sepp J Morris Date: Wed, 14 Oct 2020 18:57:25 +0200 Subject: [PATCH] forgot to add this example commit, last commit. Now it's your turn - Update joke.json, joke.js, and zoizbot.njsproj --- zoizbot/commands/fun/apiconfigs/joke.json | 11 ++++++ zoizbot/commands/fun/joke.js | 47 +++++++++++++++++++++++ zoizbot/zoizbot.njsproj | 7 ++++ 3 files changed, 65 insertions(+) create mode 100644 zoizbot/commands/fun/apiconfigs/joke.json create mode 100644 zoizbot/commands/fun/joke.js diff --git a/zoizbot/commands/fun/apiconfigs/joke.json b/zoizbot/commands/fun/apiconfigs/joke.json new file mode 100644 index 0000000..3712a27 --- /dev/null +++ b/zoizbot/commands/fun/apiconfigs/joke.json @@ -0,0 +1,11 @@ +{ + "method": "GET", + "hostname": "joke3.p.rapidapi.com", + "port": null, + "path": "/v1/joke?nsfw=false", + "headers": { + "x-rapidapi-host": "joke3.p.rapidapi.com", + "x-rapidapi-key": "589805bc00msh85fd454fda665e8p11475cjsnacbc1293e0a5", + "useQueryString": true + } +} \ No newline at end of file diff --git a/zoizbot/commands/fun/joke.js b/zoizbot/commands/fun/joke.js new file mode 100644 index 0000000..bc91bd9 --- /dev/null +++ b/zoizbot/commands/fun/joke.js @@ -0,0 +1,47 @@ +exports.run = async (zoizbot, message, args) => { + const Discord = require('discord.js'); // for some reason the jokeembed build needs discord.js defined again. whatever + var http = require("https"); + + const jokeconfig = require("./apiconfigs/joke.json"); // keep the code clean and save the variables in a config file + + const jokeloader = new Discord.MessageEmbed() // a litle feedback for if the api is slow, or the server's connection. with a nice embed for the loading joke message + .setColor('#0099ff') + .setDescription("Requesting a funny joke...") + + const joke = await message.channel.send(jokeloader); // send the loading embed + + var req = http.request(jokeconfig, function (res) { // requesting the "joke" API and pleuring it in a chunk + var chunks = []; + + res.on("data", function (chunk) { + chunks.push(chunk); + }); + + res.on("end", function () { + const body = Buffer.concat(chunks); + //console.log(body.toString()); // just a debug to see if the api be spitting the joke. Only logging body would lead to unreadable bytes, so we use toString. Simple and easy! + const jokebody = (JSON.parse(body.toString())); // i cant belive a JSON.parse worked actually // i like saving every thing in a const, i wish that worked with my parents marriage... + + //message.channel.send(jokebody.content); // Check if our joke JSON parses correctly by only displaying the "content". + + // the jokes arent that funny so lets make it goooooodlooking with a simple embed we also used in embeddedkick example + + const jokeembed = new Discord.MessageEmbed() + .setColor('#0099ff') + .setTitle('Joke API') + .setURL('https://rapidapi.com/LemmoTresto/api/joke3/details') + .setDescription(jokebody.content) + .addFields( + { name: 'Joke upvotes', value: jokebody.upvotes, inline: true }, + { name: 'Joke downvotes', value: jokebody.downvotes, inline: true }, + ) + .setTimestamp() + .setFooter(`joke API example - Joke id: ${jokebody.id}`); + + joke.edit(jokeembed); // finally edit the jokes content + }); + }); + + req.end(); + +} \ No newline at end of file diff --git a/zoizbot/zoizbot.njsproj b/zoizbot/zoizbot.njsproj index 4ee5d2d..cca16c2 100644 --- a/zoizbot/zoizbot.njsproj +++ b/zoizbot/zoizbot.njsproj @@ -45,6 +45,12 @@ Code + + Code + + + Code + Code @@ -75,6 +81,7 @@ +