Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Commit

Permalink
fix: Extract logic for getting author value for embeds
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusbegby committed Sep 5, 2023
1 parent 8c05499 commit 83e23d8
Show file tree
Hide file tree
Showing 60 changed files with 126 additions and 317 deletions.
13 changes: 12 additions & 1 deletion src/classes/interactions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import config from 'config';
import { GuildQueue } from 'discord-player';
import {
ApplicationCommandOptionChoiceData,
AutocompleteInteraction,
Expand Down Expand Up @@ -64,7 +65,7 @@ abstract class BaseInteractionWithEmbedResponse extends BaseInteraction {
this.botOptions = config.get('botOptions');
}

protected async getEmbedAuthor(
protected async getEmbedUserAuthor(
interaction: MessageComponentInteraction | ChatInputCommandInteraction
): Promise<EmbedAuthorOptions> {
let authorName: string = '';
Expand All @@ -79,6 +80,16 @@ abstract class BaseInteractionWithEmbedResponse extends BaseInteraction {
iconURL: interaction.user.avatarURL() || this.embedOptions.info.fallbackIconUrl
};
}

protected async getEmbedQueueAuthor(
interaction: MessageComponentInteraction | ChatInputCommandInteraction,
queue: GuildQueue
): Promise<EmbedAuthorOptions> {
return {
name: `Channel: ${queue.channel!.name} (${queue.channel!.bitrate / 1000}kbps)`,
iconURL: interaction.guild!.iconURL() || this.embedOptions.info.fallbackIconUrl
};
}
}

export abstract class BaseSlashCommandInteraction extends BaseInteractionWithEmbedResponse {
Expand Down
3 changes: 1 addition & 2 deletions src/events/client/debug.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Events } from 'discord.js';
import { randomUUID as uuidv4 } from 'node:crypto';

import loggerModule from '../../services/logger';
import { Logger } from 'pino';
import loggerModule from '../../services/logger';

module.exports = {
name: Events.Debug,
Expand Down
3 changes: 1 addition & 2 deletions src/events/client/disconnect.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import config from 'config';
import { BaseGuildTextChannel, EmbedBuilder } from 'discord.js';
import { randomUUID as uuidv4 } from 'node:crypto';

import { Logger } from 'pino';
import loggerModule from '../../services/logger';
import { ExtendedClient } from '../../types/clientTypes';
import { EmbedOptions, SystemOptions } from '../../types/configTypes';
import { Logger } from 'pino';

const embedOptions: EmbedOptions = config.get('embedOptions');
const systemOptions: SystemOptions = config.get('systemOptions');
Expand Down
3 changes: 1 addition & 2 deletions src/events/client/error.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Events } from 'discord.js';
import { randomUUID as uuidv4 } from 'node:crypto';

import loggerModule from '../../services/logger';
import { Logger } from 'pino';
import loggerModule from '../../services/logger';

module.exports = {
name: Events.Error,
Expand Down
3 changes: 1 addition & 2 deletions src/events/client/guildCreate.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Events, Guild } from 'discord.js';
import { randomUUID as uuidv4 } from 'node:crypto';

import loggerModule from '../../services/logger';
import { Logger } from 'pino';
import loggerModule from '../../services/logger';

module.exports = {
name: Events.GuildCreate,
Expand Down
3 changes: 1 addition & 2 deletions src/events/client/guildDelete.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Events, Guild } from 'discord.js';
import { randomUUID as uuidv4 } from 'node:crypto';

import loggerModule from '../../services/logger';
import { Logger } from 'pino';
import loggerModule from '../../services/logger';

module.exports = {
name: Events.GuildDelete,
Expand Down
1 change: 0 additions & 1 deletion src/events/client/ready.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import config from 'config';
import { BaseGuildTextChannel, EmbedBuilder, Events, PresenceData } from 'discord.js';
import { randomUUID as uuidv4 } from 'node:crypto';

import { Logger } from 'pino';
import loggerModule from '../../services/logger';
import { ExtendedClient } from '../../types/clientTypes';
Expand Down
1 change: 0 additions & 1 deletion src/events/client/reconnecting.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import config from 'config';
import { BaseGuildTextChannel, EmbedBuilder } from 'discord.js';
import { randomUUID as uuidv4 } from 'node:crypto';

import { Logger } from 'pino';
import loggerModule from '../../services/logger';
import { ExtendedClient } from '../../types/clientTypes';
Expand Down
2 changes: 1 addition & 1 deletion src/events/client/warn.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Events } from 'discord.js';
import { Logger } from 'pino';
import { randomUUID as uuidv4 } from 'node:crypto';
import { Logger } from 'pino';
import loggerModule from '../../services/logger';

module.exports = {
Expand Down
4 changes: 2 additions & 2 deletions src/events/interactions/interactionCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import {
InteractionType,
MessageComponentInteraction
} from 'discord.js';
import { Logger } from 'pino';
import { randomUUID as uuidv4 } from 'node:crypto';
import { Logger } from 'pino';
import { CustomError } from '../../classes/interactions';
import { handleAutocomplete } from '../../handlers/interactionAutocompleteHandler';
import { handleCommand } from '../../handlers/interactionCommandHandler';
import { handleComponent } from '../../handlers/interactionComponentHandler';
import { handleError } from '../../handlers/interactionErrorHandler';
import loggerModule from '../../services/logger';
import { ExtendedClient } from '../../types/clientTypes';
import { CustomError } from '../../classes/interactions';

module.exports = {
name: Events.InteractionCreate,
Expand Down
2 changes: 1 addition & 1 deletion src/events/player/generalDebug.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Logger } from 'pino';
import { randomUUID as uuidv4 } from 'node:crypto';
import { Logger } from 'pino';
import loggerModule from '../../services/logger';

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion src/events/player/generalError.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import config from 'config';
import { BaseGuildTextChannel, EmbedBuilder } from 'discord.js';
import { Logger } from 'pino';
import { randomUUID as uuidv4 } from 'node:crypto';
import { Logger } from 'pino';
import loggerModule from '../../services/logger';
import { BotOptions, EmbedOptions, SystemOptions } from '../../types/configTypes';
import { ExtendedGuildQueuePlayerNode } from '../../types/eventTypes';
Expand Down
2 changes: 1 addition & 1 deletion src/events/player/playerDebug.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Logger } from 'pino';
import { randomUUID as uuidv4 } from 'node:crypto';
import { Logger } from 'pino';
import loggerModule from '../../services/logger';

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion src/events/player/playerError.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import config from 'config';
import { BaseGuildTextChannel, EmbedBuilder } from 'discord.js';
import { Logger } from 'pino';
import { randomUUID as uuidv4 } from 'node:crypto';
import { Logger } from 'pino';
import loggerModule from '../../services/logger';
import { BotOptions, EmbedOptions, SystemOptions } from '../../types/configTypes';
import { ExtendedGuildQueuePlayerNode } from '../../types/eventTypes';
Expand Down
2 changes: 1 addition & 1 deletion src/events/player/playerSkip.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import config from 'config';
import { Track } from 'discord-player';
import { BaseGuildTextChannel, EmbedBuilder } from 'discord.js';
import { Logger } from 'pino';
import { randomUUID as uuidv4 } from 'node:crypto';
import { Logger } from 'pino';
import loggerModule from '../../services/logger';
import { BotOptions, EmbedOptions, SystemOptions } from '../../types/configTypes';
import { ExtendedGuildQueuePlayerNode } from '../../types/eventTypes';
Expand Down
2 changes: 1 addition & 1 deletion src/events/player/playerStart.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Track } from 'discord-player';
import { Logger } from 'pino';
import { randomUUID as uuidv4 } from 'node:crypto';
import { Logger } from 'pino';
import loggerModule from '../../services/logger';
import { ExtendedGuildQueuePlayerNode } from '../../types/eventTypes';

Expand Down
2 changes: 1 addition & 1 deletion src/events/process/uncaughtException.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Logger } from 'pino';
import { randomUUID as uuidv4 } from 'node:crypto';
import { Logger } from 'pino';
import loggerModule from '../../services/logger';

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion src/events/process/unhandledRejection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Logger } from 'pino';
import { randomUUID as uuidv4 } from 'node:crypto';
import { Logger } from 'pino';
import loggerModule from '../../services/logger';

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/interactionAutocompleteHandler.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { AutocompleteInteraction } from 'discord.js';
import { Logger } from 'pino';
import { BaseAutocompleteInteraction } from '../classes/interactions';
import loggerModule from '../services/logger';
import { ExtendedClient } from '../types/clientTypes';
import { BaseAutocompleteInteraction } from '../classes/interactions';

export const handleAutocomplete = async (
interaction: AutocompleteInteraction,
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/interactionCommandHandler.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ChatInputCommandInteraction } from 'discord.js';
import { Logger } from 'pino';
import { BaseSlashCommandInteraction } from '../classes/interactions';
import loggerModule from '../services/logger';
import { ExtendedClient } from '../types/clientTypes';
import { BaseSlashCommandInteraction } from '../classes/interactions';
import { checkChannelPermissionViewable } from '../utils/validation/permissionValidator';

export const handleCommand = async (
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/interactionComponentHandler.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { MessageComponentInteraction } from 'discord.js';
import { Logger } from 'pino';
import { BaseComponentInteraction } from '../classes/interactions';
import loggerModule from '../services/logger';
import { ExtendedClient } from '../types/clientTypes';
import { BaseComponentInteraction } from '../classes/interactions';
import { checkChannelPermissionViewable } from '../utils/validation/permissionValidator';

export const handleComponent = async (
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/interactionErrorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import {
MessageComponentInteraction
} from 'discord.js';
import { Logger } from 'pino';
import { CustomError, InteractionValidationError } from '../classes/interactions';
import loggerModule from '../services/logger';
import { BotOptions, EmbedOptions } from '../types/configTypes';
import { CustomError, InteractionValidationError } from '../classes/interactions';

const embedOptions: EmbedOptions = config.get('embedOptions');
const botOptions: BotOptions = config.get('botOptions');
Expand Down
4 changes: 1 addition & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import 'dotenv/config';

import config from 'config';
import { Client, Shard, ShardEvents, ShardingManager, ShardingManagerOptions } from 'discord.js';
import 'dotenv/config';
import { randomUUID as uuidv4 } from 'node:crypto';

import { Logger } from 'pino';
import loggerModule from './services/logger';

Expand Down
2 changes: 1 addition & 1 deletion src/interactions/autocomplete/lyrics.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { LyricsData, lyricsExtractor } from '@discord-player/extractor';
import { Player, SearchResult, useMainPlayer } from 'discord-player';
import { ApplicationCommandOptionChoiceData } from 'discord.js';
import { BaseAutocompleteParams, BaseAutocompleteReturnType } from '../../types/interactionTypes';
import { BaseAutocompleteInteraction } from '../../classes/interactions';
import { BaseAutocompleteParams, BaseAutocompleteReturnType } from '../../types/interactionTypes';

// TODO: create type for recent query object
const recentQueries = new Map();
Expand Down
2 changes: 1 addition & 1 deletion src/interactions/autocomplete/play.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Player, SearchResult, useMainPlayer } from 'discord-player';
import { ApplicationCommandOptionChoiceData } from 'discord.js';
import { BaseAutocompleteParams, BaseAutocompleteReturnType } from '../../types/interactionTypes';
import { BaseAutocompleteInteraction } from '../../classes/interactions';
import { BaseAutocompleteParams, BaseAutocompleteReturnType } from '../../types/interactionTypes';

// TODO: create type for recent query object
const recentQueries = new Map();
Expand Down
2 changes: 1 addition & 1 deletion src/interactions/commands/info/help.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EmbedBuilder, SlashCommandBuilder, SlashCommandNumberOption, SlashCommandStringOption } from 'discord.js';
import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes';
import { BaseSlashCommandInteraction } from '../../../classes/interactions';
import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes';

class HelpCommand extends BaseSlashCommandInteraction {
constructor() {
Expand Down
2 changes: 1 addition & 1 deletion src/interactions/commands/info/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { EmbedBuilder, Guild, SlashCommandBuilder } from 'discord.js';
import osu from 'node-os-utils';
// @ts-ignore
import { version } from '../../../../package.json';
import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes';
import { BaseSlashCommandInteraction } from '../../../classes/interactions';
import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes';
import { getUptimeFormatted } from '../../../utils/system/getUptimeFormatted';

class StatusCommand extends BaseSlashCommandInteraction {
Expand Down
4 changes: 2 additions & 2 deletions src/interactions/commands/player/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import { Logger } from 'pino';
import { BaseSlashCommandInteraction } from '../../../classes/interactions';
import { FFmpegFilterOption, FFmpegFilterOptions } from '../../../types/configTypes';
import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes';
import { checkQueueExists, checkQueueCurrentTrack } from '../../../utils/validation/queueValidator';
import { checkSameVoiceChannel, checkInVoiceChannel } from '../../../utils/validation/voiceChannelValidator';
import { checkQueueCurrentTrack, checkQueueExists } from '../../../utils/validation/queueValidator';
import { checkInVoiceChannel, checkSameVoiceChannel } from '../../../utils/validation/voiceChannelValidator';

const ffmpegFilterOptions: FFmpegFilterOptions = config.get('ffmpegFilterOptions');

Expand Down
4 changes: 2 additions & 2 deletions src/interactions/commands/player/leave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { EmbedBuilder, SlashCommandBuilder } from 'discord.js';
import { BaseSlashCommandInteraction } from '../../../classes/interactions';
import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes';
import { checkQueueExists } from '../../../utils/validation/queueValidator';
import { checkSameVoiceChannel, checkInVoiceChannel } from '../../../utils/validation/voiceChannelValidator';
import { checkInVoiceChannel, checkSameVoiceChannel } from '../../../utils/validation/voiceChannelValidator';

class LeaveCommand extends BaseSlashCommandInteraction {
constructor() {
Expand Down Expand Up @@ -36,7 +36,7 @@ class LeaveCommand extends BaseSlashCommandInteraction {
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setAuthor(await this.getEmbedAuthor(interaction))
.setAuthor(await this.getEmbedUserAuthor(interaction))
.setDescription(
`**${this.embedOptions.icons.success} Leaving channel**\nCleared the track queue and left voice channel.\n\nTo play more music, use the **\`/play\`** command!`
)
Expand Down
29 changes: 6 additions & 23 deletions src/interactions/commands/player/loop.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { GuildQueue, QueueRepeatMode, useQueue } from 'discord-player';
import { EmbedBuilder, GuildMember, SlashCommandBuilder, SlashCommandStringOption } from 'discord.js';
import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes';
import { EmbedBuilder, SlashCommandBuilder, SlashCommandStringOption } from 'discord.js';
import { BaseSlashCommandInteraction } from '../../../classes/interactions';
import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes';
import { checkQueueExists } from '../../../utils/validation/queueValidator';
import { checkSameVoiceChannel, checkInVoiceChannel } from '../../../utils/validation/voiceChannelValidator';
import { checkInVoiceChannel, checkSameVoiceChannel } from '../../../utils/validation/voiceChannelValidator';

class LoopCommand extends BaseSlashCommandInteraction {
constructor() {
Expand Down Expand Up @@ -106,14 +106,6 @@ class LoopCommand extends BaseSlashCommandInteraction {
});
}

let authorName: string;

if (interaction.member instanceof GuildMember) {
authorName = interaction.member.nickname || interaction.user.username;
} else {
authorName = interaction.user.username;
}

if (queue.repeatMode === 0) {
logger.debug('Disabled loop mode.');

Expand All @@ -122,10 +114,7 @@ class LoopCommand extends BaseSlashCommandInteraction {
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setAuthor({
name: authorName,
iconURL: interaction.user.avatarURL() || this.embedOptions.info.fallbackIconUrl
})
.setAuthor(await this.getEmbedUserAuthor(interaction))
.setDescription(
`**${this.embedOptions.icons.success} Loop mode disabled**\nChanging loop mode from **\`${currentModeUserString}\`** to **\`${modeUserString}\`**.\n\nThe ${currentModeUserString} will no longer play on repeat!`
)
Expand All @@ -141,10 +130,7 @@ class LoopCommand extends BaseSlashCommandInteraction {
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setAuthor({
name: authorName,
iconURL: interaction.user.avatarURL() || this.embedOptions.info.fallbackIconUrl
})
.setAuthor(await this.getEmbedUserAuthor(interaction))
.setDescription(
`**${this.embedOptions.icons.autoplaying} Loop mode changed**\nChanging loop mode from **\`${currentModeUserString}\`** to **\`${modeUserString}\`**.\n\nWhen the queue is empty, similar tracks will start playing!`
)
Expand All @@ -159,10 +145,7 @@ class LoopCommand extends BaseSlashCommandInteraction {
return await interaction.editReply({
embeds: [
new EmbedBuilder()
.setAuthor({
name: authorName,
iconURL: interaction.user.avatarURL() || this.embedOptions.info.fallbackIconUrl
})
.setAuthor(await this.getEmbedUserAuthor(interaction))
.setDescription(
`**${this.embedOptions.icons.looping} Loop mode changed**\nChanging loop mode from **\`${currentModeUserString}\`** to **\`${modeUserString}\`**.\n\nThe ${modeUserString} will now play on repeat!`
)
Expand Down
6 changes: 3 additions & 3 deletions src/interactions/commands/player/lyrics.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { LyricsData, lyricsExtractor } from '@discord-player/extractor';
import { GuildQueue, Player, QueryType, useMainPlayer, useQueue } from 'discord-player';
import { EmbedBuilder, SlashCommandBuilder } from 'discord.js';
import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes';
import { BaseSlashCommandInteraction } from '../../../classes/interactions';
import { checkQueueExists, checkQueueCurrentTrack } from '../../../utils/validation/queueValidator';
import { checkSameVoiceChannel, checkInVoiceChannel } from '../../../utils/validation/voiceChannelValidator';
import { BaseSlashCommandParams, BaseSlashCommandReturnType } from '../../../types/interactionTypes';
import { checkQueueCurrentTrack, checkQueueExists } from '../../../utils/validation/queueValidator';
import { checkInVoiceChannel, checkSameVoiceChannel } from '../../../utils/validation/voiceChannelValidator';

class LyricsCommand extends BaseSlashCommandInteraction {
constructor() {
Expand Down
Loading

0 comments on commit 83e23d8

Please sign in to comment.