diff --git a/.gitignore b/.gitignore index 66f32f2..745ac94 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules/ dist/ -src/config.ts \ No newline at end of file +src/config.ts +database.db diff --git a/src/client.ts b/src/client.ts index 3baa740..6699c7d 100644 --- a/src/client.ts +++ b/src/client.ts @@ -6,8 +6,9 @@ import { minky } from "./commands/minky"; import { Command } from "./utils/types"; import { forgejoDown } from "./commands/forgejo-down"; import { source } from "./commands/source"; +import { sql } from "./commands/sql"; -const commands: Command[] = [forgejoDown, minky, source]; +const commands: Command[] = [forgejoDown, minky, source, sql]; export const bot = new Telegraf(config.token); bot.on(message("text"), async (ctx) => { @@ -32,7 +33,8 @@ bot.on(message("text"), async (ctx) => { if (c.ownerOnly && msg.from.id !== 1911826384) return await ctx.reply("💢 You can't do this!"); handled = true; - c.handler(ctx, rawArgs.splice(0, 1)); + rawArgs.splice(0, 1); + c.handler(ctx, rawArgs); } c.aliases.forEach(async (a) => { if (a === rawArgs[0].replace("v", "") || `/${a}@vc_venbot` === rawArgs[0]) { @@ -46,7 +48,8 @@ bot.on(message("text"), async (ctx) => { if (c.ownerOnly && msg.from.id !== 1911826384) return await ctx.reply("💢 You can't do this!"); handled = true; - c.handler(ctx, rawArgs.splice(0, 1)); + rawArgs.splice(0, 1); + c.handler(ctx, rawArgs); } }); }); diff --git a/src/commands/sql.ts b/src/commands/sql.ts new file mode 100644 index 0000000..b43e157 --- /dev/null +++ b/src/commands/sql.ts @@ -0,0 +1,34 @@ +import { Context } from "telegraf"; +import { Command, CommandTypes } from "../utils/types"; +import { read } from "../utils/database"; + +async function handler(ctx: Context, args: string[]) { + try { + const result = await read(args.join(" "), []); + const response = await fetch("https://bin.nin0.dev", { + method: "POST", + body: JSON.stringify({ + text: `${result}`, + extension: "json" + }), + headers: new Headers({ "content-type": "application/json" }) + }); + if (!response.ok) { + return ctx.reply("Couldn't upload to wastebin :("); + } + const jsonResponse = await response.json(); + ctx.reply(`See query result at https://bin.nin0.dev${jsonResponse.path}`); + } catch (err) { + ctx.reply("Error reading from database or uploading to wastebin :("); + } +} + +export const sql: Command = { + name: "sql", + description: "Eval a SQL query", + type: CommandTypes.Utility, + modOnly: false, + ownerOnly: true, + handler, + aliases: [] +}; diff --git a/src/index.ts b/src/index.ts index 6cda6cf..5785c04 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,4 +3,12 @@ import { Command } from "./utils/types"; const commands: Command[] = []; +console.log("Clearing update queue"); +bot.telegram.setWebhook("https://nin0.dev", { + drop_pending_updates: true +}); +bot.telegram.deleteWebhook({ + drop_pending_updates: true +}); +console.log(`Connecting`); bot.launch(); diff --git a/src/utils/database.ts b/src/utils/database.ts index f838f16..ee06814 100644 --- a/src/utils/database.ts +++ b/src/utils/database.ts @@ -3,12 +3,29 @@ import { Database } from "sqlite3"; const db = new Database("database.db"); export async function write(query: string, args: string[]): Promise { - await db.run(query, args); + return new Promise((resolve, reject) => { + db.serialize(() => { + db.run(query, args, (err) => { + if (err) { + reject(err); + } else { + resolve(true); + } + }); + }); + }); } -export async function read(query: string, args: string[]) { - let rt = []; - db.each(query, args, (q) => { - rt. - }) -} \ No newline at end of file +export async function read(query: string, args: string[]): Promise { + return new Promise((resolve, reject) => { + db.serialize(() => { + db.all(query, args, (err, rows) => { + if (err) { + reject(err); + } else { + resolve(JSON.stringify(rows)); + } + }); + }); + }); +}