From f8bf6b6d55123eb7736a0649e420ef6c341589ec Mon Sep 17 00:00:00 2001 From: mrhanky Date: Fri, 30 Jun 2017 19:33:26 +0200 Subject: [PATCH] Bug fixes n shit --- config.json | 3 +- nxy/bot.py | 3 +- nxy/plugins/coins.py | 56 ++++++++++++++++++++--------- nxy/plugins/quotes.py | 24 +------------ nxy/plugins/rape.py | 5 ++- nxy/plugins/tell.py | 44 +++++++++++++---------- nxy/plugins/useless.py | 80 ++++++++++++++++++++++++++++++++++++++++-- schema.sql | 1 - 8 files changed, 149 insertions(+), 67 deletions(-) diff --git a/config.json b/config.json index 475ecfa..539716a 100644 --- a/config.json +++ b/config.json @@ -4,8 +4,7 @@ "port": 56791, "ssl": true, "raw": true, - "autojoins": ["#nxy-dev"], - "storage": "sqlite://db.sqlite", + "autojoins": ["#w0bm", "#f0ck"], "flood_burst": 1, "flood_rate": 4, "flood_rate_delay": 1, diff --git a/nxy/bot.py b/nxy/bot.py index 3e78c3a..8d2b23b 100644 --- a/nxy/bot.py +++ b/nxy/bot.py @@ -24,6 +24,7 @@ CFG_DEV = { # TODO: regex +# TODO: ddg def main(cfg_file): # Load dotenv from file load_dotenv('.env') @@ -36,7 +37,7 @@ def main(cfg_file): if bool(os.environ.get('DEV')): cfg.update(CFG_DEV) # If PASSWORD in env set it in config - elif 'PASSWORD' in os.environ: + if 'PASSWORD' in os.environ: cfg['password'] = os.environ['PASSWORD'] # Start the bot with constructed config diff --git a/nxy/plugins/coins.py b/nxy/plugins/coins.py index a4ddf55..31a855f 100644 --- a/nxy/plugins/coins.py +++ b/nxy/plugins/coins.py @@ -12,34 +12,56 @@ from . import Plugin class Coins(Plugin): requires = ['irc3.plugins.command'] + CRYPTOWAT = 'https://api.cryptowat.ch/markets/{market}/{crypto}{currency}' \ + '/summary' + CURRENCIES = { + 'usd': '$', + 'eur': '€', + 'eth': 'Ξ', + 'btc': '฿', + } + @command def btc(self, mask: IrcString, target: IrcString, args: DocOptDict): """Gets the Bitcoin values from BitStamp. - %%btc + %%btc [] """ - data = requests.get('https://www.bitstamp.net/api/ticker').json() - values = {k: float(data[k]) for k in ['last', 'high', 'low', 'volume']} - return '\x02[Bitcoin]\x02 ' \ - 'Current: \x02\x037${last:,.2f}\x0F - ' \ - 'High: \x02\x033${high:,.2f}\x0F - ' \ - 'Low: \x02\x034${low:,.2f}\x0F - ' \ - 'Volume: \x02฿{volume:,.2f}\x02'.format(**values) + return self._cryptowat_summary('btc', args.get('') or 'usd') @command def eth(self, mask: IrcString, target: IrcString, args: DocOptDict): """Gets the Ethereum values from etherscan.io. - %%eth + %%eth [] """ - volume = self._etherscan('ethsupply') - data = self._etherscan('ethprice') - return '\x02[EtherScan]\x02' \ - '\x02\x0307 ${usd:,.2f}\x0F |\x02\x0307 {btc:,.5f} BTC\x0F ' \ - '- Volume: \x02{volume:,.2f}\x02' \ - .format(usd=float(data['ethusd']), - btc=float(data['ethbtc']), - volume=float(int(volume) / 1000000000000000000)) + return self._cryptowat_summary('eth', args.get('') or 'usd') + + def _cryptowat_summary(self, crypto: str, currency: str = 'usd', + market: str = 'coinbase'): + # Check if valid currency + crypto2currency + if currency not in self.CURRENCIES or crypto == currency: + return + + # Send request to api + data = requests.get(self.CRYPTOWAT.format(market=market, + crypto=crypto, + currency=currency)) + if data: + result = data.json()['result'] + return '\x02[{crypto}]\x02 ' \ + 'Current: \x02\x0307{currency}{last:,.2f}\x0F - ' \ + 'High: \x02\x0303{currency}{high:,.2f}\x0F - ' \ + 'Low: \x02\x0304{currency}{low:,.2f}\x0F - ' \ + 'Change: \x02\x0307{change:,.2f}%\x0F - ' \ + 'Volume: \x02\x0307{volume}\x0F' \ + .format(crypto=crypto.upper(), + currency=self.CURRENCIES[currency], + last=result['price']['last'], + high=result['price']['high'], + low=result['price']['low'], + change=result['price']['change']['percentage'] * 100, + volume=result['volume']) @staticmethod def _etherscan(action: str): diff --git a/nxy/plugins/quotes.py b/nxy/plugins/quotes.py index a5a9d00..8da416d 100644 --- a/nxy/plugins/quotes.py +++ b/nxy/plugins/quotes.py @@ -10,28 +10,6 @@ from . import DatabasePlugin from ..utils import NICK_REGEX, parse_int -""" - delete from - quotes - where - id = ( - select - id - from - quotes a - where - nick like %s and %s = ( - select - count(id) - from - quotes b where a.id {op} b.id - ) - order by - id {order} - ) -""" - - @irc3.plugin class Quotes(DatabasePlugin): requires = ['irc3.plugins.command', @@ -49,7 +27,7 @@ class Quotes(DatabasePlugin): insert into quotes (nick, item, channel, created_by) values - (%s, %s) + (%s, %s, %s, %s) ''', [nick, quote, channel, mask.nick]) def delete_quote(self, nick, quote): diff --git a/nxy/plugins/rape.py b/nxy/plugins/rape.py index f3b5b73..6abfb74 100644 --- a/nxy/plugins/rape.py +++ b/nxy/plugins/rape.py @@ -61,15 +61,14 @@ class Rape(DatabasePlugin): # Insert or add fine to database and return total owe self.cur.execute(''' insert into - users (nick, host, fines) + users (nick, fines) values (%s, %s, %s) on conflict (nick) do update set - host = excluded.host, fines = users.fines + excluded.fines returning fines - ''', [nick, mask.host, fine]) + ''', [nick, fine]) self.con.commit() # Get reason based on rand value diff --git a/nxy/plugins/tell.py b/nxy/plugins/tell.py index 70527c1..d849b26 100644 --- a/nxy/plugins/tell.py +++ b/nxy/plugins/tell.py @@ -1,4 +1,6 @@ # -*- coding: utf-8 -*- +from datetime import datetime + import irc3 from docopt import Dict as DocOptDict from irc3.plugins.command import command @@ -20,20 +22,19 @@ class Tell(DatabasePlugin): # Fetch tells from database self.cur.execute(''' select - from_nick, to_nick, message + to_nick, from_nick, message, created_at from tells ''') # Add tells to queue for res in self.cur.fetchall(): - nick = res['to_nick'] + nick = res['to_nick'].lower() # Create list in queue and add tell if nick not in self.tell_queue: self.tell_queue[nick] = [] - self.tell_queue[nick].append([res['from_nick'], - res['message']]) + self.tell_queue[nick].append(res[1:]) @command def tell(self, mask: IrcString, target: IrcString, args: DocOptDict): @@ -41,21 +42,23 @@ class Tell(DatabasePlugin): %%tell ... """ - nick = args[''].lower() - tell = [mask.nick.lower(), nick, ' '.join(args['']).strip()] + nick = args[''] + nick_lower = nick.lower() + tell = [nick, mask.nick, ' '.join(args['']).strip(), + datetime.now()] # Create list in queue and add tell - if nick not in self.tell_queue: - self.tell_queue[nick] = [] - self.tell_queue[nick].append(tell) + if nick_lower not in self.tell_queue: + self.tell_queue[nick_lower] = [] + self.tell_queue[nick_lower].append(tell[1:]) try: # Insert tell into database self.cur.execute(''' insert into - tells (from_nick, to_nick, message) + tells (to_nick, from_nick, message, created_at) values - (%s, %s, %s) + (%s, %s, %s, %s) ''', tell) self.con.commit() except Error: @@ -66,15 +69,19 @@ class Tell(DatabasePlugin): def check(self, mask: str): """If activ nick has tells, forward and delete them.""" nick = IrcString(mask).nick + nick_lower = nick.lower() - if nick in self.tell_queue: + if nick_lower in self.tell_queue: # Forward all tells for nick - for tell in self.tell_queue[nick]: - self.bot.privmsg(nick, '[Tell] Message from {nick}: {message}' - .format(nick=tell[0], message=tell[1])) + for tell in self.tell_queue[nick_lower]: + # TODO: format time + self.bot.privmsg(nick, '[Tell] Message from {nick} at {time}: ' + '{message}'.format(nick=tell[0], + time=tell[2], + message=tell[1])) # Remove nick from queue - del self.tell_queue[nick] + del self.tell_queue[nick_lower] try: # Remove tells from database @@ -82,9 +89,10 @@ class Tell(DatabasePlugin): delete from tells where - to_nick = %s + lower(to_nick) = lower(%s) ''', [nick]) self.con.commit() - except Error: + except Error as ex: + print(ex) # Rollback transaction on error self.con.rollback() diff --git a/nxy/plugins/useless.py b/nxy/plugins/useless.py index 0c8c34b..531407e 100644 --- a/nxy/plugins/useless.py +++ b/nxy/plugins/useless.py @@ -39,7 +39,7 @@ class Useless(DatabasePlugin): msg=msg.upper())) @command - def kill(self, mask:IrcString,target:IrcString,args:DocOptDict): + def kill(self, mask: IrcString, target: IrcString, args: DocOptDict): """Kills a user with a random message. %%kill [] @@ -58,7 +58,7 @@ class Useless(DatabasePlugin): return self.cur.fetchone()['item'].format(nick=nick) @command - def yiff(self, mask:IrcString,target:IrcString,args:DocOptDict): + def yiff(self, mask: IrcString, target: IrcString, args: DocOptDict): """Yiffs a user with a random message. %%yiff [] @@ -76,6 +76,82 @@ class Useless(DatabasePlugin): ''') return self.cur.fetchone()['item'].format(nick=nick) + @command + def waifu(self, mask: IrcString, target: IrcString, args: DocOptDict): + """Get waifu for a user. + + %%waifu [] + """ + nick = args.get('') or mask.nick + + if nick.startswith('='): + waifu = nick[1:] + + self.cur.execute(''' + insert into + users (nick, waifu) + values + (%s, %s) + on conflict (nick) do update set + waifu = excluded.waifu + ''', [mask.nick, waifu]) + + self.bot.notice(mask.nick, 'Waifu set to: {waifu}' + .format(waifu=waifu)) + else: + self.cur.execute(''' + select + waifu + from + users + where + lower(nick) = lower(%s) + ''', [nick]) + result = self.cur.fetchone() + + if result and result['waifu']: + return '\x02[Waifu]\x0F {nick}: {waifu}'.format( + nick=nick, + waifu=result['waifu']) + + @command + def husbando(self, mask: IrcString, target: IrcString, args: DocOptDict): + """Get husbando for a user. + + %%husbando [] + """ + nick = args.get('') or mask.nick + + if nick.startswith('='): + husbando = nick[1:] + + self.cur.execute(''' + insert into + users (nick, husbando) + values + (%s, %s) + on conflict (nick) do update set + husbando = excluded.husbando + ''', [mask.nick, husbando]) + + self.bot.notice(mask.nick, 'Husbando set to: {husbando}' + .format(husbando=husbando)) + else: + self.cur.execute(''' + select + husbando + from + users + where + lower(nick) = lower(%s) + ''', [nick]) + result = self.cur.fetchone() + + if result and result['husbando']: + return '\x02[Husbando]\x0F {nick}: {husbando}'.format( + nick=nick, + husbando=result['husbando']) + @command def storyofpomfface(self, mask: IrcString, target: IrcString, args: DocOptDict): diff --git a/schema.sql b/schema.sql index e4bff9f..3ee9028 100644 --- a/schema.sql +++ b/schema.sql @@ -46,7 +46,6 @@ create table if not exists seens ( create table if not exists users ( id serial primary key, nick varchar(30) not null, - host varchar(255) not null, husbando varchar(255) null, waifu varchar(255) null, fines integer default 0,