Skip to content

Commit

Permalink
feat: custom URL param
Browse files Browse the repository at this point in the history
  • Loading branch information
rdsov committed May 29, 2024
1 parent 2dda1ae commit dfc0286
Showing 1 changed file with 6 additions and 25 deletions.
31 changes: 6 additions & 25 deletions commands/kickoff.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,19 @@ const {
AudioPlayerStatus,
VoiceConnectionStatus
} = require('@discordjs/voice');
const play = require('play-dl');
const sodium = require('libsodium-wrappers');
const { PermissionFlagsBits } = require("discord.js");

module.exports = {
data: new SlashCommandBuilder()
.setName('kickoff')
.setDescription('Start a special event.')
.setDescription('Join a voice channel and play an audio file from a URL')
.setDefaultMemberPermissions(PermissionFlagsBits.ManageMessages)
.addStringOption(option =>
option.setName('link')
.setDescription('Pre-intro audio source.')
option.setName('url')
.setDescription('The URL of the audio file to play')
.setRequired(true)),
async execute(interaction, config) {
const link = interaction.options.getString('link');
const audioUrl = interaction.options.getString('url');
const member = await interaction.guild.members.fetch(interaction.user.id);
const voiceChannel = member.voice.channel;

Expand All @@ -35,17 +33,8 @@ module.exports = {
return interaction.reply({ content: 'You need to join a voice channel first!', ephemeral: true });
}

// Validate the YouTube link
const youtubeRegex = /^(https?:\/\/)?(www\.)?(youtube\.com|youtu\.?be)\/.+$/;
if (!youtubeRegex.test(link)) {
return interaction.reply({ content: 'Please provide a valid YouTube link.', ephemeral: true });
}

await interaction.deferReply();

// Initialize sodium
await sodium.ready;

const connection = joinVoiceChannel({
channelId: voiceChannel.id,
guildId: voiceChannel.guild.id,
Expand All @@ -58,23 +47,15 @@ module.exports = {
console.log('The bot has connected to the channel!');

try {
const stream = await play.stream(link, { quality: 2 });
const resource = createAudioResource(stream.stream, {
inputType: stream.type,
});

const resource = createAudioResource(audioUrl, { inputType: 2 });
player.play(resource);
connection.subscribe(player);

player.on(AudioPlayerStatus.Idle, () => {
connection.destroy();
});

if (logChannel) {
logChannel.send(`[SYSTEM] **${ interaction.user.tag }** started a special event.`);
}

await interaction.editReply({ content: `Event started.` });
await interaction.editReply({ content: `Now playing: ${ audioUrl }` });
} catch (error) {
console.error('Error while trying to play the audio:', error);
await interaction.editReply({ content: 'An error occurred while trying to play the audio.' });
Expand Down

0 comments on commit dfc0286

Please sign in to comment.