Methods to hide your bot's token - discordrb/discordrb GitHub Wiki
Don't share your bot's token with anyone!
Your application's token is essentially the password to your bot's account. If this token is shared publicly, then anyone can log into your bot's account and do whatever they like with it.
It is strongly recommended that you take steps to hide your token when hosting an open-source bot on GitHub, or other public platforms, that can be viewed by anyone.
Below are several methods to do this.
1. Configatron
A super cool, simple, and feature rich configuration system for Ruby apps.
- Add
gem 'configatron'to yourGemfileand runbundle installor rungem install configatron. - Create a File called
example.config.rband put following content in it:configatron.token = 'YOUR_TOKEN'. - Copy
example.config.rbtoconfig.rband write your token inconfig.rbbut not inexample.config.rb. - Add
config.rbto your so called.gitignore(This prevents git from tracking the file). - Add
require 'configatron'andrequire_relative 'config.rb'on a new lines in your main project.
A bot init will look like the following:
require 'discordrb'
require 'configatron'
require_relative 'config.rb'
bot = Discordrb::Bot.new token: configatron.token
2. DotENV
Loads environment variables from .env
- Add
gem 'dotenv'to yourGemfileand runbundle installor rungem install dotenv - Create a File called
.env - Edit
.envand put your token with the following syntax in it:TOKEN=YOUR_TOKEN(no spaces) - Add
.envto your.gitignorefile (This prevents git from tracking the file).
A bot init will look like the following:
require 'discordrb'
require 'dotenv/load'
# or
# require 'dotenv'
# Dotenv.load
bot = Discordrb::Bot.new token: ENV['TOKEN']
3. YAML
YAML files are simple text files for storing data in a simple, human-readable format.
You should already have YAML parser; it is part of Ruby.
- Create a file named
example.config.yamlwith the following content:
---
token: YOUR_TOKEN
- copy
example.config.yamltoconfig.yamland insert your own token forYOUR_TOKEN - Add
config.yamlto your.gitignorefile (This prevents git from tracking the file).
A bot init will look like the following:
require 'discordrb'
require 'yaml'
CONFIG = YAML.load_file('config.yaml')
bot = Discordrb::Bot.new token: CONFIG['token']