A Discord Maths Bot written in Python. designed to give problems from the Kings Maths School Seven Day Maths website. https://www.kcl.ac.uk/mathsschool/weekly-maths-challenge/weekly-maths-challenge.aspx This includes the current weekly challenge, as well as a random problem from their archive.
!weekly
to get the weekly challenge!question
to get a random problem from the archive
- discord - An API wrapper for Discord
- requests - Used for HTTP requests to fetch content from the website
- BeautifulSoup - From
bs4
, used to parse the HTML from the website - randint - From
random
, does what it says on the tin - re - Regular expressions, or regex, used for searching through the HTML
- math - Mathematical functions, used for its
.floor()
function - requests_html - Used for additional HTTP requests and parsing HTML
To know when the weekly challenge has change, the bot relies on a webhook that posts to a Discord channel to trigger the message with the Weekly Challenge in it. This is used because @sevendaymaths only tweets when the challenge has updated, and thus serves as a useful trigger for the bot. Currently, this is done with an IFTTT applet; further details can be found in the Installation guide but details of the applet can be found below.
The recommended way to deploy the bot is using the included Dockerfile. This will automatically set up the enviroment for the bot.
The bot currently is only confirmed to work with Python 3.6. This may change depending on how discord.py
is updated, but it is recommended to use at least Python 3.4, and any higher than Python 3.6 may not function correctly.
Install the required libraries using pip install -r requirements.txt
. This will automatically install known good versions.
Once all the libraries are installed, the webhook and Discord channel must be set up. Create an IFTTT account if you have not already, then create a new applet.
The trigger should be New tweet by a specific user
where the username to watch
is sevendaymaths
. The response should be to Make a web request
with IFTTT's webhook functionality. The URL should be your own Discord webhook URL; information on how to set this up can be found here. Make sure in your Discord server there is a dedicated (ideally private) channel set up, that can be used with this webhook.
The following information should then be filled in about your request:
- Method:
POST
- Content Type:
application/json
- Body:
{ "content":"!post {{Text}}"}
(note: thecontent
can contain anything, so long as it starts with!post
)
Finally, set the enviroment variables that the bot needs.
TOKEN = "XXX" #This is the token from Discord for your bot. This assumes your bot has been created and set up already.
NOTIF_CHANNEL_ID = 'XXX' #This is the ID of the channel that your webhook will send messages to
TARGET_CHANNEL_ID = 'XXX' #This is the ID of the channel you would like the bot to send the message containing the Weekly Challenge to
And you should be good to go! Simply run your program python maths-bot.py
to get your bot online.
This project is licensed under the terms of the MIT license, which can be found in the LICENSE.txt
file in the root of the repository.