Untitled

 avatar
unknown
plain_text
3 years ago
4.7 kB
2
Indexable
const { Client, VoiceChannel, Intents } = require('discord.js');
const dotenv = require('dotenv');

dotenv.config()

const {
	joinVoiceChannel,
	createAudioPlayer,
	createAudioResource,
	entersState,
	StreamType,
	AudioPlayerStatus,
	VoiceConnectionStatus,
} = require('@discordjs/voice');

const { createDiscordJSAdapter } = require('@discordjs/voice');

const player = createAudioPlayer();

function playSong() {
    const resource = createAudioResource('https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3', {
		inputType: StreamType.Arbitrary,
	});
    player.play(resource);
	return entersState(player, AudioPlayerStatus.Playing, 5e3);
}
async function connectToChannel(channel, VoiceChannel) {
	/**
	 * Here, we try to establish a connection to a voice channel. If we're already connected
	 * to this voice channel, @discordjs/voice will just return the existing connection for us!
	 */
	const connection = joinVoiceChannel({
		channelId: channel.id,
		guildId: channel.guild.id,
		adapterCreator: createDiscordJSAdapter(channel),
        selfDeaf: true,
	});

	/**
	 * If we're dealing with a connection that isn't yet Ready, we can set a reasonable
	 * time limit before giving up. In this example, we give the voice connection 30 seconds
	 * to enter the ready state before giving up.
	 */
	try {
		/**
		 * Allow ourselves 30 seconds to join the voice channel. If we do not join within then,
		 * an error is thrown.
		 */
		await entersState(connection, VoiceConnectionStatus.Ready, 30e3);
		/**
		 * At this point, the voice connection is ready within 30 seconds! This means we can
		 * start playing audio in the voice channel. We return the connection so it can be
		 * used by the caller.
		 */
		return connection;
	} catch (error) {
		/**
		 * At this point, the voice connection has not entered the Ready state. We should make
		 * sure to destroy it, and propagate the error by throwing it, so that the calling function
		 * is aware that we failed to connect to the channel.
		 */
		connection.destroy();
		throw error;
	}
}

const client = new Client({
    intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_VOICE_STATES]
});

client.on('ready', async () => {
    console.log('ZIP FM is ready!')
    try {
		await playSong();
		console.log('Song is ready to play!');
	} catch (error) {
		/**
		 * The song isn't ready to play for some reason :(
		 */
		console.error(error);
	}
});

client.on('messageCreate', async (message) => {
	if (!message.guild) return;

	if (message.content === '-join') {
		const channel = message.member?.voice.channel;

		if (channel) {
			/**
			 * The user is in a voice channel, try to connect.
			 */
			try {
				const connection = await connectToChannel(channel);

				/**
				 * We have successfully connected! Now we can subscribe our connection to
				 * the player. This means that the player will play audio in the user's
				 * voice channel.
				 */
				connection.subscribe(player);
				await message.reply('Playing now!');
			} catch (error) {
				/**
				 * Unable to connect to the voice channel within 30 seconds :(
				 */
				console.error(error);
			}
		} else {
			/**
			 * The user is not in a voice channel.
			 */
			void message.reply('Join a voice channel then try again!');
		}
	}
});

    // new WOKCommands(client, {
    //     commandsDir: path.join(__dirname, 'commands'),
    //     typeScript: false,
    //     testServers: ['922132715384479745'],
    // })
    // .setBotOwner(['367675335853998083'])

    // const guildId = '922132715384479745';
    // const guild = client.guilds.cache.get(guildId)
    // let commands

    // if (guild) {
    //     commands = guild.commands
    // } else {
    //     commands = client.application.commands
    // }
    // commands.create({
    //     name: 'pingas',
    //     description: 'Replies with pong.',
    // })



client.on('interactionCreate', async (Interaction) => {
    if (!Interaction.isCommand()) {
        return
    }
    const { commandName, options } = Interaction
    if (commandName === 'pingas') {
        Interaction.reply({
            content: 'pongas',
            // ephemeral: true,
            // with ephemeral = true, only message.author can see the message
        })
    }
})

// client.on('messageCreate', (message) => {
//     if (message.content === 'ping') {
//         message.reply({
//             content: 'pong',
//         })
//     }
// })

client.login(process.env.ZIP_TOKEN)