From 86e8d2582eb187f9a1c6726acf9dbf901e63342a Mon Sep 17 00:00:00 2001 From: Ale Date: Sat, 2 Sep 2023 23:21:55 +0200 Subject: [PATCH] feat: Finished rewriting the bot, finally --- .gitignore | 6 ++-- src/commands/avatarAlterChange.ts | 28 ++++++++++++++++ src/commands/createAlter.ts | 54 +++++++++++++++++++++++++++++++ src/commands/deleteAlter.ts | 24 ++++++++++++++ src/commands/listAlters.ts | 16 +++++++++ src/index.ts | 11 +++++-- src/models/alterModel.ts | 8 +++++ src/models/alterModel.ts~ | 7 ++++ src/repositories/AlterRepo.ts | 39 ++++++++++++++++++++++ src/repositories/CharacterRepo.ts | 15 --------- src/services/CharacterService.ts | 13 -------- src/utils/commandHandler.ts | 23 +++++++++++-- src/utils/dbInit.ts | 5 +++ src/utils/nonCommandHandler.ts | 45 ++++++++++++++++++++++++++ src/utils/tableConstructor.ts | 11 +++++++ 15 files changed, 269 insertions(+), 36 deletions(-) create mode 100644 src/commands/avatarAlterChange.ts create mode 100644 src/commands/createAlter.ts create mode 100644 src/commands/deleteAlter.ts create mode 100644 src/commands/listAlters.ts create mode 100644 src/models/alterModel.ts create mode 100644 src/models/alterModel.ts~ create mode 100644 src/repositories/AlterRepo.ts delete mode 100644 src/repositories/CharacterRepo.ts delete mode 100644 src/services/CharacterService.ts create mode 100644 src/utils/dbInit.ts create mode 100644 src/utils/nonCommandHandler.ts create mode 100644 src/utils/tableConstructor.ts diff --git a/.gitignore b/.gitignore index 39cdef8..2999d61 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,9 @@ node_modules -build +dist +package-lock.json npm-debug.log .nyc .env .DS_Store -config.json \ No newline at end of file +config.json +*.db \ No newline at end of file diff --git a/src/commands/avatarAlterChange.ts b/src/commands/avatarAlterChange.ts new file mode 100644 index 0000000..ff262ff --- /dev/null +++ b/src/commands/avatarAlterChange.ts @@ -0,0 +1,28 @@ +import { AlterModel } from "../models/alterModel" +import { AlterRepo } from "../repositories/AlterRepo" + +export async function avatarAlterChange(userId: string, args: string[]){ + const alterRepo = new AlterRepo(); + let userAlters : AlterModel[] = []; + await alterRepo.getAltersByUserId(userId).then( result => userAlters = result ) + + let userAltersNames : string[] = []; + userAlters.forEach(alter => userAltersNames.push(alter.name)); + + if (args.length == 2) { + if (userAltersNames.includes(args[0])){ + userAlters.forEach( alter => { + if (alter.name == args[0]) { + alter.profile_pic_url = args[1]; + alterRepo.editAlter(alter) + } + }) + } + return "The profile picture has been changed" + } + + if (args.length != 2) { + return "Error: Insufficent number of arguments. \nexample: !ck avatar < name > < url >" + } + +} diff --git a/src/commands/createAlter.ts b/src/commands/createAlter.ts new file mode 100644 index 0000000..93e648e --- /dev/null +++ b/src/commands/createAlter.ts @@ -0,0 +1,54 @@ +import { AlterRepo } from "../repositories/AlterRepo"; +import { AlterModel } from "models/alterModel" + +export async function createAlter(userId: string, args: string[]){ + const alterRepo = new AlterRepo(); + + const model: AlterModel = { + 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 alterTags : string[] = []; + userAlters.forEach( alter => { + alterTags.push(alter.prefix) + }) + + let alterNames : string[] = []; + userAlters.forEach ( alter => { + alterNames.push(alter.name) + }) + + if (args.length == 2 + && args[1].includes("text") + && !alterTags.includes(args[1]) + && args[1] != "text" + && !alterNames.includes(args[0])) { + + alterRepo.addAlterForUser(model); + return "Alter < " + model.name + " > has been succefully created" + + } + else { + if (args.length != 2){ + return "Error: Insufficent arguments"; + } + if (args[1] === "text"){ + return "Error: Tag may not be only "; + } + if (!args[1].includes("text")) { + return "Error: This command requires a tag that contains in it"; + } + if (alterTags.includes(args[1])) { + return "Error: You already have an Alter with that tag" + } + if (alterNames.includes(args[0])) { + return "Error: You can only have one Alter with that name" + } + } +} diff --git a/src/commands/deleteAlter.ts b/src/commands/deleteAlter.ts new file mode 100644 index 0000000..989e881 --- /dev/null +++ b/src/commands/deleteAlter.ts @@ -0,0 +1,24 @@ +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 "Alter has been deleted" + } + else { + return "Failed to delete Alter" + } +} diff --git a/src/commands/listAlters.ts b/src/commands/listAlters.ts new file mode 100644 index 0000000..3ad9918 --- /dev/null +++ b/src/commands/listAlters.ts @@ -0,0 +1,16 @@ +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 Alters found for this user" + } +} diff --git a/src/index.ts b/src/index.ts index b232baa..77e09ed 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,19 +1,24 @@ import { Client, Message } from "revolt.js"; import { commandHandler } from "./utils/commandHandler" +import { nonCommandHandler } from "./utils/nonCommandHandler" import config from "../config.json" const client : Client = new Client(); client.on("ready", async () => { console.info(`Gummed in as ${client.user.username}!`) - client.user.edit({status : {text: config.prefix + "help for the help menu"}}) + client.user.edit({status : {text: config.prefix + " help for the help menu"}}) }); client.on("messageCreate", async (message: Message) => { if (message.author.bot) {} else { - if (message.content.startsWith(config.prefix)){commandHandler(message, config.prefix)} - else {} + if (message.content.startsWith(config.prefix)) { + commandHandler(message, config.prefix); + } + else { + nonCommandHandler(message); + } } }); diff --git a/src/models/alterModel.ts b/src/models/alterModel.ts new file mode 100644 index 0000000..2eb4e19 --- /dev/null +++ b/src/models/alterModel.ts @@ -0,0 +1,8 @@ +export interface AlterModel { + id?: number; + owner: string; + prefix: string; + name: string; + profile_pic_url: string; + color?: string; +} diff --git a/src/models/alterModel.ts~ b/src/models/alterModel.ts~ new file mode 100644 index 0000000..1dacc0d --- /dev/null +++ b/src/models/alterModel.ts~ @@ -0,0 +1,7 @@ +export interface AlterModel { + id?: number; + owner: string; + prefix: string; + name: string; + profile_pic_url: string; +} diff --git a/src/repositories/AlterRepo.ts b/src/repositories/AlterRepo.ts new file mode 100644 index 0000000..4084504 --- /dev/null +++ b/src/repositories/AlterRepo.ts @@ -0,0 +1,39 @@ +import { Database } from 'sqlite3'; +import { AlterModel } from '../models/alterModel' +import config from "../../config.json" + +export class AlterRepo { + 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)") + } + + async getAltersByUserId(userId : string) : Promise { + 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[]) => { + if (err) { return reject(err)} + else { return resolve(row) } + }) + }) + + return result + } + + addAlterForUser(alter: AlterModel){ + 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){ + 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) { + this.db.run("DELETE FROM alters WHERE alters.id='"+ alterId + "'") + } + +} diff --git a/src/repositories/CharacterRepo.ts b/src/repositories/CharacterRepo.ts deleted file mode 100644 index 75c9d2f..0000000 --- a/src/repositories/CharacterRepo.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { Database } from 'sqlite3'; -export class CharacterRepo { - db : Database - constructor() { - this.db = new Database('db.sqlite'); - } - - getAllCharacters(){ - return this.db.get( - 'SELECT * FROM CHARACTERS', - res => console.log(res) - ) - } - -} diff --git a/src/services/CharacterService.ts b/src/services/CharacterService.ts deleted file mode 100644 index 5140e20..0000000 --- a/src/services/CharacterService.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { CharacterRepo } from "repositories/CharacterRepo"; - -class CharacterService { - repo : CharacterRepo; - - constructor() { - this.repo = new CharacterRepo(); - } - - public getCharacter() : any{ - return this.repo.getAllCharacters(); - } -} \ No newline at end of file diff --git a/src/utils/commandHandler.ts b/src/utils/commandHandler.ts index 0d91210..d2b414a 100644 --- a/src/utils/commandHandler.ts +++ b/src/utils/commandHandler.ts @@ -1,11 +1,16 @@ 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 { split } from "shlex" export async function commandHandler(message : Message, prefix : String) { - let args : String[] = split(message.content); + let args : string[] = split(message.content); args.shift() const command = args[0] + args.shift() switch(command) { case "help" : { @@ -13,11 +18,23 @@ export async function commandHandler(message : Message, prefix : String) { break; } case "list" : { - message.reply("not yet implemented") + await message.reply(await listAlters(message.author.id)); break; } case "create" : { - message.reply("not yet implemented") + await message.reply(await createAlter(message.author.id, args)); + break } + case "delete" : { + await message.reply(await deleteAlter(message.author.id, args)) + break + } + case "avatar" : { + await message.reply(await avatarAlterChange(message.author.id, args)); + break + } + case "color" : { + break + } } } diff --git a/src/utils/dbInit.ts b/src/utils/dbInit.ts new file mode 100644 index 0000000..64b3d61 --- /dev/null +++ b/src/utils/dbInit.ts @@ -0,0 +1,5 @@ + + +export function dbInit(){ + +} diff --git a/src/utils/nonCommandHandler.ts b/src/utils/nonCommandHandler.ts new file mode 100644 index 0000000..e9b4f5b --- /dev/null +++ b/src/utils/nonCommandHandler.ts @@ -0,0 +1,45 @@ +import { Message } from "revolt.js" +import { AlterRepo } from "../repositories/AlterRepo" +import { AlterModel } from "../models/alterModel" + +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){ + await message.channel.sendMessage("Error: PluralCake requires at least these permissions: \n- Masquerade permissions. \n- Message editing permissions.") + } + } + }) +} diff --git a/src/utils/tableConstructor.ts b/src/utils/tableConstructor.ts new file mode 100644 index 0000000..416fa47 --- /dev/null +++ b/src/utils/tableConstructor.ts @@ -0,0 +1,11 @@ +import { AlterModel } from '../models/alterModel' +export function tableConstructor(data: AlterModel[]){ + const header = "| Name | tag |\n|----|----|\n" + let body : string = ""; + data.forEach(alter => { + body = body + + "|" + alter.name + "|" + alter.prefix + "|\n" + }) + body = body.slice(0, body.length - 2) + return header + body +}