Discord bot made using JavaScript that responds to user messages with a random GIF retrieved from Giphy API. Just thought this was a fun program that isn't very complex.
const https = require("https");
const Discord = require("discord.js");
const client = new Discord.Client({ intents: [Discord.GatewayIntentBits.GuildMessages, Discord.GatewayIntentBits.Guilds, Discord.GatewayIntentBits.MessageContent]});
const QUERY_LIMIT = 25;
require('dotenv').config({path:__dirname+'/./.env'})
// Message create event
client.on("messageCreate", (message) => {
if (message.author.bot) {
return;
}
// Long block of text that makes an API request to Giphy
const msg = message.content;
let url = `https://api.giphy.com/v1/gifs/search?api_key=${process.env.GIPHY_API_KEY}&q=${msg}&limit=${QUERY_LIMIT}`;
https.get(url, (response) => {
data = '';
response.on('data', (d) => {
data += d;
})
// Respond to user message
response.on('end', () => {
let parsed = JSON.parse(data);
if (parsed.data && parsed.data.length > 0) {
// Get a random GIF result from API response
message.reply(parsed.data[Math.floor(Math.random() * parsed.data.length)].embed_url);
} else {
message.reply("no GIF found :(");
}
})
})
})
client.login(process.env.TOKEN);