feat: Finished rewriting the bot, finally
This commit is contained in:
parent
28ea67ea41
commit
86e8d2582e
15 changed files with 269 additions and 36 deletions
6
.gitignore
vendored
6
.gitignore
vendored
|
@ -1,7 +1,9 @@
|
|||
node_modules
|
||||
build
|
||||
dist
|
||||
package-lock.json
|
||||
npm-debug.log
|
||||
.nyc
|
||||
.env
|
||||
.DS_Store
|
||||
config.json
|
||||
config.json
|
||||
*.db
|
28
src/commands/avatarAlterChange.ts
Normal file
28
src/commands/avatarAlterChange.ts
Normal 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 >"
|
||||
}
|
||||
|
||||
}
|
54
src/commands/createAlter.ts
Normal file
54
src/commands/createAlter.ts
Normal 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"
|
||||
}
|
||||
}
|
||||
}
|
24
src/commands/deleteAlter.ts
Normal file
24
src/commands/deleteAlter.ts
Normal 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"
|
||||
}
|
||||
}
|
16
src/commands/listAlters.ts
Normal file
16
src/commands/listAlters.ts
Normal 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"
|
||||
}
|
||||
}
|
11
src/index.ts
11
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);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
|
8
src/models/alterModel.ts
Normal file
8
src/models/alterModel.ts
Normal file
|
@ -0,0 +1,8 @@
|
|||
export interface AlterModel {
|
||||
id?: number;
|
||||
owner: string;
|
||||
prefix: string;
|
||||
name: string;
|
||||
profile_pic_url: string;
|
||||
color?: string;
|
||||
}
|
7
src/models/alterModel.ts~
Normal file
7
src/models/alterModel.ts~
Normal file
|
@ -0,0 +1,7 @@
|
|||
export interface AlterModel {
|
||||
id?: number;
|
||||
owner: string;
|
||||
prefix: string;
|
||||
name: string;
|
||||
profile_pic_url: string;
|
||||
}
|
39
src/repositories/AlterRepo.ts
Normal file
39
src/repositories/AlterRepo.ts
Normal 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 + "'")
|
||||
}
|
||||
|
||||
}
|
|
@ -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)
|
||||
)
|
||||
}
|
||||
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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
5
src/utils/dbInit.ts
Normal file
|
@ -0,0 +1,5 @@
|
|||
|
||||
|
||||
export function dbInit(){
|
||||
|
||||
}
|
45
src/utils/nonCommandHandler.ts
Normal file
45
src/utils/nonCommandHandler.ts
Normal 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.")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
11
src/utils/tableConstructor.ts
Normal file
11
src/utils/tableConstructor.ts
Normal 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
|
||||
}
|
Loading…
Reference in a new issue