Skip to content

Commit

Permalink
Added !tag
Browse files Browse the repository at this point in the history
Include tags in !search
  • Loading branch information
markokajzer committed Mar 19, 2018
1 parent c8a3092 commit 9cb43f3
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 22 deletions.
10 changes: 6 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
## 1.1.0 (unreleased)
## 1.0.0 (2018-03-19)

+ Added `!avatar` command to set avatar via message attachment
+ If no attachment was sent, the avatar will send a link to the current avatar
+ Use `!avatar remove` to remove the avatar

## 1.0.0 (2018-03-19)

+ Added `!tag` command to add tags to sounds
+ Use `!tag <sound>` to list the tags of a sound
+ Use `!tag <sound> <tag1> ... <tagN>` to add tags to a sound
+ Use `!tag <sound> clear` to remove all tags of a sound
+ Added `!search` command
+ Tags are included in the `!search` command
+ Sanitize sound on `!add`, `!remove`
+ Restricted access to `!rename`, `!remove`, `!ignore`, `!unignore`

Expand Down
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Type `!commands` to print the following list of available commands.
```
!commands Show this message
!sounds Show available sounds
!tag <sound> <tag> Add tag to sound
!search <tag> Search for specific sound
!add Add the attached sound
!<sound> Play the specified sound
Expand All @@ -80,9 +81,13 @@ Type `!sounds` to get a list of all sounds that are available to your bot. Play

All sounds will be added to a queue and played in succession. To halt the playback and empty the queue type `!stop`.

### Searching sounds
### Tagging and searching sounds

When your library of sounds gets too big and you forget what kinds of sounds you added, you can search for specific sounds with `!search <tag>`.
When your library of sounds gets too big and you forget what kinds of sounds you have, you can add tags to sounds.

You can add tags to sounds with `!tag <sound> <tag>`. You can specify one or more sounds. You can get the tags of a sound with `!tag <sound>`. You can also remove all tags from a sound with `!tag <sound> clear`.

To search for specific sounds use `!search <tag>`. It will look for the name of the sound as well as tags that you might have added to the sound.

### Renaming sounds

Expand All @@ -102,7 +107,7 @@ Use `!avatar` and attach an image to set the bot's avatar. You can remove the av

### Restricted commands

The commands `!rename`, `!remove`, `!ignore`, `!unignore` and `!avatar` are restricted and can only be accessed by Administrators.
The commands `!rename`, `!remove`, `!ignore`, `!unignore`, `!avatar` and `!tag <sound> clear` are restricted and can only be accessed by administrators.


## Configuration
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dicord-soundbot",
"version": "1.1.0",
"version": "1.0.0",
"description": "A Discord Bot to play sounds",
"repository": {
"type": "git",
Expand Down
4 changes: 3 additions & 1 deletion src/commands/CommandCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import SoundCommand from './soundbot/SoundCommand';

import SoundsCommand from './soundbot/SoundsCommand';
import SearchCommand from './soundbot/SearchCommand';
import TagCommand from './soundbot/TagCommand';

import StopCommand from './soundbot/StopCommand';

Expand Down Expand Up @@ -59,7 +60,8 @@ export default class CommandCollection extends Collection<string, ICommand> {
new RandomCommand(queue),

new SoundsCommand(),
new SearchCommand(),
new SearchCommand(db),
new TagCommand(db),

new StopCommand(queue),

Expand Down
1 change: 1 addition & 0 deletions src/commands/soundbot/HelpCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default class HelpCommand implements ICommand {
'',
'commands Show this message',
'sounds Show available sounds',
'tag <sound> <tag> Add tag to sound',
'search <tag> Search for specific sound',
'add Add the attached sound',
'<sound> Play the specified sound',
Expand Down
18 changes: 11 additions & 7 deletions src/commands/soundbot/SearchCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ import { Message } from 'discord.js';

import ICommand from '../base/ICommand';

import DatabaseAdapter from '../../db/DatabaseAdapter';
import SoundUtil from '../../util/SoundUtil';


export default class SearchCommand implements ICommand {
public readonly TRIGGERS = ['search'];
public readonly USAGE = 'Usage: !search <tag>';
private readonly MINIMUM_TAG_LENGTH = 3;
private readonly db: DatabaseAdapter;

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

public run(message: Message, params: Array<string>) {
if (params.length !== 1) {
Expand All @@ -16,17 +22,15 @@ export default class SearchCommand implements ICommand {
}

const tag = params.shift()!;
if (tag.length < this.MINIMUM_TAG_LENGTH) {
message.channel.send('Search tag too short!');
return;
}

const results = SoundUtil.getSounds().filter(sound => sound.includes(tag));
this.db.soundsWithTag(tag).forEach(sound => results.push(sound));

if (!results.length) {
message.author.send('No sounds found.');
return;
}

message.author.send(results.join('\n'));
const uniqueResults = [...new Set(results)].sort();
message.author.send(uniqueResults.join('\n'));
}
}
42 changes: 42 additions & 0 deletions src/commands/soundbot/TagCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Message, Permissions } from 'discord.js';

import ICommand from '../base/ICommand';

import DatabaseAdapter from '../../db/DatabaseAdapter';
import SoundUtil from '../../util/SoundUtil';

export default class TagCommand implements ICommand {
public readonly TRIGGERS = ['tag', 'tags'];
public readonly USAGE = 'Usage: !tag <sound> [<tag> ... <tagN> | clear]';
private readonly db: DatabaseAdapter;

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

public run(message: Message, params: Array<string>) {
if (params.length < 1) {
message.channel.send(this.USAGE);
return;
}

const sound = params.shift()!;
if (!SoundUtil.getSounds().includes(sound)) {
message.channel.send(`${sound} not found!`);
return;
}

if (!params.length) {
message.channel.send(`Tags for ${sound}: [${this.db.listTags(sound)}]`);
return;
}

if (params[0] === 'clear') {
if (!message.member.hasPermission(Permissions.FLAGS.ADMINISTRATOR!)) return;
this.db.removeTags(sound);
return;
}

this.db.addTags(sound, params);
}
}
38 changes: 32 additions & 6 deletions src/db/DatabaseAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,54 @@ export default class DatabaseAdapter {
}

public getMostPlayedSounds(limit = 15): Array<{ name: string, count: number }> {
return this.db.get('counts').sortBy('count').reverse().take(limit).value();
return this.db.get('sounds').sortBy('count').reverse().take(limit).value();
}

public updateSoundCount(playedSound: string) {
if (!this.soundExists(playedSound)) this.addNewSound(playedSound);
this.updateCount(playedSound);
}

public addTags(sound: string, tags: Array<string>) {
if (!this.soundExists(sound)) this.addNewSound(sound);
tags.forEach(tag => this.addTag(sound, tag));
}

public listTags(sound: string) {
return this.db.get('sounds').find({ name: sound }).value().tags;
}

public removeTags(sound: string) {
this.db.get('sounds').find({ name: sound }).assign({ tags: [] }).write();
}

public soundsWithTag(tag: string): Array<string> {
return this.db.get('sounds').value().filter((sound: any) => sound.tags.includes(tag))
.map((sound: any) => sound.name);
}

private ensureDefaults() {
this.db.defaults({ counts: [], ignoreList: [] }).write();
this.db.defaults({ sounds: [], ignoreList: [] }).write();
}

private soundExists(sound: string) {
return this.db.get('counts').find({ name: sound }).value() !== undefined;
return this.db.get('sounds').find({ name: sound }).value() !== undefined;
}

private addNewSound(sound: string) {
this.db.get('counts').push({ name: sound, count: 0 }).write();
this.db.get('sounds').push({ name: sound, count: 0, tags: [] }).write();
}

private updateCount(sound: string) {
const newValue = this.db.get('counts').find({ name: sound }).value().count + 1;
this.db.get('counts').find({ name: sound }).assign({ count: newValue }).write();
const newValue = this.db.get('sounds').find({ name: sound }).value().count + 1;
this.db.get('sounds').find({ name: sound }).assign({ count: newValue }).write();
}

private addTag(sound: string, tag: string) {
const tags = this.db.get('sounds').find({ name: sound }).value().tags;
if (tags.includes(tag)) return;
tags.push(tag);

this.db.get('sounds').find({ name: sound }).assign({ tags: tags }).write();
}
}

0 comments on commit 9cb43f3

Please sign in to comment.