Skip to content

Commit

Permalink
feat: move audios to single command
Browse files Browse the repository at this point in the history
  • Loading branch information
Guilhermeasper committed Sep 16, 2023
1 parent 7a93d24 commit 9b74fdd
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 69 deletions.
22 changes: 0 additions & 22 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ import {
SlashCommandBuilder,
} from 'discord.js';
import { BotEvent, Command, SecretChannelData, SlashCommand } from './types';
import { readdirSync } from 'fs';
import { join } from 'path';
import { logger } from './utils/logger';
import { safeExecute } from './utils/errorHandling';
import { audioCommandBuilder } from './commands/audioCommands/audioCommandBuilder';
import * as commands from './commands';
import * as events from './events';
import { mongoConnection } from './utils/mongo';
Expand All @@ -27,7 +24,6 @@ const {

class Bot {
private _client: Client;
private readonly _audiosDir = join(__dirname, './resources/sounds/');
private readonly _slashCommands: SlashCommandBuilder[] = [];

constructor() {
Expand All @@ -48,7 +44,6 @@ class Bot {

start() {
this._loadTextCommands();
this._loadAudioCommands();
this._loadSlashCommands();
this._sendSlashCommands();
this._loadEvents();
Expand Down Expand Up @@ -101,23 +96,6 @@ class Bot {
});
}

private _loadAudioCommands() {
readdirSync(this._audiosDir).forEach((file) => {
try {
if (!file.endsWith('.mp3') || file.startsWith('_')) return;
const { slashCommand, textCommand } = audioCommandBuilder(file);

this._slashCommands.push(slashCommand.command);
this._client.commands.set(textCommand.name, textCommand);
this._client.slashCommands.set(slashCommand.command.name, slashCommand);
logger.info(`Successfully loaded audio command ${textCommand.name}`);
} catch (error) {
logger.error(`Error loading audio command ${file.replace('.js', '')}`);
logger.error(error);
}
});
}

private _loadEvents() {
const eventsArray = Array.from(Object.values(events)).map(
(event: BotEvent) => event
Expand Down
47 changes: 0 additions & 47 deletions src/commands/audioCommands/audioCommandBuilder.ts

This file was deleted.

51 changes: 51 additions & 0 deletions src/commands/slashCommands/audio.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { SlashCommandBuilder, EmbedBuilder, Client } from 'discord.js';
import { SlashCommand } from '../../types';
import { readdirSync } from 'fs';
import { logger } from 'src/utils/logger';
import { join } from 'path';
import { playAudio, voiceChannelPresence } from 'src/utils/discord';

const audiosDir = join(__dirname, '../../resources/sounds/');
const audios: string[] = [];

readdirSync(audiosDir).forEach((file) => {
try {
if (!file.endsWith('.mp3') || file.startsWith('_')) return;
audios.push(file.replace('.mp3', ''));
logger.info(`Successfully loaded audio ${file.replace('.mp3', '')}`);
} catch (error) {
logger.error(`Error loading audio ${file.replace('.mp3', '')}`);
logger.error(error);
}
});

export const audio: SlashCommand = {
command: new SlashCommandBuilder()
.setName('audio')
.setDescription('Reproduz um áudio')
.addStringOption((option) =>
option
.setName('audio')
.setDescription('Áudio a ser reproduzido')
.setRequired(true)
.addChoices(
...audios.map((audio) => {
return { name: audio, value: audio };
})
)
),

execute: (interaction) => {
const channel = voiceChannelPresence(interaction);
const file = interaction.options.get('audio');
playAudio(interaction, channel, file.value as string);
interaction.reply({
embeds: [
new EmbedBuilder().setDescription(
`Reproduzindo ${file.value as string}`
),
],
});
},
cooldown: 10,
};
1 change: 1 addition & 0 deletions src/commands/slashCommands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ export { silence } from './silence';
export { unsilence } from './unsilence';
export { toggleAdminRoulette } from './toggleAdminRoulette';
export { addToRoulette } from './addToRoulette';
export { audio } from './audio';

0 comments on commit 9b74fdd

Please sign in to comment.