Migrate to SQLite3 backend

This commit is contained in:
osmarks 2020-04-18 13:14:31 +01:00
parent f24012a8a4
commit 510bdca28e
5 changed files with 425 additions and 77 deletions

3
.gitignore vendored
View File

@ -1,3 +1,4 @@
config.toml
bot-data.json
__pycache__
__pycache__
data.sqlite3

30
src/db.py Normal file
View File

@ -0,0 +1,30 @@
import aiosqlite
import logging
migrations = [
"""
CREATE TABLE deleted_items (
id INTEGER PRIMARY KEY,
timestamp INTEGER NOT NULL,
item TEXT NOT NULL
);
""",
"""
CREATE INDEX deleted_items_timestamps ON deleted_items(timestamp);
"""
]
async def init(db_path):
db = await aiosqlite.connect(db_path)
version = (await (await db.execute("PRAGMA user_version")).fetchone())[0]
for i in range(version, len(migrations)):
await db.executescript(migrations[i])
# Normally this would be a terrible idea because of SQL injection.
# However, in this case there is not an obvious alternative (the parameter-based way apparently doesn't work)
# and i + 1 will always be an integer anyway
await db.execute(f"PRAGMA user_version = {i + 1}")
await db.commit()
logging.info(f"Migrated DB to schema {i + 1}")
return db

View File

@ -6,81 +6,24 @@ import discord.ext.commands as commands
import re
import asyncio
import json
import argparse
from datetime import timezone, datetime
import tio
import db
data = {}
data_file = "bot-data.json"
def timestamp(): return int(datetime.now(tz=timezone.utc).timestamp())
def save_data():
try:
with open(data_file, "w") as f:
global data
json.dump(data, f)
except Exception as e:
logging.error(f"Error saving data file: {repr(e)}.")
def load_data():
try:
with open(data_file, "r") as f:
global data
data = json.load(f)
except Exception as e:
logging.warning(f"Error loading data file: {repr(e)}. This is not a critical error.")
load_data()
logging.basicConfig(level=logging.INFO, format="%(levelname)s %(asctime)s %(message)s", datefmt="%H:%M:%S %d/%m/%Y")
# TODO refactor this
database = None
config = toml.load(open("config.toml", "r"))
logging.basicConfig(level=logging.INFO, format="%(levelname)s %(asctime)s %(message)s", datefmt="%H:%M:%S %d/%m/%Y")
bot = commands.Bot(command_prefix='++', description="AutoBotRobot, the most useless bot in the known universe.", case_insensitive=True)
bot._skip_check = lambda x, y: False
cleaner = discord.ext.commands.clean_content()
def clean(ctx, text):
return cleaner.convert(ctx, text)
@bot.event
async def on_ready():
await bot.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.listening, name="commands beginning with ++"))
@bot.event
async def on_message(message):
print(message.content)
await bot.process_commands(message)
@bot.command(help="Gives you a random fortune as generated by `fortune`.")
async def fortune(ctx):
await ctx.send(subprocess.run(["fortune"], stdout=subprocess.PIPE, encoding="UTF-8").stdout)
@bot.command(help="Says Pong.")
async def ping(ctx):
await ctx.send("Pong.")
@bot.command(help="Deletes the specified target.", rest_is_raw=True)
async def delete(ctx, *, raw_target):
target = await clean(ctx, raw_target.strip())
async with ctx.typing():
await ctx.send(f"Deleting {target}...")
await asyncio.sleep(1)
deleted = data.get("deleted", [])
data["deleted"] = deleted + [target]
save_data()
await ctx.send(f"Deleted {target} successfully.")
@bot.command(help="View recently deleted things, optionally matching a filter.")
async def list_deleted(ctx, search=None):
acc = "Recently deleted:\n"
for thing in reversed(data.get("deleted", [])):
to_add = "- " + thing + "\n"
if len(acc + to_add) > 2000:
break
if search == None or search in thing: acc += to_add
await ctx.send(acc)
EXEC_REGEX = "^(.*)\n```([a-zA-Z0-9_\\-+]+)\n(.*)```$"
def make_embed(*, fields=[], footer_text=None, **kwargs):
embed = discord.Embed(**kwargs)
for field in fields:
@ -95,24 +38,92 @@ def make_embed(*, fields=[], footer_text=None, **kwargs):
def error_embed(msg, title="Error"):
return make_embed(color=config["colors"]["error"], description=msg, title=title)
cleaner = discord.ext.commands.clean_content()
def clean(ctx, text):
return cleaner.convert(ctx, text)
@bot.event
async def on_ready():
await bot.change_presence(status=discord.Status.online, activity=discord.Activity(type=discord.ActivityType.listening, name="commands beginning with ++"))
@bot.command(help="Gives you a random fortune as generated by `fortune`.")
async def fortune(ctx):
await ctx.send(subprocess.run(["fortune"], stdout=subprocess.PIPE, encoding="UTF-8").stdout)
@bot.command(help="Says Pong.")
async def ping(ctx):
await ctx.send("Pong.")
@bot.command(help="Deletes the specified target.", rest_is_raw=True)
async def delete(ctx, *, raw_target):
target = await clean(ctx, raw_target.strip().replace("\n", " "))
if len(target) > 256:
await ctx.send(embed=error_embed("Deletion target must be max 256 chars"))
return
async with ctx.typing():
await ctx.send(f"Deleting {target}...")
await asyncio.sleep(1)
await database.execute("INSERT INTO deleted_items (timestamp, item) VALUES (?, ?)", (timestamp(), target))
await database.commit()
await ctx.send(f"Deleted {target} successfully.")
@bot.command(help="View recently deleted things, optionally matching a filter.")
async def list_deleted(ctx, search=None):
acc = "Recently deleted:\n"
if search: acc = f"Recently deleted (matching {search}"
csr = None
if search:
csr = database.execute("SELECT * FROM deleted_items WHERE item LIKE ? ORDER BY timestamp DESC LIMIT 100", (f"%{search}%",))
else:
csr = database.execute("SELECT * FROM deleted_items ORDER BY timestamp DESC LIMIT 100")
async with csr as cursor:
async for row in cursor:
to_add = "- " + row[2] + "\n"
if len(acc + to_add) > 2000:
break
acc += to_add
await ctx.send(acc)
# Python, for some *very intelligent reason*, makes the default ArgumetParser exit the program on error.
# This is obviously undesirable behavior in a Discord bot, so we override this.
class NonExitingArgumentParser(argparse.ArgumentParser):
def exit(self, status=0, message=None):
if status:
raise Exception(f'Flag parse error: {message}')
exit(status)
EXEC_REGEX = "^(.*)```([a-zA-Z0-9_\\-+]+)?\n(.*)```$"
exec_flag_parser = NonExitingArgumentParser(add_help=False)
exec_flag_parser.add_argument("--verbose", "-v", action="store_true")
exec_flag_parser.add_argument("--language", "-L")
@bot.command(rest_is_raw=True, help="Execute provided code (in a codeblock) using TIO.run.")
async def exec(ctx, *, arg):
match = re.match(EXEC_REGEX, arg, flags=re.DOTALL)
if match == None:
await ctx.send(embed=error_embed("Invalid format. Expected a codeblock with language."))
await ctx.send(embed=error_embed("Invalid format. Expected a codeblock."))
return
flags = match.group(1)
lang = match.group(2)
flags_raw = match.group(1)
flags = exec_flag_parser.parse_args(flags_raw.split())
lang = flags.language or match.group(2)
if not lang:
await ctx.send(embed=error_embed("No language specified. Use the -L flag or add a language to your codeblock."))
return
lang = lang.strip()
code = match.group(3)
async with ctx.typing():
ok, result, debug = await tio.run(lang, code)
ok, real_lang, result, debug = await tio.run(lang, code)
if not ok:
await ctx.send(embed=error_embed(result, "Execution error"))
await ctx.send(embed=error_embed(f"""```{result}```""", "Execution error"))
else:
out = result
if "debug" in flags: out += debug
out = out[:2000]
if flags.verbose:
debug_block = f"""\n```{debug}\nLanguage: {real_lang}```"""
out = out[:2000 - len(debug_block)] + debug_block
else:
out = out[:2000]
await ctx.send(out)
@bot.command(help="List supported languages, optionally matching a filter.")
@ -124,6 +135,19 @@ async def supported_langs(ctx, search=None):
await ctx.send(acc)
acc = ""
if search == None or search in lang: acc += lang + " "
if acc == "": acc = "No results."
await ctx.send(acc)
bot.run(config["token"])
async def run_bot():
global database
database = await db.init(config["database"])
await bot.start(config["token"])
if __name__ == '__main__':
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(run_bot())
except KeyboardInterrupt:
loop.run_until_complete(bot.logout())
finally:
loop.close()

292
src/random_unicode.py Normal file
View File

@ -0,0 +1,292 @@
import random
def generate(length):
# Generated from here (https://unicode-table.com/en/blocks/) with this vaguely horrible snippet:
# Array.from(document.querySelectorAll("span.range")).map(node => node.textContent.split("—").map(x => x.trim())).map(([x, y]) => ` (0x${x}, 0x${y}),`).join("\n")
include_ranges = [
(0x0000, 0x001F),
(0x0020, 0x007F),
(0x0080, 0x00FF),
(0x0100, 0x017F),
(0x0180, 0x024F),
(0x0250, 0x02AF),
(0x02B0, 0x02FF),
(0x0300, 0x036F),
(0x0370, 0x03FF),
(0x0400, 0x04FF),
(0x0500, 0x052F),
(0x0530, 0x058F),
(0x0590, 0x05FF),
(0x0600, 0x06FF),
(0x0700, 0x074F),
(0x0750, 0x077F),
(0x0780, 0x07BF),
(0x07C0, 0x07FF),
(0x0800, 0x083F),
(0x0840, 0x085F),
(0x0860, 0x086F),
(0x08A0, 0x08FF),
(0x0900, 0x097F),
(0x0980, 0x09FF),
(0x0A00, 0x0A7F),
(0x0A80, 0x0AFF),
(0x0B00, 0x0B7F),
(0x0B80, 0x0BFF),
(0x0C00, 0x0C7F),
(0x0C80, 0x0CFF),
(0x0D00, 0x0D7F),
(0x0D80, 0x0DFF),
(0x0E00, 0x0E7F),
(0x0E80, 0x0EFF),
(0x0F00, 0x0FFF),
(0x1000, 0x109F),
(0x10A0, 0x10FF),
(0x1100, 0x11FF),
(0x1200, 0x137F),
(0x1380, 0x139F),
(0x13A0, 0x13FF),
(0x1400, 0x167F),
(0x1680, 0x169F),
(0x16A0, 0x16FF),
(0x1700, 0x171F),
(0x1720, 0x173F),
(0x1740, 0x175F),
(0x1760, 0x177F),
(0x1780, 0x17FF),
(0x1800, 0x18AF),
(0x18B0, 0x18FF),
(0x1900, 0x194F),
(0x1950, 0x197F),
(0x1980, 0x19DF),
(0x19E0, 0x19FF),
(0x1A00, 0x1A1F),
(0x1A20, 0x1AAF),
(0x1AB0, 0x1AFF),
(0x1B00, 0x1B7F),
(0x1B80, 0x1BBF),
(0x1BC0, 0x1BFF),
(0x1C00, 0x1C4F),
(0x1C50, 0x1C7F),
(0x1C80, 0x1C8F),
(0x1CC0, 0x1CCF),
(0x1CD0, 0x1CFF),
(0x1D00, 0x1D7F),
(0x1D80, 0x1DBF),
(0x1DC0, 0x1DFF),
(0x1E00, 0x1EFF),
(0x1F00, 0x1FFF),
(0x2000, 0x206F),
(0x2070, 0x209F),
(0x20A0, 0x20CF),
(0x20D0, 0x20FF),
(0x2100, 0x214F),
(0x2150, 0x218F),
(0x2190, 0x21FF),
(0x2200, 0x22FF),
(0x2300, 0x23FF),
(0x2400, 0x243F),
(0x2440, 0x245F),
(0x2460, 0x24FF),
(0x2500, 0x257F),
(0x2580, 0x259F),
(0x25A0, 0x25FF),
(0x2600, 0x26FF),
(0x2700, 0x27BF),
(0x27C0, 0x27EF),
(0x27F0, 0x27FF),
(0x2800, 0x28FF),
(0x2900, 0x297F),
(0x2980, 0x29FF),
(0x2A00, 0x2AFF),
(0x2B00, 0x2BFF),
(0x2C00, 0x2C5F),
(0x2C60, 0x2C7F),
(0x2C80, 0x2CFF),
(0x2D00, 0x2D2F),
(0x2D30, 0x2D7F),
(0x2D80, 0x2DDF),
(0x2DE0, 0x2DFF),
(0x2E00, 0x2E7F),
(0x2E80, 0x2EFF),
(0x2F00, 0x2FDF),
(0x2FF0, 0x2FFF),
(0x3000, 0x303F),
(0x3040, 0x309F),
(0x30A0, 0x30FF),
(0x3100, 0x312F),
(0x3130, 0x318F),
(0x3190, 0x319F),
(0x31A0, 0x31BF),
(0x31C0, 0x31EF),
(0x31F0, 0x31FF),
(0x3200, 0x32FF),
(0x3300, 0x33FF),
(0x3400, 0x4DBF),
(0x4DC0, 0x4DFF),
(0x4E00, 0x9FFF),
(0xA000, 0xA48F),
(0xA490, 0xA4CF),
(0xA4D0, 0xA4FF),
(0xA500, 0xA63F),
(0xA640, 0xA69F),
(0xA6A0, 0xA6FF),
(0xA700, 0xA71F),
(0xA720, 0xA7FF),
(0xA800, 0xA82F),
(0xA830, 0xA83F),
(0xA840, 0xA87F),
(0xA880, 0xA8DF),
(0xA8E0, 0xA8FF),
(0xA900, 0xA92F),
(0xA930, 0xA95F),
(0xA960, 0xA97F),
(0xA980, 0xA9DF),
(0xA9E0, 0xA9FF),
(0xAA00, 0xAA5F),
(0xAA60, 0xAA7F),
(0xAA80, 0xAADF),
(0xAAE0, 0xAAFF),
(0xAB00, 0xAB2F),
(0xAB30, 0xAB6F),
(0xAB70, 0xABBF),
(0xABC0, 0xABFF),
(0xAC00, 0xD7AF),
(0xD7B0, 0xD7FF),
(0xD800, 0xDB7F),
(0xDB80, 0xDBFF),
(0xDC00, 0xDFFF),
(0xE000, 0xF8FF),
(0xF900, 0xFAFF),
(0xFB00, 0xFB4F),
(0xFB50, 0xFDFF),
(0xFE00, 0xFE0F),
(0xFE10, 0xFE1F),
(0xFE20, 0xFE2F),
(0xFE30, 0xFE4F),
(0xFE50, 0xFE6F),
(0xFE70, 0xFEFF),
(0xFF00, 0xFFEF),
(0xFFF0, 0xFFFF),
(0x10000, 0x1007F),
(0x10080, 0x100FF),
(0x10100, 0x1013F),
(0x10140, 0x1018F),
(0x10190, 0x101CF),
(0x101D0, 0x101FF),
(0x10280, 0x1029F),
(0x102A0, 0x102DF),
(0x102E0, 0x102FF),
(0x10300, 0x1032F),
(0x10330, 0x1034F),
(0x10350, 0x1037F),
(0x10380, 0x1039F),
(0x103A0, 0x103DF),
(0x10400, 0x1044F),
(0x10450, 0x1047F),
(0x10480, 0x104AF),
(0x104B0, 0x104FF),
(0x10500, 0x1052F),
(0x10530, 0x1056F),
(0x10600, 0x1077F),
(0x10800, 0x1083F),
(0x10840, 0x1085F),
(0x10860, 0x1087F),
(0x10880, 0x108AF),
(0x108E0, 0x108FF),
(0x10900, 0x1091F),
(0x10920, 0x1093F),
(0x10980, 0x1099F),
(0x109A0, 0x109FF),
(0x10A00, 0x10A5F),
(0x10A60, 0x10A7F),
(0x10A80, 0x10A9F),
(0x10AC0, 0x10AFF),
(0x10B00, 0x10B3F),
(0x10B40, 0x10B5F),
(0x10B60, 0x10B7F),
(0x10B80, 0x10BAF),
(0x10C00, 0x10C4F),
(0x10C80, 0x10CFF),
(0x10E60, 0x10E7F),
(0x11000, 0x1107F),
(0x11080, 0x110CF),
(0x110D0, 0x110FF),
(0x11100, 0x1114F),
(0x11150, 0x1117F),
(0x11180, 0x111DF),
(0x111E0, 0x111FF),
(0x11200, 0x1124F),
(0x11280, 0x112AF),
(0x112B0, 0x112FF),
(0x11300, 0x1137F),
(0x11400, 0x1147F),
(0x11480, 0x114DF),
(0x11580, 0x115FF),
(0x11600, 0x1165F),
(0x11660, 0x1167F),
(0x11680, 0x116CF),
(0x11700, 0x1173F),
(0x118A0, 0x118FF),
(0x11A00, 0x11A4F),
(0x11A50, 0x11AAF),
(0x11AC0, 0x11AFF),
(0x11C00, 0x11C6F),
(0x11C70, 0x11CBF),
(0x11D00, 0x11D5F),
(0x12000, 0x123FF),
(0x12400, 0x1247F),
(0x12480, 0x1254F),
(0x13000, 0x1342F),
(0x14400, 0x1467F),
(0x16800, 0x16A3F),
(0x16A40, 0x16A6F),
(0x16AD0, 0x16AFF),
(0x16B00, 0x16B8F),
(0x16F00, 0x16F9F),
(0x16FE0, 0x16FFF),
(0x17000, 0x187FF),
(0x18800, 0x18AFF),
(0x1B000, 0x1B0FF),
(0x1B100, 0x1B12F),
(0x1B170, 0x1B2FF),
(0x1BC00, 0x1BC9F),
(0x1BCA0, 0x1BCAF),
(0x1D000, 0x1D0FF),
(0x1D100, 0x1D1FF),
(0x1D200, 0x1D24F),
(0x1D300, 0x1D35F),
(0x1D360, 0x1D37F),
(0x1D400, 0x1D7FF),
(0x1D800, 0x1DAAF),
(0x1E000, 0x1E02F),
(0x1E800, 0x1E8DF),
(0x1E900, 0x1E95F),
(0x1EE00, 0x1EEFF),
(0x1F000, 0x1F02F),
(0x1F030, 0x1F09F),
(0x1F0A0, 0x1F0FF),
(0x1F100, 0x1F1FF),
(0x1F200, 0x1F2FF),
(0x1F300, 0x1F5FF),
(0x1F600, 0x1F64F),
(0x1F650, 0x1F67F),
(0x1F680, 0x1F6FF),
(0x1F700, 0x1F77F),
(0x1F780, 0x1F7FF),
(0x1F800, 0x1F8FF),
(0x1F900, 0x1F9FF),
(0x20000, 0x2A6DF),
(0x2A700, 0x2B73F),
(0x2B740, 0x2B81F),
(0x2B820, 0x2CEAF),
(0x2CEB0, 0x2EBEF),
(0x2F800, 0x2FA1F),
(0xE0000, 0xE007F),
(0xE0100, 0xE01EF),
]
alphabet = [
chr(code_point) for current_range in include_ranges
for code_point in range(current_range[0], current_range[1] + 1)
]
return ''.join(random.choice(alphabet) for i in range(length))

View File

@ -15,11 +15,12 @@ aliases = {
client = http3.AsyncClient()
async def run(lang, code):
req = pytio.TioRequest(aliases.get(lang, lang), code)
real_lang = aliases.get(lang, lang)
req = pytio.TioRequest(real_lang, code)
res = await client.post("https://tio.run/cgi-bin/run/api/", data=req.as_deflated_bytes(), timeout=65)
content = res.content.decode("UTF-8")
split = list(filter(lambda x: x != "\n" and x != "", content.split(content[:16])))
if len(split) == 1:
return False, split[0], None
return False, real_lang, split[0], None
else:
return True, split[0], split[1]
return True, real_lang, split[0], split[1]