A discord bot to generate inspiring and comforting quotes , part of the Hlin project
To run the bot, you will need the following:
-
A bot token from the discord developer API
-
Python 3.10 installed on your system
-
A MongoDB cluster and/or MongoDB Shell for testing
-
A connection string for your cluster in the format :
- "mongodb+srv://
username
:password
@cluster-name
mongodb.net/?retryWrites=true&w=majority"
- "mongodb+srv://
Helpfull Ressources
To run the bot:
- Create a Python virtual environment and install the dependencies listed in requirements.txt
$ python3 -m venv .virtual-env
$ source .virtual-env/bin/activate
$ pip install -r requirements.txt
- Run the
setup.py
file to automaticaly create and writeconfig.json
and.env
files
$ python3 setup.py
-
You will need to provide your
Discord Bot token
,Mongodb Connection Link
andGoogle Perspective API Key
-
Run the
feed_database.py
file to get quotes from the zenquotes API (make sure to read the docs) or any other API (which may require further configuration).
$ python3 feed_database.py
- And start the Bot
$ python3 main.py
If you plan to host the bot with replit.com, note that the Replit servers shut down if 30 minutes pass without them being pinged. To work around this, you can start a Flask server with the keep_alive.py
file and use a website like Uptime Robot that will ping your server periodically.
By default, a database called quotes
will be created, and the quotes will be stored in a collection called quotes_collection
.
The quotes are structured as follows:
{
"_id": { "$oid": "63f4c572798e1cdb2613fd04" },
"author": "Walt Whitman",
"quote": "Simplicity is the glory of expression. ",
"date": "23:02:21:13:21:54"
}
When retrieving a quote from the database, a random quote is selected, and only the author
and quote fields
are retrieved.
When feeding a quote to the database, the author,
quote, and
date``` fields are required. The document to insert should look like:
doc = { "author": quote_author, "quote": quote_text, "date": now }
The bot has three commands stored in cogs/quote_cogs.py
:
-
inspire
: grab a quote from the database.- Calls the
utils.get_quote_from_db()
function. - Returns a
string
structured like:quote - author
. - Sends the quote string to the user.
- Calls the
-
add_quote
: post a quote to the database.- Calls the
perspective_client.analyze_quote(quote)
function. - Returns a toxicity score.
- Calls the
utils.post_quote(quote, username)
function and sends the quote to the database based on the toxicity score.
- Calls the
-
daily_quotes
: sends a daily quote at a specified hour.- Calls the
utils.setup_daily_quotes(interaction, time)
function with the provided time inHH:MM
format passed by the user. - Updates or creates if not exist the user's configuration stored in a
config.json
file. - The
utils.start_scheduled_task()
function starts on the bot'son_ready()
event. - Reads the configs and triggers the
utils.send_daily_quote_to_user(user_id, bot)
function. - Sends the quote to the user at the specified GMT time.
- Calls the