DEV Community

Cover image for How to Create a Discord Bot to Get GitHub Repository Issues
Danilo Assis
Danilo Assis

Posted on

How to Create a Discord Bot to Get GitHub Repository Issues

Discord is a popular platform for communication among teams and communities, and it also provides APIs for building chatbots that can automate various tasks. In this tutorial, we will create a Discord bot using Node.js that can retrieve all issues from a GitHub repository using the Octokit REST library.

Prerequisites

To follow this tutorial, you should have the following:

  • A Discord account and a Discord server where you have permission to create a bot and add it to the server.
  • A GitHub account and a personal access token that has permissions to access the repository you want to fetch issues from.
  • Node.js and npm installed on your machine.

Creating the Discord Bot

First, we need to create a new Discord bot application and get its token. You can do this by going to the Discord Developer Portal, creating a new application, adding a bot to the application, and copying its token.

Next, we need to install the necessary dependencies using npm. You will need the following packages: discord.js for interacting with Discord's API, octokit/rest for making API calls to GitHub, and dotenv for managing environment variables. You can install these packages by running the following command in your terminal:

npm install discord.js octokit/rest dotenv
Enter fullscreen mode Exit fullscreen mode

Then, create a new JavaScript file and require the necessary modules at the top of the file:

const Discord = require('discord.js');
const { Octokit } = require('@octokit/rest');
require('dotenv').config();
Enter fullscreen mode Exit fullscreen mode

Once the client is logged in, listen for messages and respond to commands. In this case, we want to listen for a specific command (e.g. !get-issues) and respond by fetching all issues from the specified GitHub repository:

client.on('message', async (message) => {
  if (message.content === '!get-issues') {
    const octokit = new Octokit({ auth: process.env.GITHUB_PERSONAL_ACCESS_TOKEN });
    const issues = await octokit.issues.listForRepo({
      owner: 'owner-name',
      repo: 'repository-name',
    });
    message.channel.send(`Found ${issues.data.length} issues in the repository.`);
  }
});
Enter fullscreen mode Exit fullscreen mode

This code creates a new Octokit instance using a personal access token for GitHub authentication, fetches all issues from the specified repository, and sends a message back to the Discord channel with the number of issues found.

Finally, start the bot by running the JavaScript file using Node.js:

node discord-bot.js
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this tutorial, we have learned how to create a Discord bot using Node.js that can retrieve all issues from a GitHub repository using the Octokit REST library. With this knowledge, you can build more complex bots that can automate various tasks and make your team's workflow more efficient.

References

Follow me on Twitter
If you like and want to support my work be my patreon
See more in https://daniloassis.dev.


Photo by Maximalfocus on Unsplash

Top comments (0)