MOOver.js/helpFunctions.js
ZyLacx d2a87e6f10 MOOver 2.0
Slashcommands added hehehehehe
2022-02-12 14:04:18 +01:00

72 lines
No EOL
1.8 KiB
JavaScript

const axios = require('axios').default;
const Discord = require('discord.js');
const fs = require('fs');
require('dotenv').config();
module.exports = {
randomColor: randomColor,
RNG: RNG,
getGifs: getGifs,
getGifEmbed: getGifEmbed,
getGifWithMessage: getGifWithMessage,
returnPromiseString: returnPromiseString,
writeToFile: writeToFile,
};
function randomColor() {
let color = '#';
for (let i = 0; i < 6; i++) {
const random = Math.random();
const bit = (random * 16) | 0;
color += (bit).toString(16);
}
return color;
}
function RNG(max) {
return Math.floor(Math.random() * max);
}
async function getGifs(gifs) {
return new Promise((resolve) => {
resolve(axios.get(gifs));
});
}
async function getGifEmbed(gifQuery, gifAmount) {
const response = await getGifs(gifQuery);
const gif = response.data.results[RNG(gifAmount)].media[0].gif.url;
const gifEmbed = new Discord.MessageEmbed()
.setImage(gif)
.setColor(randomColor());
return gifEmbed;
}
async function getGifWithMessage(interaction, gifQuery, gifAmount) {
const gifEmbed = getGifEmbed(gifQuery, gifAmount);
try {
return (interaction.user.username + ' headpats ' + interaction.options.getMentionable('who'), gifEmbed);
}
catch {
return (null, gifEmbed);
}
}
async function returnPromiseString(guildMembers) {
return new Promise(() => {
guildMembers.fetch();
});
}
function writeToFile(content, path) {
const jsonString = JSON.stringify(content, null, 4);
let error = null;
fs.writeFile(path, jsonString, 'utf8', (err) => {
if (err) {
error = 'There was an error while updating the birthday list';
}
});
return error;
}