diff --git a/.gitignore b/.gitignore index 1b5ac8b..614de36 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ config.yml build tts.spec tts.exe +random*.txt diff --git a/CHANGELOG.md b/CHANGELOG.md index e7bef18..3ba5054 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,20 @@ All notable changes to this project will be documented in this file. If there is a `Changed` section please read carefully, as this often means that you will need to adapt your `config.yml`, otherwise the bot might fail to start. +## [1.2.0] - 2022-08-13 + +### Added + + * `!random` feature (see README.md for details) + +### Changed + + * The vote result will be read out + +### Fixed + + * Improved handling of missing config values. + ## [1.1.0] - 2022-08-12 ### Added diff --git a/README.md b/README.md index 61edb41..129d8a7 100644 --- a/README.md +++ b/README.md @@ -168,7 +168,13 @@ Additional commands (broadcaster and mods only) are: ### Additional features -The bot also contains a `!quickvote` feature. If a broadcaster or moderator send the `!quickvote` command a vote will be started (or a already running vote will be ended). After a quickvote has been started your community can casts votes by sending a chat message starting with `#`. You can include a message after `!quickvote` (e.g. `!quickvote Is pizza hawaii any good? #yes/#no`). If you do so, this message will be repeated every 60 seconds, so everyone keeps in mind, that a vote is still active. +#### !quickvote + +The `!quickvote` feature implements a simple vote system. If a broadcaster or moderator send the `!quickvote` command a vote will be started (or a already running vote will be ended). After a quickvote has been started your community can casts votes by sending a chat message starting with `#`. You can include a message after `!quickvote` (e.g. `!quickvote Is pizza hawaii any good? #yes/#no`). If you do so, this message will be repeated every 60 seconds, so everyone keeps in mind, that a vote is still active. + +#### !random + +The `!random` command will read a random line from a file called `random.txt`. You can also use multiple files, if you call `!random foo` the bot fetch the random line from a file called `random_foo.txt` instead of `random.txt`. `!random bar` will use the file `random_bar.txt` and so on. The `!random` command is restricted to the broadcaster and moderators. ## Build diff --git a/tts.py b/tts.py index 645f689..dc33271 100644 --- a/tts.py +++ b/tts.py @@ -20,10 +20,12 @@ along with this program. If not, see . """ +import os import sys import time import json import socket +import random import logging import datetime import webbrowser @@ -198,6 +200,38 @@ class IRC: return True + if msg.startswith('!random'): + logging.info('!random command detected') + + randomfile = msg.replace('!random', '').strip().lower() + if randomfile: + randomfile = "random_"+str(os.path.basename(randomfile))+".txt" + else: + randomfile = "random.txt" + + try: + with open(randomfile,"r", encoding="utf-8") as file: + lines = file.read().splitlines() + random_msg = random.choice(lines) + except FileNotFoundError: + logging.error('%s not found', randomfile) + return False + + raw_msg = { + "TTS": True, + "msg": random_msg, + "badges": True, + "subscriber": True, + "msgid": True, + "user": conf['IRC_USERNAME'], + "length": conf['IRC_TTS_LEN'], + "queuetime": datetime.datetime.now(), + "timestamp": str(time.time_ns()) + } + msg_queue[raw_msg['timestamp']] = [raw_msg['user'], raw_msg['msg']] + + return True + if msg.startswith('!quickvote'): logging.info("!quickvote command detected") if self.quickvote: