TriviaBot

A script to listen to chat messages and respond to a specific prefix

Author
BadNintendo
Daily installs
0
Total installs
5
Ratings
0 0 0
Version
0.1
Created
2023-05-14
Updated
2023-05-14
Size
18.5 KB
License
MIT
Applies to

Basic Trivia Bot functionality.

1. **Chatbox Initialization**: The code starts by initializing variables and selecting the chatbox element and input area of the chat interface. It also retrieves the username of the bot itself.

2. **User Join Handling**: The `handleUserJoin` function is responsible for greeting users when they join the chat room. It listens for user join events and extracts the username from the event message, then calls the `greetUserOnJoin` function to greet the user.

3. **Greeting Handling**: The `handleGreeting` function is triggered when a user sends a message that starts with the prefix "!" (exclamation mark). It checks if the message is a specific command, such as asking for a random greeting or an "8ball" question. If it's a "hello" command, it sends a random greeting if enough time has passed since the last greeting to avoid spamming.

4. **Sending Chat Messages**: The `sendChatMessage` function is used to send a chat message. It checks if the input area is empty, waits for it to become available, sets the message text, and triggers the send button click event.

5. **8ball Question Handling**: The `handleEightBallQuestion` function handles the "8ball" command. If the command is asking for available responses, it sends a message listing the available responses. Otherwise, it randomly selects a response from the available responses and sends it as a chat message.

6. **Mutation Observer**: The code sets up a mutation observer to listen for new chat messages in the chatbox. When a new message is added, the observer triggers the `handleUserJoin` and `handleGreeting` functions to handle user join events and greetings.

7. **Trivia Question Handling**: The code defines an array of trivia questions with their options and answers. The `handleTriviaQuestion` function is responsible for selecting and asking a new trivia question. It chooses a random question from the array, avoids asking the same question consecutively, and sends the question as a chat message with the available options.

8. **Answer Handling**: The `handleAnswer` function is triggered when a user sends a message. It checks if it's the bot itself or if games are disabled. It handles user answers to the trivia question, validating the answer, keeping track of attempts and timing, and awarding points for correct answers. It also handles penalties for incorrect answers after multiple attempts.

9. **Leaderboard**: The code introduces a leaderboard functionality by defining the `points` object to store the points earned by each user. The `updateLeaderboard` function is responsible for updating and displaying the leaderboard based on the current points. It sorts the users based on their points and generates a leaderboard message.

10. **Bonus Rounds**: Bonus rounds are introduced to the trivia game. The `isBonusRound` flag is used to indicate if it's a bonus round, and the `handleTriviaQuestion` function randomly determines if it's a bonus round. If it is, the points for the correct answer are doubled, and a message is sent to inform users that it's a bonus round.

11. **Interval for Asking Questions**: The code sets an interval using `setInterval` to call the `handleTriviaQuestion` function at a specified interval (default every 5 minutes). This ensures that a new trivia question is asked periodically.

That's a detailed breakdown of the bot's functionality. It includes greeting users on join, responding to specific commands, asking trivia questions, handling user answers, maintaining a leaderboard, and introducing bonus rounds to enhance the gameplay experience.


To enable or disable a feature from responding change to true or false.
const enableCommands = false;
const enableGames = true;
const enableGreet = false;