A Simple Twitch Bot, that regularly posts questions into a twitch channel that can be answered by the viewers.
This bot was built for the streamer https://twitch.tv/HansiPowers
This is also the reason all text, that is posted to the chat is in german.
When implementing multiple languages it is a requirement to always have german as a supported language.
New features can freely be tested in the chat of https://twitch.tv/dota_quiz
Make sure NodeJS
and npm
are installed on your pc
When starting the bot in a dev environment (where is constantly restarted and does not have to run as a daemon), execute the following commands:
npm install
(Installs all dependencies)
npm start:dev
This will start the bot in a blocking command which can be terminated to stop the bot.
All logs are written to the log files and nothing is printed to the console.
Create a file token
in the main directory that holds the oauth token for the user that posts in twitch chat.
Make sure NodeJS
and npm
are installed on your pc
To have the bot run as a service in production execute the following commands:
npm install
(Installs all dependencies)
npm run setup-system-startup
This will print a command that should be executed to have the service be in the autostart of the machine
npm start
This will start the service in the background where all logs are printed to te corresponding log files.
These additional commands can be used to manage the service:
npm stop
npm restart
npm uninstall
For additional monitoring check https://pm2.keymetrics.io/docs/usage/pm2-doc-single-page/
Create a file token
in the main directory that holds the oauth token for the user that posts in twitch chat.
Important files:
config/config.json
: The general configuration file, that holds most propertiesconfig/question.json
: Holds all questions and its answerslang/*
: Configuration files for all supported languages
- Before usage, a twitch account, that should post the messages has to be created. After that, a login token has to be created for that user, that is used by this bot to login. Note that removing the token from the twitch account will remove all access for the bot. No need to change the account password.
A token can be created using this link: https://twitchapps.com/tmi/ - After creating the token, write the username of the account into the configuration file under
identity.username
and write the token into a text file namedtoken
in the base folder - Set the
channelName
to the channel the bot should post into
π Properties with this icon can be changed in the configuration file
Note, that all commands are case-insensitive, meaning, that for example #score
and #SCOre
would do the same
- When running, the bot has to be started to post questions using the command
#startQuiz
π - It can always be disabled again using
#stopQuiz
π, at which point it will only react to commands but will not post anything on its own - Note, that start and stop are only available to a channel admin who can be configured in the config under
channelAdmin
- When enabled in the configuration, the bot will automatically disable itself when the stream of the configured channel is offline
- The bot will post questions into the chat every 60seconds π
- When the question is not answered correctly within 20seconds π, it "expires" with a notification in chat
- Chat users can try to answer the question by prefixing the chat answer with
?
π. For example:?110mana
- When sending a wrong answer the bot will not react (:pushpin:: It can be configured to send a "wrong answer" notification into the chat)
- When correctly answering a question, the user gets a message in chat and gets a score point. Also, the question immediately expires so no other chat used can answer it.
- The use score for each correct answer is saved and can be accessed later
#score
π: Sends a message with the current score of the user, that sent the command#topTen
π: Prints the scores of the ten users with the most points#frage
π: Resends the current question
#startQuiz
π: Starts the bots feature to post questions in chat#stopQuiz
π: Stops the bots feature to post questions in chat. It will still react to all commands (user or admin)#totalScore
π: Prints the current scores for ALL users, that have collected points to the chat#reset
π: Resets the saved points for ALL users. Prints them all to the chat before clearing it
The name of the twitch channel, the bot should post its messages and react to commands
The name of the twitch user, that is able to execute the Admin Commands
When enabled, the quiz is automatically stopped when stream is offline
The interval in which questions should be posted in seconds
The time a question expires after it has been posted in seconds
When a question was posted it goes on "cooldown" and this percentage of other question have to be posted before this question can come up again
The prefix, that is required on a chat message for it to be recognized as a potential answer
When enabled, a message is sent when a user sent a wrong answer
When enabled, a message is sent when a user asks for the current question or tries to answer it, but no answer is currently active
The name of the twitch account, that is used by the bot to send and receive messages
See Admin Commands #totalScore
Currently all questions are build like this:
- A question is just a simple text with no variables or anything similar.
For example: "Welchen Cooldown hat Snapfires Mortimer Kisses?" - A question have multiple answers where each answer is correct.
A user has to given any of these answers to be correct.
For example:110
,110s
- When checking if an answer is correct it is compared with all configured correct answers.
When doing this all whitespaces are removed from the answer and the answer is case-insensitive.
For example: The answer110 S
still matches the answer110s
- This also means, when setting up questions, the answers should not contain any whitespaces and all text should be in lower case
These are the texts, that are posted into the chat as described.
Most messages can have variables, that are replaced when the message is posted into the chat.
A variable can be used in a message by surrounding it with ${
and }
A new question is asked or a user sent the command to see the question
Variables:
question
: The actual question, that is askedtimeout
: The timeout for the current questionanswerPrefix
: The prefix, that has to be used when answering the question. See General Features for details
A user asked for the current question or tried to answer a question, when none is currently open.
This message can be disabled completely
Variables:
user
: The user, that sent the message
The wrong answer was given for a question
This message can be disabled completely
Variables:
user
: The user, that sent the message
The correct answer was sent for a question
Variables:
user
: The user, that sent the messagenewQuestionIn
: The time until the next question will be asked
The question timed out and can not be answered anymore
Variables:
question
: The full question, that has just timed outanswer
: One valid answer for the questionnewQuestionIn
: The time until the next question will be asked
Send the total score of the user, that send the command
Variables:
user
: The user, that sent the messagescoreNumber
: The score of the useruserRank
: The rank of the current compared to all others
Sent when the scores of multiple users are printed in a single chat message
Variables:
user
: The user, that sent the messagescoreNumber
: The score of the useruserRank
: The rank of the current compared to all others
Message confirming the reset of all scores
Message when loading all scores, but nobody has scores yet
The text used when sending hours in a message
The text used when sending minutes in a message
The text used when sending seconds in a message