Skip to content

Commit

Permalink
[Enhancement] exported repeated api requests to a function, fixed a b…
Browse files Browse the repository at this point in the history
…ug with insult when if a user is not mentioned it returns null
  • Loading branch information
naseif committed Sep 1, 2021
1 parent cd24b28 commit a931beb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
24 changes: 7 additions & 17 deletions commands/fun/insult.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const { SlashCommandBuilder } = require("@discordjs/builders");
const fetch = require("node-fetch");

const { requestAPI } = require("../../modules/requestAPI");
module.exports = {
data: new SlashCommandBuilder()
.setName("insult")
Expand All @@ -11,23 +10,14 @@ module.exports = {
async execute(interaction, client) {
const user = interaction.options.getUser("user");
await interaction.deferReply();

async function getInsult() {
try {
const requestInsult = await fetch(
"https://evilinsult.com/generate_insult.php?lang=en&type=json"
);
const responseToJson = await requestInsult.json();
return responseToJson;
} catch (err) {
throw err;
}
}

try {
const insult = await getInsult();
const insult = await requestAPI(
"https://evilinsult.com/generate_insult.php?lang=en&type=json"
);

await interaction.followUp(`${user}, ${insult.insult}`);
await interaction.followUp(
`${user ? user : `${interaction.member.toString()}`}, ${insult.insult}`
);
} catch (error) {
await interaction.followUp(
`Couldn't retrieve Insult, I blame <@503264757785165851>`
Expand Down
1 change: 0 additions & 1 deletion commands/misc/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,3 @@ module.exports = {
await interaction.followUp({ embeds: [embed] });
},
};
// .addField(`:calendar: Created:`, '```' + `${serverinfo.created} \n ${serverinfo.ago}` + '```', true)
11 changes: 11 additions & 0 deletions modules/requestAPI.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const fetch = require("node-fetch");

module.exports.requestAPI = async (url) => {
try {
const request = await fetch(url);
const responseToJson = await request.json();
return responseToJson;
} catch (err) {
throw err;
}
};

0 comments on commit a931beb

Please sign in to comment.