const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); const { Sentry } = require('../../../utils/debugs'); module.exports = { data: new SlashCommandBuilder() .setName('autoplay') .setDescription('Enable or disable autoplay for the current session.') .addBooleanOption(option => option.setName('status') .setDescription('Turn autoplay on or off') .setRequired(true) ), async execute(interaction) { const discordClient = interaction.client; const guildId = interaction.guild?.id; if (!guildId) { return await interaction.reply({ embeds: [new EmbedBuilder() .setColor('#ff5555') .setDescription(`<:check_unsuccess:1351159295928045668> This command only works in a server.`) ], ephemeral: true }); } const status = interaction.options.getBoolean('status'); const userId = interaction.user.id; const member = interaction.guild?.members.cache.get(userId) ?? await interaction.guild.members.fetch(userId).catch((err) => { console.error(err); return null; }); const voiceChannel = member?.voice?.channel; if (!voiceChannel) { return await interaction.reply({ embeds: [new EmbedBuilder() .setColor('#ff5555') .setDescription(`<:check_unsuccess:1351159295928045668> You must be in a voice channel to use this command.`) ], ephemeral: true }); } const player = discordClient.manager.players.get(guildId); if (!player) { return await interaction.reply({ embeds: [new EmbedBuilder() .setColor('#ff5555') .setDescription(`<:check_unsuccess:1351159295928045668> There is no active player.`) ], ephemeral: true }); } const botMember = interaction.guild.members.cache.get(discordClient.user.id) ?? await interaction.guild.members.fetch(discordClient.user.id).catch((err) => { console.error(err); return null; }); const botVoiceChannel = botMember?.voice?.channel; if (botVoiceChannel && botVoiceChannel.id !== voiceChannel.id) { return await interaction.reply({ embeds: [new EmbedBuilder() .setColor('#ff5555') .setDescription(`<:check_unsuccess:1351159295928045668> You must be in the same voice channel as the bot (**${botVoiceChannel.name}**) to change autoplay.`) ], ephemeral: true }); } try { player.setAutoPlay(status); const embed = new EmbedBuilder() .setColor('#00ccff') .setTitle(`<:check_success:1351159102675484742> Autoplay ${status ? 'Enabled' : 'Disabled'}`) .setDescription(`Autoplay has been turned **${status ? 'on <:check_success:1351159102675484742>' : 'off <:check_unsuccess:1351159295928045668>'}** for this session.`) .setFooter({ text: `Changed by ${interaction.user.username}`, iconURL: interaction.user.displayAvatarURL() }) .setTimestamp(); return await interaction.reply({ embeds: [embed] }); } catch (error) { console.error("[autoplay command] Failed to toggle:", error); Sentry.captureException(error); return await interaction.reply({ embeds: [new EmbedBuilder() .setColor('#ff5555') .setDescription(`<:check_unsuccess:1351159295928045668> Something went wrong while setting autoplay.`) ], ephemeral: true }); } } };