Skip to content

GetUsernameFromDatabase/discord-server-bot

Repository files navigation

Discord Server Bot

UNSTABLE: Due to conversion to typescript and dependencies major version changes I also haven't had the time to work on this much of late

This is a discord server bot made for personal use
aka usage for multiple servers is not prioritized during development and testing

This bot:

  • fetches giveaways
  • plays music

Table Of Contents

TODO

  • rework my sql builder with https://github.com/kysely-org/kysely (or something like that) for a more suistanable solution
  • refocus to work with multiple servers, current focus is on just my own personal server
  • docker containerization
  • update music bot
  • see if it is possible to have customizable rights
    • should be able to have minimum rights needed and give more if wanted by developer, just some checks are required before initiating action
  • tests for parts that are uncoupled from discord client
  • redo this readme -- outdated information
  • hide some useful nuggets like
    • npm exec -- tsc --showConfig > tsconfig.final.json

Setup

You can follow a guide to get started

Setup your bot application in the Discord developer portal

Install yarn -- guide

Continous Run

If you want to have it running continously I reccomend pm2

npm install --global pm2

Install pm2-logrotate to keep the log files limited size

pm2 install pm2-logrotate

This project has setup pm2 config file so you can easily get started using the following command

NB: this only works on linux systems

pm2 start pm2.config.cjs

Setup environment variables

For environment variables I use dotenv

example .env

DISCORD_CLIENT_ID=XXX
DISCORD_CLIENT_TOKEN="XXX"
DISCORD_CLIENT_PUBKEY="XXX"

DISCORD_GUILD_ID=XXX
TEST_CHANNEL_ID=XXX
GIVEAWAYS_CHANNEL_ID=XXX
DEV=FALSE

The following environment variables are required to run the application:

  • DISCORD_CLIENT_ID: The client ID of your Discord bot.

  • DISCORD_CLIENT_TOKEN: The token of your Discord bot.

  • DISCORD_CLIENT_PUBKEY: The public key of your Discord bot.

  • DISCORD_GUILD_ID: The ID of the Discord server (guild) that the bot will operate in.

  • TEST_CHANNEL_ID: The ID of the channel in the Discord server used for testing.

  • GIVEAWAYS_CHANNEL_ID: The ID of the channel in the Discord server where giveaways will be held.

  • DEV: Set to TRUE if running the bot in a development environment, otherwise set to FALSE.

    • Can be set via command line.

Make sure to replace the placeholder values (XXX) with your actual values before running the application.

Tests

TODO: Fix problems that have occured since switching to typescript and upgrading packages

For testing I use Jest testing framework
Tests can be run with yarn test

Commands

Commands are made using slash-create -- makes it easy to handle and create discord slash commands

Giveaways

Giveaway Commands

Fetching

This bot mainly tries to get giveaways from https://grabfreegames.com/
Should that fail it will then try to get them from GrabFreeGames steam group page

Conversion

Fetched html will be turned into a usable format using Cheerio and Turndown - used to convert HTML into MD.
Resulting string will be turned into a MessageEmbed.

Conversion Result

  • URL used during fetch will become the title url to link the source
  • Border colour of MessageEmbed is the same colour that the bot has in the Server's member list
  • Image is only displayed if the source has it

GrabFreeGames_EmbedExample

Posting Giveaways

When the bot runs giveaways will be posted into the giveaway channel.
The bot will check if a MessageEmbed with the same title can already be found in the channel. This check can be disabled but is on by default - used when sending messages into a channel in bulk.

Before the previous check, giveaways that have been sent will be filtered out using a json file.
This is used since the previous check only compares against last 100 messages sent in the channel.
This filtering won't happen if giveaway channel was changed

This file will be generated in the data folder

Memes

Meme Commands

TODO: Plan is to have this bot send memes taken from reddit

Music

Music Commands

For music bot functionality I use discord-player which handles the nitty gritty.

Useful Links

Regex101 -- extremely helpful in the making of regular expressions.

Discord.js guide -- starting the project from the ground up

Discord.js documentation

PM2 documentation

slash-create documentation

Go to the table of contents