From 51e198810a831af92ce1892e32370e6eab10d141 Mon Sep 17 00:00:00 2001 From: markokajzer Date: Mon, 19 Mar 2018 19:53:30 +0100 Subject: [PATCH] Made !rename, !remove update the db --- src/commands/CommandCollection.ts | 4 ++-- src/commands/soundbot/RemoveCommand.ts | 8 ++++++++ src/commands/soundbot/RenameCommand.ts | 7 +++++++ src/commands/soundbot/TagCommand.ts | 2 +- src/db/DatabaseAdapter.ts | 8 ++++++++ 5 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/commands/CommandCollection.ts b/src/commands/CommandCollection.ts index 652dea6e..7d3414ce 100644 --- a/src/commands/CommandCollection.ts +++ b/src/commands/CommandCollection.ts @@ -54,8 +54,8 @@ export default class CommandCollection extends Collection { private initializeCommands(queue: SoundQueue, db: DatabaseAdapter) { [ new AddCommand(), - new RenameCommand(), - new RemoveCommand(), + new RenameCommand(db), + new RemoveCommand(db), new RandomCommand(queue), diff --git a/src/commands/soundbot/RemoveCommand.ts b/src/commands/soundbot/RemoveCommand.ts index 01667dfb..9b658fa2 100644 --- a/src/commands/soundbot/RemoveCommand.ts +++ b/src/commands/soundbot/RemoveCommand.ts @@ -4,11 +4,17 @@ import { Message, Permissions } from 'discord.js'; import ICommand from '../base/ICommand'; +import DatabaseAdapter from '../../db/DatabaseAdapter'; import SoundUtil from '../../util/SoundUtil'; export default class RemoveCommand implements ICommand { public readonly TRIGGERS = ['remove']; public readonly USAGE = 'Usage: !remove '; + private readonly db: DatabaseAdapter; + + constructor(db: DatabaseAdapter) { + this.db = db; + } public run(message: Message, params: Array) { if (!message.member.hasPermission(Permissions.FLAGS.ADMINISTRATOR!)) return; @@ -26,6 +32,8 @@ export default class RemoveCommand implements ICommand { const file = SoundUtil.getPathForSound(sound); fs.unlinkSync(file); + this.db.removeSound(sound); + message.channel.send(`${sound} removed!`); } } diff --git a/src/commands/soundbot/RenameCommand.ts b/src/commands/soundbot/RenameCommand.ts index 8b4db08f..badaab7b 100644 --- a/src/commands/soundbot/RenameCommand.ts +++ b/src/commands/soundbot/RenameCommand.ts @@ -4,11 +4,17 @@ import { Message, Permissions } from 'discord.js'; import ICommand from '../base/ICommand'; +import DatabaseAdapter from '../../db/DatabaseAdapter'; import SoundUtil from '../../util/SoundUtil'; export default class RenameCommand implements ICommand { public readonly TRIGGERS = ['rename']; public readonly USAGE = 'Usage: !rename '; + private readonly db: DatabaseAdapter; + + constructor(db: DatabaseAdapter) { + this.db = db; + } public run(message: Message, params: Array) { if (!message.member.hasPermission(Permissions.FLAGS.ADMINISTRATOR!)) return; @@ -35,6 +41,7 @@ export default class RenameCommand implements ICommand { const oldFile = `sounds/${oldName}.${extension}`; const newFile = `sounds/${newName}.${extension}`; fs.renameSync(oldFile, newFile); + this.db.renameSound(oldName, newName); message.channel.send(`${oldName} renamed to ${newName}!`); } diff --git a/src/commands/soundbot/TagCommand.ts b/src/commands/soundbot/TagCommand.ts index a2100fea..7436164a 100644 --- a/src/commands/soundbot/TagCommand.ts +++ b/src/commands/soundbot/TagCommand.ts @@ -27,7 +27,7 @@ export default class TagCommand implements ICommand { } if (!params.length) { - message.channel.send(`Tags for ${sound}: [${this.db.listTags(sound)}]`); + message.author.send(`Tags for ${sound}: [${this.db.listTags(sound)}]`); return; } diff --git a/src/db/DatabaseAdapter.ts b/src/db/DatabaseAdapter.ts index 6cb1139f..0d27feac 100644 --- a/src/db/DatabaseAdapter.ts +++ b/src/db/DatabaseAdapter.ts @@ -34,6 +34,14 @@ export default class DatabaseAdapter { this.updateCount(playedSound); } + public removeSound(sound: string) { + this.db.get('sounds').remove({ name: sound }).write(); + } + + public renameSound(oldName: string, newName: string) { + this.db.get('sounds').find({ name: oldName }).assign({ name: newName }).write(); + } + public addTags(sound: string, tags: Array) { if (!this.soundExists(sound)) this.addNewSound(sound); tags.forEach(tag => this.addTag(sound, tag));