From f11f90e01d91fc37859c1451c2b0599779ba9c1f Mon Sep 17 00:00:00 2001 From: Yuuki Chan Date: Tue, 20 Feb 2024 00:16:13 +0900 Subject: [PATCH] Add plugins. --- commands.py | 59 +++++++++++++++++++++++++++++++ cookie.py | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 159 insertions(+) create mode 100644 commands.py create mode 100644 cookie.py diff --git a/commands.py b/commands.py new file mode 100644 index 0000000..47e145a --- /dev/null +++ b/commands.py @@ -0,0 +1,59 @@ +import sys + +from src.channel_manager import ChannelManager +from src.plugin_base import PluginBase + + +class Plugin(PluginBase): + def handle_message(self, source_nick, channel, message): + message_parts = message.split() + self.channel_manager = ChannelManager() + if message_parts[0] == '!hello': + self.bot.ircsend(f'PRIVMSG {channel} :Hello, {source_nick}!') + + elif message_parts[0] == '!join': + if len(message_parts) == 0: + self.bot.ircsend(f'PRIVMSG {channel} :Please specify a channel to join') + return + else: + self.channel_manager.save_channel(message_parts[1]) + self.bot.ircsend(f'JOIN {message_parts[1]}') + + elif message_parts[0] == '!part': + if len(message_parts) == 1: + self.bot.ircsend(f'PART {channel}') + self.channel_manager.remove_channel(channel) + else: + self.bot.ircsend(f'PART {message_parts[1]}') + self.channel_manager.remove_channel(message_parts[1]) + + elif message_parts[0] == '!quit': + if len(message_parts) == 0: + quit_message = 'EliteBot!' + else: + quit_message = message[len(message_parts[0]) + 1:] + self.bot.ircsend(f'QUIT :{quit_message}') + self.bot.ircsock.close() + self.bot.connected = False + sys.exit() + + elif message_parts[0] == '!raw': + if len(message_parts) > 1: + if message_parts[1].upper() == 'PRIVMSG' and len(message_parts) > 3: + raw_command = ' '.join(message_parts[1:3]) + " :" + ' '.join(message_parts[3:]) + else: + raw_command = ' '.join(message_parts[1:]) + self.bot.ircsend(raw_command) + + elif message_parts[0] == '!me': + if len(message_parts) > 1: + action_message = ' '.join(message_parts[1:]) + self.bot.ircsend(f'PRIVMSG {channel} :\x01ACTION {action_message}\x01') + else: + self.bot.ircsend(f'PRIVMSG {channel} :Please specify an action') + + elif message_parts[0] == '!ping': + if len(message_parts) > 1: + self.bot.ircsend(f'PRIVMSG {channel} :Pinging {message_parts[1]}') + else: + self.bot.ircsend(f'PRIVMSG {channel} :Please specify a nick to ping') diff --git a/cookie.py b/cookie.py new file mode 100644 index 0000000..8eedb97 --- /dev/null +++ b/cookie.py @@ -0,0 +1,100 @@ +import random +import time +from datetime import datetime + +from sqlalchemy import Table, Column, Integer, String, MetaData, insert, select + +from src.channel_manager import ChannelManager +from src.db import Database +from src.plugin_base import PluginBase + +meta = MetaData() +cookie_table = Table( + 'Cookie', + meta, + Column('id', Integer, primary_key=True, autoincrement=True), + Column('name', String, unique=True, nullable=False), + Column('cookies', Integer, default=0), + Column('last', String, default='1999/01/01 00:00:00'), +) +c_db = Database(cookie_table, meta) + + +class Plugin(PluginBase): + def handle_message(self, source_nick, channel, message): + parts = message.split() + c_db.create_table('Cookie') + self.channel_manager = ChannelManager() + + if parts[0].lower() == '!cookie': + if len(parts) == 1: + self.insert_user(source_nick) + + cookies = c_db.get(source_nick, 2) + rnd = random.randint(1, 10) + last = datetime.strptime(c_db.get(source_nick, 3), '%Y/%m/%d %H:%M:%S') + current = datetime.strptime(datetime.now().strftime('%Y/%m/%d %H:%M:%S'), '%Y/%m/%d %H:%M:%S') + diff = round((current - last).total_seconds() / 60.0) + + if diff >= 30: + c1 = 'no cookies' if rnd == 0 \ + else f'{rnd} cookie' if rnd == 1 \ + else f'{rnd} cookies' + c2 = 'no cookies' if (cookies + rnd) == 0 \ + else f'{(cookies + rnd)} cookie' if (cookies + rnd) == 1 \ + else f'{(cookies + rnd)} cookies' + + self.bot.ircsend(f'PRIVMSG {channel} :\x01ACTION gives {c1} to {source_nick}.\x01') + self.bot.ircsend(f'PRIVMSG {channel} :You now have a total of {c2}.') + + c_db.set(source_nick, { + 'cookies': (cookies + rnd), + 'last': current.strftime('%Y/%m/%d %H:%M:%S') + }) + else: + rem = self.remaining_time(last.strftime('%Y/%m/%d %H:%M:%S'), 30 * 60000) + self.bot.ircsend(f'PRIVMSG {channel} :Remaining time: {rem}') + elif len(parts) == 2: + cookies = c_db.get(parts[1], 2) + + if cookies == -1: + self.bot.ircsend(f'PRIVMSG {channel} :I\'ve looked everywhere for {parts[1]}, but I couldn\'t ' + f'find them.') + else: + c = 'no cookies' if cookies == 0 \ + else f'{cookies} cookie' if cookies == 1 \ + else f'{cookies} cookies' + self.bot.ircsend(f'PRIVMSG {channel} :{parts[1]} currently has {c}.') + + def insert_user(self, user: str): + with c_db.engine.connect() as conn: + stmt = select(cookie_table).where(cookie_table.c.name == user) + cnt = len(conn.execute(stmt).fetchall()) + + if cnt == 0: + conn.execute(( + insert(cookie_table). + values({'name': user}) + )) + conn.commit() + + def remaining_time(self, date: str, timeout: int): + diff = (int(time.mktime( + datetime.strptime(datetime.now().strftime('%Y/%m/%d %H:%M:%S'), '%Y/%m/%d %H:%M:%S').timetuple()) * 1000) - + int(time.mktime(datetime.strptime(date, '%Y/%m/%d %H:%M:%S').timetuple()) * 1000)) + h = int((timeout - diff) / (60 * 60 * 1000) % 24) + m = int((timeout - diff) / (60 * 1000) % 60) + s = int((timeout - diff) / 1000 % 60) + hms = '' + + if h == 0 and m == 0 and s == 0: + return 0 + + if h != 0: + hms += f'{h}h ' + if m != 0: + hms += f'{m}m ' + if s != 0: + hms += f'{s}s ' + + return f'{hms[:-1]}.'