Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: delete all typed commands when deleteMessages is set #144

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/bot/MessageHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ export default class MessageHandler {
this.commands = commands;
}

public handle(message: Message) {
public handle(message: Message, deleteMessages?: boolean) {
if (!this.isValidMessage(message)) return;

const messageToHandle = message;
messageToHandle.content = message.content.substring(config.prefix.length);

this.execute(messageToHandle);
this.execute(messageToHandle, deleteMessages);
}

private isValidMessage(message: Message) {
Expand All @@ -34,7 +34,7 @@ export default class MessageHandler {
);
}

private execute(message: Message) {
private execute(message: Message, deleteMessages?: boolean) {
const [command, ...params] = message.content.split(' ');
const commandToRun = this.commands.get(command);

Expand All @@ -44,5 +44,15 @@ export default class MessageHandler {
}

commandToRun.run(message, params);

if (deleteMessages && !message.deleted && !this.wasMessageAlreadyDeleted(message)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

config is already imported. as an example check L23 which just uses config.prefix.length

i think you can just skip using a parameter, and simply use config.deleteMessages here

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd also love for the command to finish first, and then delete the message, so everyone knows what's up 🤙

message.delete();
}
}

private wasMessageAlreadyDeleted(message: Message) {
if (!message) return false;

return message.channel.messages.cache.find(msg => msg.id === message.id) === null;
}
}
2 changes: 1 addition & 1 deletion src/bot/SoundBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export default class SoundBot extends Client {
}

private onMessage(message: Message) {
this.messageHandler.handle(message);
this.messageHandler.handle(message, this.config.deleteMessages);
}

private onBotJoinsServer(guild: Guild) {
Expand Down
41 changes: 1 addition & 40 deletions src/queue/SoundQueue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Message, StreamDispatcher, VoiceConnection } from 'discord.js';
import { StreamDispatcher, VoiceConnection } from 'discord.js';

import Config from '~/config/Config';
import * as sounds from '~/util/db/Sounds';
Expand Down Expand Up @@ -38,7 +38,6 @@ export default class SoundQueue {

public clear() {
if (!this.currentSound) return;
if (this.config.deleteMessages) this.deleteMessages();

// Prevent further looping
this.currentSound.count = 0;
Expand All @@ -49,23 +48,6 @@ export default class SoundQueue {
return !this.currentSound;
}

private deleteMessages() {
if (!this.currentSound) return;
if (this.isEmpty()) return;

let deleteableMessages = this.queue
.map(item => item.message)
.filter((message): message is Message => !!message);

const { message: currentMessage } = this.currentSound;
if (currentMessage) {
deleteableMessages = deleteableMessages.filter(msg => msg.id !== currentMessage.id);
}

// Do not try to delete the same sound multiple times (!combo)
Array.from(new Set(deleteableMessages)).forEach(message => message.delete());
}

private async playNext() {
this.currentSound = this.queue.shift()!;
const sound = getPathForSound(this.currentSound.name);
Expand Down Expand Up @@ -104,8 +86,6 @@ export default class SoundQueue {

if (count > 1) {
this.add(new QueueItem(name, channel, message, count - 1));
} else {
this.deleteCurrentMessage();
}

this.currentSound = null;
Expand Down Expand Up @@ -136,26 +116,7 @@ export default class SoundQueue {
this.dispatcher = null;
}

private deleteCurrentMessage() {
if (!this.config.deleteMessages) return;
if (!this.currentSound || !this.currentSound.message) return;
if (!this.isLastSoundFromCurrentMessage(this.currentSound.message)) return;
if (this.wasMessageAlreadyDeleted(this.currentSound.message)) return;

this.currentSound.message.delete();
}

private isEmpty() {
return this.queue.length === 0;
}

private wasMessageAlreadyDeleted(message: Message) {
if (!message) return false;

return message.channel.messages.cache.find(msg => msg.id === message.id) === null;
}

private isLastSoundFromCurrentMessage(message: Message) {
return !this.queue.some(item => !!item.message && item.message.id === message.id);
}
}