SQL command

This commit is contained in:
nin0dev 2024-09-23 18:59:32 -04:00
parent 0143dc9abe
commit 161748b993
Signed by: nin0
GPG key ID: 3FA8F96ABAE04214
5 changed files with 74 additions and 11 deletions

1
.gitignore vendored
View file

@ -1,3 +1,4 @@
node_modules/
dist/
src/config.ts
database.db

View file

@ -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);
}
});
});

34
src/commands/sql.ts Normal file
View file

@ -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: []
};

View file

@ -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();

View file

@ -3,12 +3,29 @@ import { Database } from "sqlite3";
const db = new Database("database.db");
export async function write(query: string, args: string[]): Promise<boolean> {
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.
})
export async function read(query: string, args: string[]): Promise<string> {
return new Promise((resolve, reject) => {
db.serialize(() => {
db.all(query, args, (err, rows) => {
if (err) {
reject(err);
} else {
resolve(JSON.stringify(rows));
}
});
});
});
}