DEV Community

Cover image for Dicord bot
MOHSIN ALI SOOMRO
MOHSIN ALI SOOMRO

Posted on

Dicord bot

Requirements Pkgs

Nodejs 16.10
axios 
env
discord.js 
Enter fullscreen mode Exit fullscreen mode

Import packages on top

Image description

Initialize discord client

Image description

Ready discord

Image description

get memes (axios call)

Image description

Discord message

Image description

Login

Image description

Run your code

node index.js
Enter fullscreen mode Exit fullscreen mode

Complete code 💻

require('dotenv').config(); //initialize dotenv
const {Client,Intents} = require('discord.js'); //import discord.js
const axios =require('axios')

const client = new Client({intents:[Intents.FLAGS.GUILDS,Intents.FLAGS.GUILD_MESSAGES]}); //create new client

client.on('ready', () => {
    console.log(`Logged in as ${client.user.tag}!`);
  });
  async function getMeme(){
    const res = await axios.get('https://meme-api.herokuapp.com/gimme');
    return res.data.preview[0];
  }

  client.on('message', async msg => {
    switch (msg.content) {
      case "ping":
        msg.reply("Pong!");
        break;
      //our meme command below
      case "!meme":
        msg.channel.send("Here's your meme!"); //Replies to user command
        const img = await getMeme(); //fetches an URL from the API
        msg.channel.send(img); //send the image URL
        break;
     }
  })

  //make sure this line is the last line
client.login(process.env.CLIENT_TOKEN); //login bot using token


Enter fullscreen mode Exit fullscreen mode

Thanks for reading...! 😃

Top comments (0)