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": "",
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"@types/sqlite3": "^3.1.8",
|
||||
"bun-types": "^1.0.31",
|
||||
"typescript": "^5.1.3",
|
||||
"typescript-language-server": "^3.3.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"revolt.js": "^7.0.0-beta.9",
|
||||
"shlex": "^2.1.2",
|
||||
"sqlite3": "^5.1.6"
|
||||
"shlex": "^2.1.2"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { Database } from "sqlite3";
|
||||
import { Database } from "bun:sqlite";
|
||||
import MemberModel from "../models/MemberModel"
|
||||
import { MemberRepo } from "../repositories/MemberRepo"
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { MemberRepo } from "../repositories/MemberRepo"
|
||||
import MemberModel from "../models/MemberModel"
|
||||
import { Database } from "sqlite3";
|
||||
import { Database } from "bun:sqlite";
|
||||
|
||||
async function colorMemberChange(userId: string, args: string[], database: 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 MemberModel from "models/MemberModel"
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { MemberRepo } from "../repositories/MemberRepo"
|
||||
import MemberModel from "../models/MemberModel"
|
||||
import { Database } from "sqlite3";
|
||||
import { Database } from "bun:sqlite";
|
||||
|
||||
export async function deleteMember(userId: string, args: string[], database: Database) {
|
||||
const memberRepo = new MemberRepo(database);
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import { MemberRepo } from "../repositories/MemberRepo"
|
||||
import { tableConstructor } from '../utils/tableConstructor'
|
||||
import { Database } from "sqlite3";
|
||||
import { Database } from "bun:sqlite";
|
||||
import MemberModel from "../models/MemberModel";
|
||||
|
||||
async function listMember(userId: string, database: Database) {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { File } from "revolt.js"
|
||||
import { Database } from "sqlite3"
|
||||
import { Database } from "bun:sqlite"
|
||||
import createMember from "./createMember"
|
||||
import { avatarMemberChange } from "./avatarMemberChange"
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { Client, Message } from "revolt.js";
|
||||
import { commandHandler } from "./utils/commandHandler"
|
||||
import { nonCommandHandler } from "./utils/nonCommandHandler"
|
||||
import { Database } from "sqlite3";
|
||||
import { Database } from "bun:sqlite";
|
||||
import config from "../config.json"
|
||||
import dbInit from "utils/dbInit";
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { Database } from 'sqlite3';
|
||||
import { Database } from 'bun:sqlite';
|
||||
import MemberModel from '../models/MemberModel';
|
||||
|
||||
export class MemberRepo {
|
||||
|
@ -9,30 +9,33 @@ export class MemberRepo {
|
|||
}
|
||||
|
||||
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) => {
|
||||
this.db.all(query, (err, row: MemberModel[]) => {
|
||||
if (err) { return reject(err)}
|
||||
else { return resolve(row) }
|
||||
})
|
||||
let result: MemberModel[] = query.all(userId).map( (row: any) => {
|
||||
const mapped_row: MemberModel = {
|
||||
id: row.id,
|
||||
"name": row.name,
|
||||
"prefix": row.prefix,
|
||||
"owner": row.owner,
|
||||
"profile_pic_url": row.profile_pic_url
|
||||
}
|
||||
return mapped_row
|
||||
})
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
addAlterForUser(alter: MemberModel){
|
||||
this.db.run("INSERT INTO members (owner, prefix, name, profile_pic_url, color) VALUES(?, ?, ?, ?, ?)",
|
||||
[alter.owner, alter.prefix, alter.name, alter.profile_pic_url])
|
||||
this.db.query("INSERT INTO members (owner, prefix, name, profile_pic_url) VALUES(?, ?, ?, ?)")
|
||||
.run(alter.owner, alter.prefix, alter.name, alter.profile_pic_url)
|
||||
}
|
||||
|
||||
editAlter(alter: MemberModel){
|
||||
this.db.run("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])
|
||||
this.db.query("UPDATE members SET owner=?, prefix=?, name=?, profile_pic_url=?, color=? WHERE members.id=?")
|
||||
.run(alter.owner, alter.prefix, alter.name, alter.profile_pic_url, alter.color, alter.id)
|
||||
}
|
||||
|
||||
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 {
|
||||
db: Database
|
||||
|
@ -7,8 +8,41 @@ class UserRepo {
|
|||
this.db = database
|
||||
}
|
||||
|
||||
getById() {}
|
||||
new() {}
|
||||
delete() {}
|
||||
getById(id) {
|
||||
const row = this.db.run(`
|
||||
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 { Database } from "sqlite3";
|
||||
import UserRepo from "repositories/UserRepo";
|
||||
import { Database } from "bun:sqlite";
|
||||
|
||||
async function checkAutoProxy
|
||||
function checkAutoProxy
|
||||
(
|
||||
userId: number,
|
||||
userId: string,
|
||||
database: Database
|
||||
): Promise<Boolean>
|
||||
)
|
||||
{
|
||||
const repo = new MemberRepo(database)
|
||||
|
||||
return true
|
||||
const repo = new UserRepo(database)
|
||||
const user_id = repo.getIdByRevoltId(userId)
|
||||
const is_enabled = repo.isProxyEnabled(user_id)
|
||||
return is_enabled
|
||||
}
|
||||
|
||||
export default checkAutoProxy;
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { Message } from "revolt.js"
|
||||
import { Database } from "sqlite3"
|
||||
import { Database } from "bun:sqlite"
|
||||
import { split } from "shlex"
|
||||
|
||||
import { returnHelpText } from "../commands/help"
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { Database } from "sqlite3";
|
||||
import { Database } from "bun:sqlite";
|
||||
|
||||
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)");
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
import { Message } from "revolt.js"
|
||||
import { Database } from "sqlite3";
|
||||
import { Database } from "bun:sqlite";
|
||||
import sendAsMember from "./sendAsMemberHandler";
|
||||
import checkAutoProxy from "./checkAutoProxy";
|
||||
|
||||
export async function nonCommandHandler(message : Message, database: Database){
|
||||
await sendAsMember(message, database);
|
||||
checkAutoProxy(message.author.id, database)
|
||||
}
|
||||
|
|
|
@ -1,16 +1,21 @@
|
|||
import { Message } from "revolt.js"
|
||||
import { Database } from "sqlite3";
|
||||
import { Database } from "bun:sqlite";
|
||||
import { MemberRepo } from "../repositories/MemberRepo"
|
||||
import MemberModel from "../models/MemberModel"
|
||||
|
||||
export default async function sendAsMember(message: Message, database: Database) {
|
||||
const alterRepo: MemberRepo = new MemberRepo(database);
|
||||
let alters: MemberModel[];
|
||||
await alterRepo.getAltersByUserId(message.author.id).then(result => alters = result);
|
||||
const memberRepo: MemberRepo = new MemberRepo(database);
|
||||
let members: MemberModel[];
|
||||
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");
|
||||
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;
|
||||
actualContent = actualContent.slice(pre_prefix[0].length, actualContent.length - pre_prefix[1].length)
|
||||
|
||||
|
|
|
@ -4,6 +4,9 @@
|
|||
"esModuleInterop": true,
|
||||
"resolveJsonModule" : true,
|
||||
"target": "es6",
|
||||
"types": [
|
||||
"bun-types"
|
||||
],
|
||||
"moduleResolution": "node",
|
||||
"sourceMap": true,
|
||||
"outDir": "dist",
|
||||
|
|
Loading…
Reference in a new issue