Skip to content

Commit

Permalink
Made !rename, !remove update the db
Browse files Browse the repository at this point in the history
  • Loading branch information
markokajzer committed Mar 19, 2018
1 parent 9cb43f3 commit 51e1988
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/commands/CommandCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ export default class CommandCollection extends Collection<string, ICommand> {
private initializeCommands(queue: SoundQueue, db: DatabaseAdapter) {
[
new AddCommand(),
new RenameCommand(),
new RemoveCommand(),
new RenameCommand(db),
new RemoveCommand(db),

new RandomCommand(queue),

Expand Down
8 changes: 8 additions & 0 deletions src/commands/soundbot/RemoveCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <sound>';
private readonly db: DatabaseAdapter;

constructor(db: DatabaseAdapter) {
this.db = db;
}

public run(message: Message, params: Array<string>) {
if (!message.member.hasPermission(Permissions.FLAGS.ADMINISTRATOR!)) return;
Expand All @@ -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!`);
}
}
7 changes: 7 additions & 0 deletions src/commands/soundbot/RenameCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <old> <new>';
private readonly db: DatabaseAdapter;

constructor(db: DatabaseAdapter) {
this.db = db;
}

public run(message: Message, params: Array<string>) {
if (!message.member.hasPermission(Permissions.FLAGS.ADMINISTRATOR!)) return;
Expand All @@ -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}!`);
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/soundbot/TagCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
8 changes: 8 additions & 0 deletions src/db/DatabaseAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>) {
if (!this.soundExists(sound)) this.addNewSound(sound);
tags.forEach(tag => this.addTag(sound, tag));
Expand Down

0 comments on commit 51e1988

Please sign in to comment.