Skip to content

Commit

Permalink
Fixed !sounds when having a lot of sounds
Browse files Browse the repository at this point in the history
  • Loading branch information
markokajzer committed Aug 17, 2018
1 parent 863fd08 commit 073ad9e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 11 deletions.
11 changes: 9 additions & 2 deletions src/commands/soundbot/SoundsCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@ import { Message } from 'discord.js';

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

import MessageChunker from '../../message/MessageChunker';
import SoundUtil from '../../util/SoundUtil';

export default class SoundsCommand implements ICommand {
public readonly TRIGGERS = ['sounds'];
private readonly chunker: MessageChunker;

public run(message: Message) {
constructor(chunker = new MessageChunker()) {
this.chunker = chunker;
}

public run(message: Message, params: Array<string>) {
const sounds = SoundUtil.getSounds();

if (!sounds.length) {
message.author.send("You don't have any sounds yet! Try adding one with the !add command.");
return;
}

message.author.send(['```', ...sounds, '```'].join('\n'));
this.chunker.chunkedMessages(sounds, params)
.forEach(chunk => message.author.send(chunk));
}
}
20 changes: 11 additions & 9 deletions src/commands/soundbot/TagsCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,30 @@ import { Message } from 'discord.js';
import ICommand from '../base/ICommand';

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

export default class TagsCommand implements ICommand {
public readonly TRIGGERS = ['tags'];
private readonly db: DatabaseAdapter;
private readonly chunker: MessageChunker;

constructor(db: DatabaseAdapter) {
constructor(db: DatabaseAdapter, chunker = new MessageChunker()) {
this.db = db;
this.chunker = chunker;
}

public run(message: Message) {
public run(message: Message, params: Array<string>) {
const sounds = SoundUtil.getSounds();
const reply = this.formattedMessage(sounds, this.findLongestWord(sounds).length);
const soundsWithTags = this.formattedMessage(sounds);

message.author.send(reply);
this.chunker.chunkedMessages(soundsWithTags, params)
.forEach(chunk => message.author.send(chunk));
}

private formattedMessage(sounds: Array<string>, longestSoundLength: number) {
const message = ['```'];
sounds.forEach(sound => message.push(this.listSoundWithTags(sound, longestSoundLength)));
message.push('```');
return message.join('\n');
private formattedMessage(sounds: Array<string>) {
const longestSoundLength = this.findLongestWord(sounds).length;
return sounds.map(sound => this.listSoundWithTags(sound, longestSoundLength));
}

private listSoundWithTags(sound: string, longestSoundLength: number): string {
Expand Down
40 changes: 40 additions & 0 deletions src/message/MessageChunker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
export default class MessageChunker {
private readonly MAX_MESSAGE_LENGTH = 2000;
private readonly CODE_MARKER_LENGTH = '```'.length * 2;

public chunkedMessages(toChunk: Array<string>, params: Array<string>): Array<string> {
const chunks = this.chunkArray(toChunk);

const indexInput = parseInt(params[0]);
const realIndex = indexInput - 1;

if (indexInput && realIndex >= 0 && realIndex < chunks.length) {
return [
`\`\`\`Page ${indexInput} of ${chunks.length}\`\`\``,
['```', ...chunks[realIndex], '```'].join('\n')
];
}

return chunks.map(chunk => ['```', ...chunk, '```'].join('\n'));
}

private chunkArray(input: Array<string>): Array<Array<string>> {
const chunkedInput: Array<Array<string>> = [];

let total = 0;
let temp: Array<string> = [];
for (const element of input) {
if (total + element.length > this.MAX_MESSAGE_LENGTH - this.CODE_MARKER_LENGTH) {
chunkedInput.push(temp);
temp = [element];
continue;
}

total += element.length;
temp.push(element);
}

chunkedInput.push(temp);
return chunkedInput;
}
}

0 comments on commit 073ad9e

Please sign in to comment.