feat: Finished rewriting the bot, finally

This commit is contained in:
Ale 2023-09-02 23:21:55 +02:00
parent 28ea67ea41
commit 86e8d2582e
No known key found for this signature in database
GPG key ID: 284AFB134BB2E07D
15 changed files with 269 additions and 36 deletions

4
.gitignore vendored
View file

@ -1,7 +1,9 @@
node_modules
build
dist
package-lock.json
npm-debug.log
.nyc
.env
.DS_Store
config.json
*.db

View file

@ -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 >"
}
}

View file

@ -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 <text>";
}
if (!args[1].includes("text")) {
return "Error: This command requires a tag that contains <text> 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"
}
}
}

View file

@ -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"
}
}

View file

@ -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"
}
}

View file

@ -1,5 +1,6 @@
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();
@ -12,8 +13,12 @@ client.on("ready", async () => {
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);
}
}
});

8
src/models/alterModel.ts Normal file
View file

@ -0,0 +1,8 @@
export interface AlterModel {
id?: number;
owner: string;
prefix: string;
name: string;
profile_pic_url: string;
color?: string;
}

View file

@ -0,0 +1,7 @@
export interface AlterModel {
id?: number;
owner: string;
prefix: string;
name: string;
profile_pic_url: string;
}

View file

@ -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<AlterModel[]> {
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 + "'")
}
}

View file

@ -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)
)
}
}

View file

@ -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();
}
}

View file

@ -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
}
}
}

5
src/utils/dbInit.ts Normal file
View file

@ -0,0 +1,5 @@
export function dbInit(){
}

View file

@ -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.")
}
}
})
}

View file

@ -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
}