Added !pick command

This commit is contained in:
gpkvt 2022-08-25 01:55:04 +02:00
parent 43e6cf5e54
commit e7ab7e0bf1
5 changed files with 110 additions and 8 deletions

View File

@ -2,6 +2,12 @@
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.6.0] - 2022-08-25
### Added 1.6.0
* `!pick` command
## [1.5.0] - 2022-08-24
### Added 1.5.0

View File

@ -70,6 +70,9 @@ messages:
votenobody: "Nobody casted a vote. :("
voteresult: "Voting has ended. The result is:"
votes: "Votes"
pickstart: "Pick started. Send #pickme to participate."
pickresult: "Pick ended. The results are:"
picknone: "Nobody was picked. :("
quotenotfound: "Sorry, no quote found."
quoteaddedprefix: "Quote:"
quoteaddedsuffix: "added."
@ -124,11 +127,14 @@ Please note that the `oauth_token` is valid for approximately 60 days. If it bec
* `whitelist`: The bots reply if `whitelist` is set and user isn't on the list.
* `ready`: The bots init message
* `says`: Prefix to add between username and message
* `votestart`: Message when a quickvote is started.
* `voteend`: Message if a quickvote ends.
* `votenobody`: Message if quickvote ends, but nobody has voted.
* `votestart`: Message when a quickvote was started.
* `voteend`: Message when a quickvote ends.
* `votenobody`: Message when quickvote ends, but nobody has voted.
* `voteresult`: Prefix for the result (will be read out).
* `votes`: Suffix to vote count.
* `pickstart`: Message when `!pick` was started.
* `pickresult`: Message when `!pick` ends.
* `picknone`: Message if nobody was picked.
* `quotenotfound`: Message if requests quote wasn't found.
* `quoteaddedprefix`: Prefix for `Quote <number> added` message.
* `quoteaddedsuffix`: Suffix for `Quote <number> added` message.
@ -210,6 +216,10 @@ The `!addquote` command adds a new line to `quotes.txt`. It expects two paramete
The `!wiki` command searchs the wikipedia and fetches the first three sentences of the result.
#### !pick
The `!pick` command will randomly pick a given number of participants. After `!pick <max number of participants>` was started by the broadcaster or a moderator, everyone can type `#pickme` to become the chance to get picked when `!pick` gets triggered again. If no number was given, when `!pick` was frist triggered only one participant will get picked.
## Build
If you prefer to build your own `tts.exe` instead of using the shipped one, you can do as follows:

View File

@ -32,6 +32,9 @@ messages: # Things the bot can send as chat message
votenobody: "Nobody casted a vote. :("
voteresult: "Voting has ended. The result is:"
votes: "Votes"
pickstart: "Pick started. Send #pickme to participate."
pickresult: "Pick ended. The results are:"
picknone: "Nobody was picked. :("
quotenotfound: "Sorry, no quote found."
quoteaddedprefix: "Quote:"
quoteaddedsuffix: "added."

BIN
tts.exe

Binary file not shown.

93
tts.py
View File

@ -55,9 +55,13 @@ class IRC:
self.tts_allowed = []
self.tts_status = conf['TTS_STARTENABLED']
self.quickvote_status = False
self.pick_status = False
self.votemsg = False
self.poll = {}
self.pollcount = 0
self.pickme = []
self.picknumber = 1
self.pickcount = 0
if 'WHITELIST_USER' in conf:
self.tts_allowed = conf['WHITELIST_USER']
@ -156,6 +160,12 @@ class IRC:
self.priviledged_commands(message, tags)
if msg.startswith('#pickme') and self.pick_status is True:
logging.info('Pickme detected')
self.pickcount = self.pickcount + 1
self.pickme.append(user)
logging.debug("pickme %s added", user)
if msg.startswith('#') and self.quickvote_status is True:
logging.info('Quickvote: Cast detected')
self.pollcount += 1
@ -410,9 +420,13 @@ class IRC:
self.tts_denied = []
self.tts_allowed = []
self.quickvote_status = self.quickvote_status
self.pick_status = self.pick_status
self.votemsg = self.votemsg
self.poll = self.poll
self.pollcount = self.pollcount
self.pickme = self.pickme
self.picknumber = self.picknumber
self.pickcount = self.pickcount
def tts(self, msg, tags):
""" !tts command
@ -441,11 +455,6 @@ class IRC:
logging.info('Sending TTS message to raw_queue')
IRC.send_tts_msg(self, msg, tags)
def pick(self, msg):
""" !pick command """
pass
def addquote(self, tags, msg):
""" !addquote command
@ -701,6 +710,76 @@ class IRC:
yaml.safe_dump(cur_yaml, yamlfile)
load_config()
def pick(self, msg):
""" !pick command """
if self.pick_status:
logging.info('Pick stopped')
logging.debug("Got %s participats, wanted %s", self.pickcount, self.picknumber)
try:
if self.pickcount > self.picknumber:
picks = random.sample(self.pickme, self.picknumber)
logging.info('Got more than the requested number of participants')
else:
picks = self.pickme
logging.info('Got less than or exactly the requested number of participants')
converted_picks = [str(element) for element in picks]
joined_picks = " ".join(converted_picks)
except: # pylint: disable=bare-except
joined_picks = False
if joined_picks:
raw_msg = {
"TTS": True,
"msg": conf['MESSAGE']['PICKRESULT'] +" "+ str(joined_picks),
"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']]
IRC.sendmsg(self,
conf['IRC_CHANNEL'], "",
conf['MESSAGE']['PICKRESULT']
)
IRC.sendmsg(self,
conf['IRC_CHANNEL'], "*",
joined_picks
)
else:
IRC.sendmsg(self,
conf['IRC_CHANNEL'], "*",
conf['MESSAGE']['PICKNONE']
)
self.pick_status = False
self.pickme = []
self.pickcount = 0
return
logging.debug('Pick started')
self.pick_status = True
try:
msg = msg.split(' ')[1].strip()
self.picknumber = msg
except IndexError:
self.picknumber = self.picknumber
logging.info("Will pick %s participants", self.picknumber)
IRC.sendmsg(self,
conf['IRC_CHANNEL'], "@chat",
conf['MESSAGE']['PICKSTART']
)
return
def quickvote(self, msg):
""" !quickvote command
@ -1062,6 +1141,10 @@ def load_config():
conf['MESSAGE']['VOTERESULT'] = cfg.get('messages', {}).get('voteresult', "Voting has ended. The result is:")
conf['MESSAGE']['VOTES'] = cfg.get('messages', {}).get('votes', "Votes")
conf['MESSAGE']['PICKSTART'] = cfg.get('messages', {}).get('pickstart', "Pick started. Send #pickme to participate.")
conf['MESSAGE']['PICKRESULT'] = cfg.get('messages', {}).get('pickresult', "Pick ended. The results are:")
conf['MESSAGE']['PICKNONE'] = cfg.get('messages', {}).get('picknone', "Pick ended. Nobody was picked.")
conf['MESSAGE']['QUOTE_NOT_FOUND'] = cfg.get('messages', {}).get('quotenotfound', "Sorry, no quote found.")
conf['MESSAGE']['QUOTE_ADDED_PREFIX'] = cfg.get('messages', {}).get('quoteaddedprefix', "Quote:")
conf['MESSAGE']['QUOTE_ADDED_SUFFIX'] = cfg.get('messages', {}).get('quoteaddedsuffix', "added.")