fixed a bug in sendAsMemberHandler.ts and switched to bun's sqlite library
This commit is contained in:
parent
fd3977d224
commit
0a56096f09
17 changed files with 91 additions and 44 deletions
BIN
bun.lockb
BIN
bun.lockb
Binary file not shown.
|
@ -9,13 +9,12 @@
|
||||||
"author": "",
|
"author": "",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/sqlite3": "^3.1.8",
|
"bun-types": "^1.0.31",
|
||||||
"typescript": "^5.1.3",
|
"typescript": "^5.1.3",
|
||||||
"typescript-language-server": "^3.3.2"
|
"typescript-language-server": "^3.3.2"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"revolt.js": "^7.0.0-beta.9",
|
"revolt.js": "^7.0.0-beta.9",
|
||||||
"shlex": "^2.1.2",
|
"shlex": "^2.1.2"
|
||||||
"sqlite3": "^5.1.6"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { Database } from "sqlite3";
|
import { Database } from "bun:sqlite";
|
||||||
import MemberModel from "../models/MemberModel"
|
import MemberModel from "../models/MemberModel"
|
||||||
import { MemberRepo } from "../repositories/MemberRepo"
|
import { MemberRepo } from "../repositories/MemberRepo"
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { MemberRepo } from "../repositories/MemberRepo"
|
import { MemberRepo } from "../repositories/MemberRepo"
|
||||||
import MemberModel from "../models/MemberModel"
|
import MemberModel from "../models/MemberModel"
|
||||||
import { Database } from "sqlite3";
|
import { Database } from "bun:sqlite";
|
||||||
|
|
||||||
async function colorMemberChange(userId: string, args: string[], database: Database){
|
async function colorMemberChange(userId: string, args: string[], database: Database){
|
||||||
const memberRepo = new MemberRepo(database);
|
const memberRepo = new MemberRepo(database);
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { Database } from "sqlite3";
|
import { Database } from "bun:sqlite";
|
||||||
import { MemberRepo } from "../repositories/MemberRepo";
|
import { MemberRepo } from "../repositories/MemberRepo";
|
||||||
import MemberModel from "models/MemberModel"
|
import MemberModel from "models/MemberModel"
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { MemberRepo } from "../repositories/MemberRepo"
|
import { MemberRepo } from "../repositories/MemberRepo"
|
||||||
import MemberModel from "../models/MemberModel"
|
import MemberModel from "../models/MemberModel"
|
||||||
import { Database } from "sqlite3";
|
import { Database } from "bun:sqlite";
|
||||||
|
|
||||||
export async function deleteMember(userId: string, args: string[], database: Database) {
|
export async function deleteMember(userId: string, args: string[], database: Database) {
|
||||||
const memberRepo = new MemberRepo(database);
|
const memberRepo = new MemberRepo(database);
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { MemberRepo } from "../repositories/MemberRepo"
|
import { MemberRepo } from "../repositories/MemberRepo"
|
||||||
import { tableConstructor } from '../utils/tableConstructor'
|
import { tableConstructor } from '../utils/tableConstructor'
|
||||||
import { Database } from "sqlite3";
|
import { Database } from "bun:sqlite";
|
||||||
import MemberModel from "../models/MemberModel";
|
import MemberModel from "../models/MemberModel";
|
||||||
|
|
||||||
async function listMember(userId: string, database: Database) {
|
async function listMember(userId: string, database: Database) {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { File } from "revolt.js"
|
import { File } from "revolt.js"
|
||||||
import { Database } from "sqlite3"
|
import { Database } from "bun:sqlite"
|
||||||
import createMember from "./createMember"
|
import createMember from "./createMember"
|
||||||
import { avatarMemberChange } from "./avatarMemberChange"
|
import { avatarMemberChange } from "./avatarMemberChange"
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { Client, Message } from "revolt.js";
|
import { Client, Message } from "revolt.js";
|
||||||
import { commandHandler } from "./utils/commandHandler"
|
import { commandHandler } from "./utils/commandHandler"
|
||||||
import { nonCommandHandler } from "./utils/nonCommandHandler"
|
import { nonCommandHandler } from "./utils/nonCommandHandler"
|
||||||
import { Database } from "sqlite3";
|
import { Database } from "bun:sqlite";
|
||||||
import config from "../config.json"
|
import config from "../config.json"
|
||||||
import dbInit from "utils/dbInit";
|
import dbInit from "utils/dbInit";
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { Database } from 'sqlite3';
|
import { Database } from 'bun:sqlite';
|
||||||
import MemberModel from '../models/MemberModel';
|
import MemberModel from '../models/MemberModel';
|
||||||
|
|
||||||
export class MemberRepo {
|
export class MemberRepo {
|
||||||
|
@ -9,30 +9,33 @@ export class MemberRepo {
|
||||||
}
|
}
|
||||||
|
|
||||||
async getAltersByUserId(userId : string) : Promise<MemberModel[]> {
|
async getAltersByUserId(userId : string) : Promise<MemberModel[]> {
|
||||||
const query : string = 'SELECT * FROM members WHERE members.owner = "' + userId + '"'
|
const query = this.db.query("SELECT * FROM members WHERE members.owner=?")
|
||||||
|
|
||||||
let result: MemberModel[] = await new Promise((resolve, reject) => {
|
let result: MemberModel[] = query.all(userId).map( (row: any) => {
|
||||||
this.db.all(query, (err, row: MemberModel[]) => {
|
const mapped_row: MemberModel = {
|
||||||
if (err) { return reject(err)}
|
id: row.id,
|
||||||
else { return resolve(row) }
|
"name": row.name,
|
||||||
|
"prefix": row.prefix,
|
||||||
|
"owner": row.owner,
|
||||||
|
"profile_pic_url": row.profile_pic_url
|
||||||
|
}
|
||||||
|
return mapped_row
|
||||||
})
|
})
|
||||||
})
|
|
||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
addAlterForUser(alter: MemberModel){
|
addAlterForUser(alter: MemberModel){
|
||||||
this.db.run("INSERT INTO members (owner, prefix, name, profile_pic_url, color) VALUES(?, ?, ?, ?, ?)",
|
this.db.query("INSERT INTO members (owner, prefix, name, profile_pic_url) VALUES(?, ?, ?, ?)")
|
||||||
[alter.owner, alter.prefix, alter.name, alter.profile_pic_url])
|
.run(alter.owner, alter.prefix, alter.name, alter.profile_pic_url)
|
||||||
}
|
}
|
||||||
|
|
||||||
editAlter(alter: MemberModel){
|
editAlter(alter: MemberModel){
|
||||||
this.db.run("UPDATE members SET owner=?, prefix=?, name=?, profile_pic_url=?, color=? WHERE members.id=?",
|
this.db.query("UPDATE members SET owner=?, prefix=?, name=?, profile_pic_url=?, color=? WHERE members.id=?")
|
||||||
[alter.owner, alter.prefix, alter.name, alter.profile_pic_url, alter.color, alter.id])
|
.run(alter.owner, alter.prefix, alter.name, alter.profile_pic_url, alter.color, alter.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
delete(alterId: number) {
|
delete(alterId: number) {
|
||||||
this.db.run("DELETE FROM members WHERE members.id=?", [alterId])
|
this.db.query("DELETE FROM members WHERE members.id=?").run(alterId)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import { Database } from "sqlite3";
|
import UserModel from "models/UserModel";
|
||||||
|
import { Database } from "bun:sqlite";
|
||||||
|
|
||||||
class UserRepo {
|
class UserRepo {
|
||||||
db: Database
|
db: Database
|
||||||
|
@ -7,8 +8,41 @@ class UserRepo {
|
||||||
this.db = database
|
this.db = database
|
||||||
}
|
}
|
||||||
|
|
||||||
getById() {}
|
getById(id) {
|
||||||
new() {}
|
const row = this.db.run(`
|
||||||
delete() {}
|
SELECT *
|
||||||
|
FROM users
|
||||||
|
WHERE users.id = ?
|
||||||
|
`, [id])
|
||||||
|
|
||||||
|
return row
|
||||||
}
|
}
|
||||||
|
new(model: UserModel) {}
|
||||||
|
delete(id) {}
|
||||||
|
|
||||||
|
isProxyEnabled(id: number) {
|
||||||
|
const row: any = this.db.query(`
|
||||||
|
SELECT auto_proxy
|
||||||
|
FROM users
|
||||||
|
WHERE users.id=?
|
||||||
|
`).get(id)
|
||||||
|
|
||||||
|
let is_enabled: boolean
|
||||||
|
|
||||||
|
is_enabled = row.auto_proxy == "TRUE" ? true : false
|
||||||
|
return is_enabled
|
||||||
|
}
|
||||||
|
|
||||||
|
getIdByRevoltId(revolt_id: string){
|
||||||
|
const row: any = this.db.query(`
|
||||||
|
SELECT id
|
||||||
|
FROM users
|
||||||
|
WHERE users.revolt_id=?
|
||||||
|
`).get(revolt_id)
|
||||||
|
|
||||||
|
const id: number = row.id
|
||||||
|
return id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default UserRepo
|
||||||
|
|
|
@ -1,15 +1,16 @@
|
||||||
import { MemberRepo } from "repositories/MemberRepo";
|
import UserRepo from "repositories/UserRepo";
|
||||||
import { Database } from "sqlite3";
|
import { Database } from "bun:sqlite";
|
||||||
|
|
||||||
async function checkAutoProxy
|
function checkAutoProxy
|
||||||
(
|
(
|
||||||
userId: number,
|
userId: string,
|
||||||
database: Database
|
database: Database
|
||||||
): Promise<Boolean>
|
)
|
||||||
{
|
{
|
||||||
const repo = new MemberRepo(database)
|
const repo = new UserRepo(database)
|
||||||
|
const user_id = repo.getIdByRevoltId(userId)
|
||||||
return true
|
const is_enabled = repo.isProxyEnabled(user_id)
|
||||||
|
return is_enabled
|
||||||
}
|
}
|
||||||
|
|
||||||
export default checkAutoProxy;
|
export default checkAutoProxy;
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { Message } from "revolt.js"
|
import { Message } from "revolt.js"
|
||||||
import { Database } from "sqlite3"
|
import { Database } from "bun:sqlite"
|
||||||
import { split } from "shlex"
|
import { split } from "shlex"
|
||||||
|
|
||||||
import { returnHelpText } from "../commands/help"
|
import { returnHelpText } from "../commands/help"
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { Database } from "sqlite3";
|
import { Database } from "bun:sqlite";
|
||||||
|
|
||||||
function dbInit(database: Database){
|
function dbInit(database: Database){
|
||||||
database.run("CREATE TABLE IF NOT EXISTS members (id INTEGER PRIMARY KEY AUTOINCREMENT, owner TEXT, prefix TEXT, name TEXT, profile_pic_url TEXT, color TEXT)");
|
database.run("CREATE TABLE IF NOT EXISTS members (id INTEGER PRIMARY KEY AUTOINCREMENT, owner TEXT, prefix TEXT, name TEXT, profile_pic_url TEXT, color TEXT)");
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
import { Message } from "revolt.js"
|
import { Message } from "revolt.js"
|
||||||
import { Database } from "sqlite3";
|
import { Database } from "bun:sqlite";
|
||||||
import sendAsMember from "./sendAsMemberHandler";
|
import sendAsMember from "./sendAsMemberHandler";
|
||||||
|
import checkAutoProxy from "./checkAutoProxy";
|
||||||
|
|
||||||
export async function nonCommandHandler(message : Message, database: Database){
|
export async function nonCommandHandler(message : Message, database: Database){
|
||||||
await sendAsMember(message, database);
|
await sendAsMember(message, database);
|
||||||
|
checkAutoProxy(message.author.id, database)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +1,21 @@
|
||||||
import { Message } from "revolt.js"
|
import { Message } from "revolt.js"
|
||||||
import { Database } from "sqlite3";
|
import { Database } from "bun:sqlite";
|
||||||
import { MemberRepo } from "../repositories/MemberRepo"
|
import { MemberRepo } from "../repositories/MemberRepo"
|
||||||
import MemberModel from "../models/MemberModel"
|
import MemberModel from "../models/MemberModel"
|
||||||
|
|
||||||
export default async function sendAsMember(message: Message, database: Database) {
|
export default async function sendAsMember(message: Message, database: Database) {
|
||||||
const alterRepo: MemberRepo = new MemberRepo(database);
|
const memberRepo: MemberRepo = new MemberRepo(database);
|
||||||
let alters: MemberModel[];
|
let members: MemberModel[];
|
||||||
await alterRepo.getAltersByUserId(message.author.id).then(result => alters = result);
|
await memberRepo.getAltersByUserId(message.author.id).then(result => members = result);
|
||||||
|
if (members === undefined) {return}
|
||||||
|
|
||||||
alters.forEach( async alter => {
|
members.forEach( async alter => {
|
||||||
const pre_prefix = alter.prefix.split("text");
|
const pre_prefix = alter.prefix.split("text");
|
||||||
if (message.content.startsWith(pre_prefix[0]) && message.content.endsWith(pre_prefix[1])) {
|
if (
|
||||||
|
message.content.startsWith(pre_prefix[0])
|
||||||
|
&& message.content.endsWith(pre_prefix[1]
|
||||||
|
&& message.content.length > 1)
|
||||||
|
) {
|
||||||
let actualContent: string = message.content;
|
let actualContent: string = message.content;
|
||||||
actualContent = actualContent.slice(pre_prefix[0].length, actualContent.length - pre_prefix[1].length)
|
actualContent = actualContent.slice(pre_prefix[0].length, actualContent.length - pre_prefix[1].length)
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,9 @@
|
||||||
"esModuleInterop": true,
|
"esModuleInterop": true,
|
||||||
"resolveJsonModule" : true,
|
"resolveJsonModule" : true,
|
||||||
"target": "es6",
|
"target": "es6",
|
||||||
|
"types": [
|
||||||
|
"bun-types"
|
||||||
|
],
|
||||||
"moduleResolution": "node",
|
"moduleResolution": "node",
|
||||||
"sourceMap": true,
|
"sourceMap": true,
|
||||||
"outDir": "dist",
|
"outDir": "dist",
|
||||||
|
|
Loading…
Reference in a new issue