From: Georgios Atheridis Date: Fri, 30 Jun 2023 23:38:29 +0000 (+0100) Subject: Add command to control subathon timer X-Git-Url: https://git.atheridis.org/?a=commitdiff_plain;h=efb59cbb1ec107345aea014208c1baa27b05e116;p=ihaspeks%2Faptbot-ihaspeks.git Add command to control subathon timer --- diff --git a/ihaspeks/commands/timer.py b/ihaspeks/commands/timer.py new file mode 100644 index 0000000..0e2e9ec --- /dev/null +++ b/ihaspeks/commands/timer.py @@ -0,0 +1,73 @@ +import json +import os +import random +from datetime import datetime + +import pytz +import tools.smart_privmsg +from aptbot.bot import Bot, Commands, Message +from scripts.subathon import sock + +PERMISSION = 10 +PREFIX = "\\" +DESCRIPTION = "" +USER_COOLDOWN = 0 +GLOBAL_COOLDOWN = 0 + + +COMMANDS_PATH = os.path.dirname(os.path.realpath(__file__)) +PATH = os.path.join(COMMANDS_PATH, "..") + + +def main(bot: Bot, message: Message): + subcommand = message.value.split(" ")[1] + time = message.value.split(" ")[2] + try: + time = int(time) + except ValueError: + tools.smart_privmsg.send_safe( + bot, + message.channel, + "You need to put the number second. For example: \\timer add 300", + ) + return + if subcommand == "add": + add_time(time) + elif subcommand == "set": + set_time(time) + elif subcommand == "now": + now() + else: + tools.smart_privmsg.send_safe( + bot, + message.channel, + "You need to use a valid command. Possible sub commands are 'add' 'set' and 'now'", + ) + return + accepted_path = os.path.join(DATA_PATH, "jokes") + with open(accepted_path, "r") as f: + jokes = [user_id.rstrip() for user_id in f] + joke = random.choice(jokes) + + tools.smart_privmsg.send(bot, message, f"{joke}", reply=message.tags["id"]) + + +def set_time(ts: int): + data = { + "set_ts": ts, + } + sock.send(json.dumps(data).encode()) + + +def add_time(ts: int): + data = { + "add_ts": ts, + } + sock.send(json.dumps(data).encode()) + + +def now(): + data = { + "set_ts": datetime.now(pytz.UTC), + } + sock.send(json.dumps(data).encode())