From c0f8606715c0e78ca4598ea4ad6f5a9773407d2a Mon Sep 17 00:00:00 2001 From: ComputerTech312 Date: Tue, 20 Feb 2024 09:32:48 +0100 Subject: [PATCH] Replaced multiple instances of double quotes with single quotes. --- .gitignore | 3 ++- config.json | 3 ++- config.yaml | 7 ++--- plugins/cookie.py | 68 ++++++++++++++++++++++++++++++++++++++--------- plugins/hello.py | 25 ----------------- src/bot.py | 22 +++++++-------- src/logger.py | 2 +- 7 files changed, 73 insertions(+), 57 deletions(-) delete mode 100644 plugins/hello.py diff --git a/.gitignore b/.gitignore index 07bc8ff..1899225 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ __pycache__ data logs -test.json \ No newline at end of file +test.json +elitebot \ No newline at end of file diff --git a/config.json b/config.json index 90a0e29..6d67a18 100644 --- a/config.json +++ b/config.json @@ -1,4 +1,5 @@ { + "VERSION": "1.0.0", "Connection": { "Hostname": "irc.example.net", "Port": "+6697", @@ -18,4 +19,4 @@ "Database": { "ConnectionString": "mysql://user:password@host:port/database" } -} +} \ No newline at end of file diff --git a/config.yaml b/config.yaml index 34c5ac2..5d98877 100644 --- a/config.yaml +++ b/config.yaml @@ -1,14 +1,15 @@ +VERSION: 1.0.0 Connection: Hostname: irc.rizon.net - Port: "+6697" + Port: '+6697' Nick: EliteBot Ident: EliteBot Name: EliteBot BindHost: 0.0.0.0 SASL: - UseSASL: true + UseSASL: false SASLNick: EliteBot - SASLPassword: 789abc// + SASLPassword: password Logging: Console: true Database: diff --git a/plugins/cookie.py b/plugins/cookie.py index f8170af..d7926a5 100644 --- a/plugins/cookie.py +++ b/plugins/cookie.py @@ -1,3 +1,7 @@ +import random +import time +from datetime import datetime + from sqlalchemy import Table, Column, Integer, String, MetaData, insert, select from src.channel_manager import ChannelManager @@ -23,27 +27,44 @@ class Plugin(PluginBase): self.channel_manager = ChannelManager() if parts[0].lower() == '!cookie': - if len(parts) == 1: # !cookie + if len(parts) == 1: self.insert_user(source_nick) - self.bot.ircsend(f'PRIVMSG {channel} :Nooooo~~') - c_db.set(source_nick, {'cookies': 1, 'last': '1999/01/01 00:00:01'}) - elif len(parts) == 2: # !cookie USER + 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 = 'cookie' - if cookies == 0: - c = 'no cookies.' - elif cookies == 1: - c = f'{cookies} cookie.' - else: - c = f'{cookies} cookies.' - - self.bot.ircsend(f'PRIVMSG {channel} :{parts[1]} currently has {c}') + 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: @@ -56,3 +77,24 @@ class Plugin(PluginBase): 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]}.' \ No newline at end of file diff --git a/plugins/hello.py b/plugins/hello.py deleted file mode 100644 index fd4a92a..0000000 --- a/plugins/hello.py +++ /dev/null @@ -1,25 +0,0 @@ -from src.plugin_base import PluginBase - -class WhoisPlugin(PluginBase): - def __init__(self, bot): - super().__init__(bot) - self.pending_whois = {} - - def handle_message(self, source_nick, channel, message): - message_parts = message.split() - if message_parts[0] == '@whois': - self.bot.ircsend(f'WHOIS {source_nick}') - self.pending_whois[source_nick] = channel - print(f"Sent WHOIS for {source_nick}") - elif ' | ' in message: - parts = message.split(' | ') - command = parts[2].split(': ')[1] - if command == '311': - args = parts[3].split(': ')[1].strip('[]').split(', ') - nick = args[1].strip('\'') - hostmask = args[3].strip('\'') - if nick in self.pending_whois: - channel = self.pending_whois[nick] - self.bot.ircsend(f'PRIVMSG {channel} :pew') - print(f"Sent 'pew' to {channel}") - del self.pending_whois[nick] \ No newline at end of file diff --git a/src/bot.py b/src/bot.py index d34cb19..bcb9618 100644 --- a/src/bot.py +++ b/src/bot.py @@ -8,7 +8,6 @@ import socket import ssl import sys import time - import yaml from src.channel_manager import ChannelManager @@ -32,18 +31,17 @@ class Bot: def validate_config(self, config): required_fields = [ - ["Connection", "Port"], - ["Connection", "Hostname"], - ["Connection", "Nick"], - ["Connection", "Ident"], - ["Connection", "Name"], - # ["SASL", "UseSASL"], - ["Database", "ConnectionString"] + ['Connection', 'Port'], + ['Connection' 'Hostname'], + ['Connection', 'Nick'], + ['Connection', 'Ident'], + ['Connection', 'Name'], + ['Database', 'ConnectionString'] ] for field in required_fields: if not self.get_nested_config_value(config, field): - raise ValueError(f'Missing required config field: {" -> ".join(field)}') + raise ValueError(f'Missing required config field: {' -> '.join(field)}') def get_nested_config_value(self, config, keys): value = config @@ -92,7 +90,7 @@ class Bot: except UnicodeDecodeError: continue self.logger.error('Could not decode byte string with any known encoding') - return bytes.decode('utf-8', 'ignore') # Ignore errors and return as much as possible + return bytes.decode('utf-8', 'ignore') def ircsend(self, msg): try: @@ -143,7 +141,7 @@ class Bot: if self.config['SASL'].get('UseSASL'): self.ircsend('CAP REQ :sasl') except Exception as e: - self.logger.error(f"Error establishing connection: {e}") + self.logger.error(f'Error establishing connection: {e}') self.connected = False return @@ -168,13 +166,11 @@ class Bot: source, command, args = self.parse_message(ircmsg) self.logger.debug(f'Received: source: {source} | command: {command} | args: {args}') - # Handle PING requests immediately if command == 'PING': nospoof = args[0][1:] if args[0].startswith(':') else args[0] self.ircsend(f'PONG :{nospoof}') continue - # Process other messages if command == 'PRIVMSG': channel, message = args[0], args[1] source_nick = source.split('!')[0] diff --git a/src/logger.py b/src/logger.py index 7a0bcec..1a5255d 100644 --- a/src/logger.py +++ b/src/logger.py @@ -25,7 +25,7 @@ class Logger: case 'error': print(f'\033[91m[{asctime}] - {message}\033[39m') case _: - pass # just ignore it. + pass def debug(self, message): self.log('debug', message)