Skip to content

Commit

Permalink
Merge pull request #1098 from LEDBrain/fix/empty-voicestate-update-log
Browse files Browse the repository at this point in the history
fix(events/VoiceStateUpdate): empty log messages
  • Loading branch information
kdev authored May 21, 2024
2 parents d54d190 + 85231e9 commit 9f6515d
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions src/events/VoiceStateUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ import { EmbedBuilder, Events } from 'discord.js';
import type Client from '../base/Client.js';
import Event from '../base/Event.js';

enum VoiceEvents {
'join',
'leave',
'move',
}

interface CompareVoiceStatesReturn {
embed: EmbedBuilder;
event: VoiceEvents | null;
}

export default class VoiceStateUpdate extends Event {
constructor() {
super({ name: Events.VoiceStateUpdate });
Expand All @@ -15,17 +26,18 @@ export default class VoiceStateUpdate extends Event {
if (!oldState.member || !newState.member) return;
// console.log(oldState.channelId, newState.channelId ?? 'NULL');

const embed = this.compareVoiceStates(oldState, newState);
const comparedVoiceStates = this.compareVoiceStates(oldState, newState);

this.log(newState.guild ? newState.guild : oldState.guild, {
embeds: [embed],
});
if (comparedVoiceStates.event)
this.log(newState.guild ? newState.guild : oldState.guild, {
embeds: [comparedVoiceStates.embed],
});
}

private compareVoiceStates(
oldState: VoiceState,
newState: VoiceState
): EmbedBuilder {
): CompareVoiceStatesReturn {
const basicEmbed = new EmbedBuilder()
.setAuthor({
name: newState.member?.user.tag ?? '',
Expand All @@ -34,30 +46,34 @@ export default class VoiceStateUpdate extends Event {
.setTimestamp()
.setFooter({ text: `Community Service ${this.version}` });

let voiceEvent: CompareVoiceStatesReturn['event'] = null;

if (!oldState?.channelId && newState.channelId) {
basicEmbed
.setDescription(
`**${newState.member?.user.tag}** joined: ${newState.channel}.`
`**${newState.member}** joined: ${newState.channel}.`
)
.addFields([
{
name: 'New Voice Channel',
value: `${newState.channel}`,
},
]);
voiceEvent = VoiceEvents.join;
}

if (oldState.channelId && !newState.channelId) {
basicEmbed
.setDescription(
`**${newState.member?.user.tag}** left: ${oldState.channel}.`
`**${newState.member}** left: ${oldState.channel}.`
)
.addFields([
{
name: 'Old Voice Channel',
value: `${oldState.channel}`,
},
]);
voiceEvent = VoiceEvents.leave;
}

if (
Expand All @@ -67,7 +83,7 @@ export default class VoiceStateUpdate extends Event {
) {
basicEmbed
.setDescription(
`**${newState.member?.user.tag}** moved to: ${newState.channel}.`
`**${newState.member}** moved to: ${newState.channel}.`
)
.addFields([
{
Expand All @@ -79,7 +95,9 @@ export default class VoiceStateUpdate extends Event {
value: `${newState.channel}`,
},
]);
voiceEvent = VoiceEvents.leave;
}
return basicEmbed;

return { embed: basicEmbed, event: voiceEvent };
}
}

0 comments on commit 9f6515d

Please sign in to comment.