added auto proxy

This commit is contained in:
Ale 2024-03-17 17:13:51 +01:00
parent 0a59132329
commit 962ff117ed
16 changed files with 238 additions and 33 deletions

View file

@ -4,8 +4,7 @@ import { MemberRepo } from "../repositories/MemberRepo"
export async function avatarMemberChange(userId: string, args: string[], database: Database){
const memberRepo = new MemberRepo(database);
let userAlters : MemberModel[] = [];
await memberRepo.getAltersByUserId(userId).then( result => userAlters = result )
const userAlters : MemberModel[] = memberRepo.getAltersByUserId(userId)
let userAltersNames : string[] = [];
userAlters.forEach(alter => userAltersNames.push(alter.name));

View file

@ -10,8 +10,7 @@ async function colorMemberChange(userId: string, args: string[], database: Datab
const alterName = args[0];
const alterColor = args[1];
let alters: MemberModel[];
await memberRepo.getAltersByUserId(userId).then( result => alters = result ) // fetching alters from user.
const alters: MemberModel[] = memberRepo.getAltersByUserId(userId) // fetching alters from user.
if (alters.length == 0) return "This user has no Members." // checking if returned array is at least not 0, if len 0, then return.
let alterToEdit: MemberModel;

View file

@ -12,8 +12,7 @@ async function createMember(userId: string, args: string[], database: Database){
profile_pic_url: "https://tse3.mm.bing.net/th?id=OIP.yte7rRnbCnWi1giriwTOvwHaHa&pid=15.1"
}
let userAlters: MemberModel[];
await memberRepo.getAltersByUserId(userId).then(result => userAlters = result)
const userAlters: MemberModel[] = memberRepo.getAltersByUserId(userId)
let alterTags : string[] = [];
userAlters.forEach( alter => {

View file

@ -7,8 +7,7 @@ export async function deleteMember(userId: string, args: string[], database: Dat
let success: boolean = false;
if (args.length === 1) {
let alters: MemberModel[];
await memberRepo.getAltersByUserId(userId).then(result => alters = result )
const alters: MemberModel[] = memberRepo.getAltersByUserId(userId)
alters.forEach(alter => {
if (alter.name == args[0]) {
memberRepo.delete(alter.id)

View file

@ -11,5 +11,8 @@ export function returnHelpText() {
"- avatar <'Member's name'> <'picture url'> | Allows you to edit your Member's profile picture\n" +
"- color <'Member's name'> <'color hex'> | Allows you to change the color of your Member ( may be integrated with the future website )\n"+
"### Migrations\n"+
"- tupper < Json file attached > | Migrates your Member from tupper"
"- tupper < Json file attached > | Migrates your Member from tupper\n"
+"### Auto proxy\n"
+"- setAutoProxy <member name> | Sets the member to automatically send messages as\n"
+"- toggleAutoProxy | Enable or disable the autoproxy"
}

View file

@ -5,8 +5,7 @@ import MemberModel from "../models/MemberModel";
async function listMember(userId: string, database: Database) {
const memberRepo = new MemberRepo(database);
let alters: MemberModel[];
await memberRepo.getAltersByUserId(userId).then(result => { alters = result })
const alters: MemberModel[] = memberRepo.getAltersByUserId(userId)
if (alters.length > 0 ) {
return tableConstructor(alters)

View file

@ -0,0 +1,30 @@
import Database from "bun:sqlite";
import { MemberRepo } from "repositories/MemberRepo";
import UserRepo from "repositories/UserRepo";
import UserModel from "models/UserModel";
function setAutoProxy(author_id: string, args: string[], database: Database) {
if (args.length != 1) {return 0}
const memberRepo = new MemberRepo(database);
const userRepo = new UserRepo(database);
const member = memberRepo.getByName(args[0], author_id);
if (member == null) {return 1}
if (
userRepo.getIdByRevoltId(author_id)
){
const user_model: UserModel = {
"revolt_id": author_id,
"autoProxy": false
}
userRepo.new(user_model)
}
const bot_user_id = userRepo.getIdByRevoltId(author_id)
userRepo.setSelectedAutoProxyId(member.id, bot_user_id)
}
export default setAutoProxy

View file

@ -0,0 +1,25 @@
import Database from "bun:sqlite"
import UserModel from "models/UserModel"
import UserRepo from "repositories/UserRepo"
function toggleAutoProxy(revolt_id: string, database: Database) {
const userRepo = new UserRepo(database)
if (userRepo.getById(
userRepo.getIdByRevoltId(revolt_id)
) == null){
const model: UserModel = {
"revolt_id": revolt_id,
"autoProxy": false,
}
userRepo.new(model)
}
const user_id = userRepo.getIdByRevoltId(revolt_id)
const is_enabled = userRepo.isProxyEnabled(
userRepo.getIdByRevoltId(revolt_id)
)
userRepo.setProxyStatus(user_id, !is_enabled)
}
export default toggleAutoProxy

View file

@ -1,8 +1,8 @@
interface UserModel {
id: number,
id?: number,
revolt_id: string,
autoProxy: boolean,
autoProxyMember: number
autoProxyMember?: number
}
export default UserModel;

View file

@ -8,12 +8,51 @@ export class MemberRepo {
this.db = database_instance;
}
async getAltersByUserId(userId : string) : Promise<MemberModel[]> {
getById(member_id: number): MemberModel {
const row: any = this.db.query("SELECT * FROM members WHERE members.id=?")
.get(member_id)
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
}
getByName(name: string, user_id: string) {
if (name == null || user_id == null) {return null}
const query = this.db.query(`
SELECT *
FROM members
WHERE members.name=?
AND members.owner=?
`)
const row: any = query.get(name, user_id)
if (row == null) {return null}
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
}
getAltersByUserId(userId : string) : MemberModel[] {
const query = this.db.query("SELECT * FROM members WHERE members.owner=?")
let result: MemberModel[] = query.all(userId).map( (row: any) => {
const mapped_row: MemberModel = {
id: row.id,
"id": row.id,
"name": row.name,
"prefix": row.prefix,
"owner": row.owner,
@ -37,5 +76,4 @@ export class MemberRepo {
delete(alterId: number) {
this.db.query("DELETE FROM members WHERE members.id=?").run(alterId)
}
}

View file

@ -9,18 +9,33 @@ class UserRepo {
}
getById(id) {
const row = this.db.run(`
const row: any = this.db.query(`
SELECT *
FROM users
WHERE users.id = ?
`, [id])
return row
`).get(id)
if (row != null){
return row.id
}
return null
}
new(model: UserModel) {
this.db.query(`
INSERT INTO users (revolt_id, auto_proxy) values ( ?, ? )
`)
.run(model.revolt_id, model.autoProxy)
}
delete(id) {
this.db.query(`
DELETE *
FROM users
WHERE users.id=?
`).run(id)
}
new(model: UserModel) {}
delete(id) {}
isProxyEnabled(id: number) {
if (id == null) { return false }
const row: any = this.db.query(`
SELECT auto_proxy
FROM users
@ -33,15 +48,41 @@ class UserRepo {
return is_enabled
}
getIdByRevoltId(revolt_id: string){
setProxyStatus(id: number, status: boolean) {
const string_status = status == true ? "TRUE" : "FALSE"
this.db.query("UPDATE users SET auto_proxy=? WHERE users.id=?").run(string_status, id)
}
getIdByRevoltId(revolt_id: string): number | null {
const row: any = this.db.query(`
SELECT id
FROM users
WHERE users.revolt_id=?
`).get(revolt_id)
const id: number = row.id
return id
if (row != null) {
const id: number = row.id
return id
}
return null
}
setSelectedAutoProxyId(id: number, user_id: number){
const query = this.db.query(`
UPDATE users
SET auto_proxy_member=?
WHERE users.id=?
`).run(id, user_id)
}
getSelectedAutoProxyIdByUserId(id: number) {
const row: any = this.db.query(`
SELECT auto_proxy_member
FROM users
WHERE users.id=?
`).get(id)
return row.auto_proxy_member
}
}

View file

@ -5,11 +5,12 @@ function checkAutoProxy
(
userId: string,
database: Database
)
): boolean
{
const repo = new UserRepo(database)
const user_id = repo.getIdByRevoltId(userId)
const is_enabled = repo.isProxyEnabled(user_id)
const repo = new UserRepo(database);
const user_id = repo.getIdByRevoltId(userId);
const is_enabled = repo.isProxyEnabled(user_id);
return is_enabled
}

View file

@ -9,6 +9,8 @@ import { deleteMember } from "../commands/deleteMember"
import { avatarMemberChange } from "../commands/avatarMemberChange"
import colorMemberChange from "../commands/colorMemberChange"
import migrationCommand from "../commands/migrateMember"
import toggleAutoProxy from "commands/toggleAutoProxy"
import setAutoProxy from "commands/setAutoProxy"
@ -40,6 +42,25 @@ export async function commandHandler(message : Message, db: Database, _prefix :
await message.reply(await avatarMemberChange(message.author.id, args, db));
break
}
case "setAutoProxy": {
const status = setAutoProxy(message.author.id, args, db);
switch(status) {
case 0: {
await message.reply("not enough arguments")
break
}
case 1: {
message.reply("no member found with that member name")
break
}
}
break
}
case "toggleAutoProxy": {
toggleAutoProxy(message.author.id, db)
await message.reply("toggled")
break
}
case "color" : {
await message.reply(await colorMemberChange(message.author.id, args, db));
break

View file

@ -2,8 +2,15 @@ import { Message } from "revolt.js"
import { Database } from "bun:sqlite";
import sendAsMember from "./sendAsMemberHandler";
import checkAutoProxy from "./checkAutoProxy";
import sendAsAutoProxyMember from "./sendAsAutoProxyMember";
export async function nonCommandHandler(message : Message, database: Database){
await sendAsMember(message, database);
checkAutoProxy(message.author.id, database)
const should_auto_proxy = checkAutoProxy(message.author.id, database)
if (should_auto_proxy) {
await sendAsAutoProxyMember(message.author.id, message, database)
}
else {
await sendAsMember(message, database);
}
}

View file

@ -0,0 +1,45 @@
import Database from "bun:sqlite";
import { Message } from "revolt.js"
import { MemberRepo } from "repositories/MemberRepo";
import UserRepo from "repositories/UserRepo";
import MemberModel from "models/MemberModel";
async function sendAsAutoProxyMember(revolt_uid: string, message: Message, database: Database) {
// assumes that every check has been done previously
const userRepo = new UserRepo(database)
const memberRepo = new MemberRepo(database)
const user_id = userRepo.getIdByRevoltId(revolt_uid)
const auto_proxy_id = userRepo.getSelectedAutoProxyIdByUserId(user_id)
const member_to_proxy_as: MemberModel = memberRepo.getById(auto_proxy_id)
const replyIds: string[] | undefined = message.replyIds;
let replies: any[] = [];
if (replyIds !== undefined) {
replyIds.forEach( replyId => {
replies.push({
id: replyId,
mention: false
})
})
}
try{
await message.channel.sendMessage({
content: message.content,
masquerade: {
name: member_to_proxy_as.name ,
},
replies: replies
})
await message.delete();
}
catch(e){
console.log(e)
await message.channel.sendMessage("Error: PluralCake requires at least these permissions: \n- Masquerade permissions. \n- Message editing permissions.")
}
}
export default sendAsAutoProxyMember

View file

@ -5,8 +5,7 @@ import MemberModel from "../models/MemberModel"
export default async function sendAsMember(message: Message, database: Database) {
const memberRepo: MemberRepo = new MemberRepo(database);
let members: MemberModel[];
await memberRepo.getAltersByUserId(message.author.id).then(result => members = result);
let members: MemberModel[] = memberRepo.getAltersByUserId(message.author.id);
members.forEach( async alter => {
const pre_prefix = alter.prefix.split("text");