Skip to content

A customizable Discord bot based on discord.js v14 with Typescript!

License

Notifications You must be signed in to change notification settings

IronBatman2715/DiscordTS-Bot

Repository files navigation

DiscordTS-Bot

A customizable Discord bot based on discord.js v14 with Typescript!


Setup

  1. Install the following software.
  1. Create your bot application on the Discord developer portal

    It is highly recommended to use separate discord bot applications for development and production. It can be difficult to differentiate global (production) commands from guild (development) commands in the discord client.

  2. Setup your database of choice.

    • MongoDB is the default. You can easily set up a free instance with MongoDB Atlas.

    • If you want to use a different database, see here for databases supported by Prisma. This will also require some changes to prisma/schema.prisma.

  3. Download this repository (clone/fork/raw download).

From here, follow the steps for your desired setup:

Development environment

  1. Rename/copy sample.dev.env to dev.env and set the environment variables as defined in src/global.d.ts

    • DISCORD_TOKEN: Discord bot token (acquired in step 1).

    • DB_URL Database URL

    • CLIENT_ID Discord bot client ID (acquired in step 1).

    • TEST_GUILD_ID guild ID of the server you will use to test this bot.

      • Slash commands will immediately be updated for this server and this server only upon restarting the bot in developer mode.
  2. Run npm i to install dependencies and generate Prisma client files.

  3. Run npm run dev to start a developer environment instance!

    • Running this for the first time will generate config.dev.json if not present.

    • Edit and save any of the files in the src directory and the development environment will automatically restart to reflect the changes!

Production environment

  1. Rename/copy sample.env to .env and set the environment variables as defined in src/global.d.ts

    • DISCORD_TOKEN: Discord bot token (acquired in step 1).

    • DB_URL Database URL

    • CLIENT_ID Discord bot client ID (acquired in step 1).

  2. Run npm i to install dependencies and generate Prisma client files.

  3. Run npm run build to transpile the source code for production.

    • Running this for the first time will generate config.json if not present.

    • If this is the first time you have run the bot or you are updating the source code, run npm run commands:registerafter transpilation to register all your commands to any and all servers this bot is in. You may need to run npm run commands:resetProd prior to npm run commands:register if some old commands are not being removed (Discord states that their servers can take up to an hour to reflect these changes. In experience this takes only a couple minutes).

  4. With transpilation complete, you can now delete the node_modules folder. Then, run npm ci --production to install only the dependencies needed for production, omitting the dependencies only needed for transpilation/development.

    • Changing/updating any files other than .env or config.json will require you to redo steps 6 and 7.
  5. Change the config.json file to your liking. See information about how you can change it here.

  6. Run npm start to run the bot!

  7. [Optional] Add PM2 or containerize with Docker. Then, deploy to a cloud service (Heroku, Linode, Vultr, etc.) or on your own hardware.

Configuration file properties

  • name: name of the bot (displayed in some commands).

  • activities: list of activities the bot will randomly cycle through as its current activity (Exs: "Watching The Fellowship of the Ring", "Listening to Never Gonna Give you Up", etc.).

    • name: string after the type.

    • type: string that can only be one of the following values (case-sensitive).

      • playing = Playing name

      • streaming = Streaming name

      • listening = Listening to name

      • watching = Watching name

      • competing = Competing in name

    • url: (optional) url link to stream. Only include when type is streaming.

References

I did not create the icons used. They are sourced from here under their license.