Added !random feature

This commit is contained in:
gpkvt 2022-08-13 09:10:16 +02:00
parent 053796ffdd
commit 31c319ecd5
4 changed files with 56 additions and 1 deletions

1
.gitignore vendored
View file

@ -2,3 +2,4 @@ config.yml
build build
tts.spec tts.spec
tts.exe tts.exe
random*.txt

View file

@ -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. 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 ## [1.1.0] - 2022-08-12
### Added ### Added

View file

@ -168,7 +168,13 @@ Additional commands (broadcaster and mods only) are:
### Additional features ### 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 ## Build

34
tts.py
View file

@ -20,10 +20,12 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
""" """
import os
import sys import sys
import time import time
import json import json
import socket import socket
import random
import logging import logging
import datetime import datetime
import webbrowser import webbrowser
@ -198,6 +200,38 @@ class IRC:
return True 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'): if msg.startswith('!quickvote'):
logging.info("!quickvote command detected") logging.info("!quickvote command detected")
if self.quickvote: if self.quickvote: