changed alter to model

This commit is contained in:
Ale 2024-03-15 18:41:08 +01:00
parent 21927ae359
commit 87e8a4295d
16 changed files with 205 additions and 149 deletions

View file

@ -1,10 +1,11 @@
import { AlterModel } from "../models/alterModel"
import { AlterRepo } from "../repositories/AlterRepo"
import { Database } from "sqlite3";
import MemberModel from "../models/MemberModel"
import { MemberRepo } from "../repositories/MemberRepo"
export async function avatarAlterChange(userId: string, args: string[]){
const alterRepo = new AlterRepo();
let userAlters : AlterModel[] = [];
await alterRepo.getAltersByUserId(userId).then( result => userAlters = result )
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 )
let userAltersNames : string[] = [];
userAlters.forEach(alter => userAltersNames.push(alter.name));
@ -14,7 +15,7 @@ export async function avatarAlterChange(userId: string, args: string[]){
userAlters.forEach( alter => {
if (alter.name == args[0]) {
alter.profile_pic_url = args[1];
alterRepo.editAlter(alter)
memberRepo.editAlter(alter)
}
})
}

View file

@ -1,19 +1,20 @@
import { AlterRepo } from "../repositories/AlterRepo"
import { AlterModel } from "../models/alterModel"
import { MemberRepo } from "../repositories/MemberRepo"
import MemberModel from "../models/MemberModel"
import { Database } from "sqlite3";
export async function colorAlterChange(userId: string, args: string[]){
const alterRepo = new AlterRepo();
async function colorMemberChange(userId: string, args: string[], database: Database){
const memberRepo = new MemberRepo(database);
const regexHex = /^#[0-9A-F]{6}$/i;
if (args.length == 2 && regexHex.test(args[1])){
const alterName = args[0];
const alterColor = args[1];
let alters: AlterModel[];
await alterRepo.getAltersByUserId(userId).then( result => alters = result ) // fetching alters from user.
let alters: MemberModel[];
await memberRepo.getAltersByUserId(userId).then( result => alters = result ) // 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: AlterModel;
let alterToEdit: MemberModel;
alters.forEach( alter => {
if (alter.name == alterName) {
alterToEdit = alter;
@ -21,10 +22,12 @@ export async function colorAlterChange(userId: string, args: string[]){
})
alterToEdit.color = alterColor;
alterRepo.editAlter(alterToEdit)
memberRepo.editAlter(alterToEdit)
return "The Member's colour has been changed."
}
return "Error: Insufficent Arguments"
}
export default colorMemberChange

View file

@ -1,18 +1,19 @@
import { AlterRepo } from "../repositories/AlterRepo";
import { AlterModel } from "models/alterModel"
import { Database } from "sqlite3";
import { MemberRepo } from "../repositories/MemberRepo";
import MemberModel from "models/MemberModel"
export async function createAlter(userId: string, args: string[]){
const alterRepo = new AlterRepo();
async function createMember(userId: string, args: string[], database: Database){
const memberRepo = new MemberRepo(database);
const model: AlterModel = {
const model: MemberModel = {
owner: userId,
name: args[0],
prefix: args[1],
profile_pic_url: "https://tse3.mm.bing.net/th?id=OIP.yte7rRnbCnWi1giriwTOvwHaHa&pid=15.1"
}
let userAlters: AlterModel[];
await alterRepo.getAltersByUserId(userId).then(result => userAlters = result)
let userAlters: MemberModel[];
await memberRepo.getAltersByUserId(userId).then(result => userAlters = result)
let alterTags : string[] = [];
userAlters.forEach( alter => {
@ -30,7 +31,7 @@ export async function createAlter(userId: string, args: string[]){
&& args[1] != "text"
&& !alterNames.includes(args[0])) {
alterRepo.addAlterForUser(model);
memberRepo.addAlterForUser(model);
return {message: "Member < " + model.name + " > has been succefully created", code: 0}
}
@ -52,3 +53,5 @@ export async function createAlter(userId: string, args: string[]){
}
}
}
export default createMember;

View file

@ -1,24 +0,0 @@
import { AlterRepo } from "../repositories/AlterRepo"
import { AlterModel } from "../models/alterModel"
export async function deleteAlter(userId: string, args: string[]) {
const alterRepo = new AlterRepo();
let success: boolean = false;
if (args.length === 1) {
let alters: AlterModel[];
await alterRepo.getAltersByUserId(userId).then(result => alters = result )
alters.forEach(alter => {
if (alter.name == args[0]) {
alterRepo.deleteAlter(alter.id)
success = true;
}
})
}
if (success) {
return "Member has been deleted"
}
else {
return "Failed to delete Member"
}
}

View file

@ -0,0 +1,25 @@
import { MemberRepo } from "../repositories/MemberRepo"
import MemberModel from "../models/MemberModel"
import { Database } from "sqlite3";
export async function deleteMember(userId: string, args: string[], database: Database) {
const memberRepo = new MemberRepo(database);
let success: boolean = false;
if (args.length === 1) {
let alters: MemberModel[];
await memberRepo.getAltersByUserId(userId).then(result => alters = result )
alters.forEach(alter => {
if (alter.name == args[0]) {
memberRepo.delete(alter.id)
success = true;
}
})
}
if (success) {
return "Member has been deleted"
}
else {
return "Failed to delete Member"
}
}

View file

@ -1,16 +0,0 @@
import { AlterRepo } from "../repositories/AlterRepo"
import { AlterModel } from "../models/alterModel"
import { tableConstructor } from '../utils/tableConstructor'
export async function listAlters(userId: string) {
const alterRepo = new AlterRepo();
let alters: AlterModel[];
await alterRepo.getAltersByUserId(userId).then(result => { alters = result })
if (alters.length > 0 ) {
return tableConstructor(alters)
}
else {
return "No Members found for this user"
}
}

View file

@ -0,0 +1,19 @@
import { MemberRepo } from "../repositories/MemberRepo"
import { tableConstructor } from '../utils/tableConstructor'
import { Database } from "sqlite3";
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 })
if (alters.length > 0 ) {
return tableConstructor(alters)
}
else {
return "No Members found for this user"
}
}
export default listMember

View file

@ -1,8 +1,9 @@
import { File } from "revolt.js"
import { createAlter } from "./createAlter"
import { avatarAlterChange } from "./avatarAlterChange"
import { Database } from "sqlite3"
import createMember from "./createMember"
import { avatarMemberChange } from "./avatarMemberChange"
export default async function migrateAlters(author: string, attachments: File[] | undefined, source: String) {
export default async function migrateAlters(author: string, attachments: File[] | undefined, source: String, database: Database) {
// source is not utilized for now, it is put as an argument in case the bot evolves further
let file = attachments[0]
@ -24,9 +25,9 @@ export default async function migrateAlters(author: string, attachments: File[]
let name = element.name
let avatar = element.avatar_url
let brackets = element.brackets[0] + "text" + element.brackets[1]
const createResult = await createAlter(author, [name, brackets])
const createResult = await createMember(author, [name, brackets], database)
if ( createResult.code === 0 ) {
await avatarAlterChange(author, [name, avatar])
await avatarMemberChange(author, [name, avatar], database)
}
let tupperStruct = {tupper: name, message: createResult}

View file

@ -1,28 +1,37 @@
import { Client, Message } from "revolt.js";
import { commandHandler } from "./utils/commandHandler"
import { nonCommandHandler } from "./utils/nonCommandHandler"
import { Database } from "sqlite3";
import config from "../config.json"
const client : Client = new Client({ eagerFetching: false });
const client : Client = new Client({ eagerFetching: false });
const db = new Database(config.databaseName);
client.on("ready", async () => {
console.info(`Gummed in as ${client.user.username}!`)
client.user.edit({status : {text: config.prefix + " help for the help menu"}})
console.info(`logged in as ${client.user.username}!`)
client.user.edit(
{
status : {
text: config.prefix + " help for the help menu"
}
}
)
});
client.on("messageCreate", async (message: Message) => {
try{ if (!message.author) { await client.users.fetch(message.authorId) } }
catch(e){console.log(e)}
if (message.content === undefined) {return}
if (message.author?.bot) {}
else {
if (message.author?.bot) {return}
// checks if the message's body starts with the prefix
if (message.content.startsWith(config.prefix)) {
commandHandler(message, config.prefix);
commandHandler(message, db, config.prefix);
}
else {
nonCommandHandler(message);
}
nonCommandHandler(message, db);
}
});

View file

@ -1,4 +1,4 @@
export interface AlterModel {
interface MemberModel {
id?: number;
owner: string;
prefix: string;
@ -6,3 +6,5 @@ export interface AlterModel {
profile_pic_url: string;
color?: string;
}
export default MemberModel;

7
src/models/User.ts Normal file
View file

@ -0,0 +1,7 @@
interface UserModel {
id: number,
autoProxy: boolean,
autoProxyMember: number
}
export default UserModel;

View file

@ -1,19 +1,19 @@
import { Database } from 'sqlite3';
import { AlterModel } from '../models/alterModel'
import config from "../../config.json"
import MemberModel from '../models/MemberModel';
export class AlterRepo {
export class MemberRepo {
db : Database
constructor() {
this.db = new Database(config.databaseName);
this.db.run("CREATE TABLE IF NOT EXISTS alters (id INTEGER PRIMARY KEY AUTOINCREMENT, owner TEXT, prefix TEXT, name TEXT, profile_pic_url TEXT, color TEXT)")
constructor(database_instance: Database) {
this.db = database_instance;
this.db.run("CREATE TABLE IF NOT EXISTS alters (id INTEGER PRIMARY KEY AUTOINCREMENT, owner TEXT, prefix TEXT, name TEXT, profile_pic_url TEXT, color TEXT)");
}
async getAltersByUserId(userId : string) : Promise<AlterModel[]> {
async getAltersByUserId(userId : string) : Promise<MemberModel[]> {
const query : string = 'SELECT * FROM alters WHERE alters.owner = "' + userId + '"'
let result: AlterModel[] = await new Promise((resolve, reject) => {
this.db.all(query, (err, row: AlterModel[]) => {
let result: MemberModel[] = await new Promise((resolve, reject) => {
this.db.all(query, (err, row: MemberModel[]) => {
if (err) { return reject(err)}
else { return resolve(row) }
})
@ -22,17 +22,17 @@ export class AlterRepo {
return result
}
addAlterForUser(alter: AlterModel){
addAlterForUser(alter: MemberModel){
this.db.run("INSERT INTO alters (owner, prefix, name, profile_pic_url, color) VALUES(?, ?, ?, ?, ?)",
[alter.owner, alter.prefix, alter.name, alter.profile_pic_url])
}
editAlter(alter: AlterModel){
editAlter(alter: MemberModel){
this.db.run("UPDATE alters SET owner=?, prefix=?, name=?, profile_pic_url=?, color=? WHERE alters.id ='" + alter.id + "'",
[alter.owner, alter.prefix, alter.name, alter.profile_pic_url, alter.color])
}
deleteAlter(alterId: number) {
delete(alterId: number) {
this.db.run("DELETE FROM alters WHERE alters.id='"+ alterId + "'")
}

View file

@ -0,0 +1,15 @@
import { MemberRepo } from "repositories/MemberRepo";
import { Database } from "sqlite3";
async function checkAutoProxy
(
userId: number,
database: Database
): Promise<Boolean>
{
const repo = new MemberRepo(database)
return true
}
export default checkAutoProxy;

View file

@ -1,15 +1,18 @@
import { Message } from "revolt.js"
import { returnHelpText } from "../commands/help"
import { listAlters } from "../commands/listAlters"
import { createAlter } from "../commands/createAlter"
import { deleteAlter } from "../commands/deleteAlter"
import { avatarAlterChange } from "../commands/avatarAlterChange"
import { colorAlterChange } from "../commands/colorAlterChange"
import migrationCommand from "../commands/migrateAlters"
import { Database } from "sqlite3"
import { split } from "shlex"
export async function commandHandler(message : Message, _prefix : String) {
import { returnHelpText } from "../commands/help"
import listMember from "../commands/listMember"
import createMember from "../commands/createMember"
import { deleteMember } from "../commands/deleteMember"
import { avatarMemberChange } from "../commands/avatarMemberChange"
import colorMemberChange from "../commands/colorMemberChange"
import migrationCommand from "../commands/migrateMember"
export async function commandHandler(message : Message, db: Database, _prefix : String) {
let args : string[] = split(message.content);
args.shift()
const command = args[0]
@ -21,28 +24,28 @@ export async function commandHandler(message : Message, _prefix : String) {
break;
}
case "list" : {
await message.reply(await listAlters(message.author.id));
await message.reply(await listMember(message.author.id, db));
break;
}
case "create" : {
const commandResponse = await createAlter(message.author.id, args);
const commandResponse = await createMember(message.author.id, args, db);
await message.reply (commandResponse.message)
break
}
case "delete" : {
await message.reply(await deleteAlter(message.author.id, args))
await message.reply(await deleteMember(message.author.id, args, db))
break
}
case "avatar" : {
await message.reply(await avatarAlterChange(message.author.id, args));
await message.reply(await avatarMemberChange(message.author.id, args, db));
break
}
case "color" : {
await message.reply(await colorAlterChange(message.author.id, args));
await message.reply(await colorMemberChange(message.author.id, args, db));
break
}
case "tupper" : {
await message.reply(await migrationCommand(message.author.id, message.attachments, "placeholder"))
await message.reply(await migrationCommand(message.author.id, message.attachments, "placeholder", db))
break
}
}

View file

@ -1,46 +1,7 @@
import { Message } from "revolt.js"
import { AlterRepo } from "../repositories/AlterRepo"
import { AlterModel } from "../models/alterModel"
import { Database } from "sqlite3";
import sendAsMember from "./sendAsMemberHandler";
export async function nonCommandHandler(message : Message){
const alterRepo: AlterRepo = new AlterRepo();
let alters: AlterModel[];
await alterRepo.getAltersByUserId(message.author.id).then(result => alters = result);
alters.forEach( async alter => {
const pre_prefix = alter.prefix.split("text");
if (message.content.startsWith(pre_prefix[0]) && message.content.endsWith(pre_prefix[1])) {
let actualContent: string = message.content;
actualContent = actualContent.slice(pre_prefix[0].length, actualContent.length - pre_prefix[1].length)
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: actualContent,
masquerade: {
name: alter.name ,
avatar: alter.profile_pic_url,
color: alter.color
},
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 async function nonCommandHandler(message : Message, database: Database){
await sendAsMember(message, database);
}

View file

@ -0,0 +1,47 @@
import { Message } from "revolt.js"
import { Database } from "sqlite3";
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);
alters.forEach( async alter => {
const pre_prefix = alter.prefix.split("text");
if (message.content.startsWith(pre_prefix[0]) && message.content.endsWith(pre_prefix[1])) {
let actualContent: string = message.content;
actualContent = actualContent.slice(pre_prefix[0].length, actualContent.length - pre_prefix[1].length)
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: actualContent,
masquerade: {
name: alter.name ,
avatar: alter.profile_pic_url,
color: alter.color
},
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.")
}
}
})
}