NodeJS 使用discord bot播放音频- discordjs 14

njthzxwz  于 2023-10-17  发布在  Node.js
关注(0)|答案(1)|浏览(144)
const { joinVoiceChannel, createAudioPlayer, VoiceConnectionStatus, createAudioResource } = require('@discordjs/voice');
const { SlashCommandBuilder } = require('discord.js');
const { join } = require('path'); // Dovresti usare 'path' invece di 'node:path'

module.exports = {
  data: new SlashCommandBuilder()
    .setName('play')
    .setDescription('Playing audio in a voice channel'),
  async execute(interaction) {
    // Verifica se l'utente è in un canale vocale
    if (!interaction.member.voice.channel) {
      await interaction.reply('Devi essere in un canale vocale per utilizzare questo comando.');
      return;
    }

    // Connetti il bot al canale vocale
    const connection = joinVoiceChannel({
      channelId: interaction.member.voice.channelId,
      guildId: interaction.guildId,
      adapterCreator: interaction.guild.voiceAdapterCreator,
    });

    connection.on('error', (error) => {
      console.error(`Errore nella connessione al canale vocale: ${error.message}`);
    });
    
    // Gestisci lo stato della connessione
    connection.on(VoiceConnectionStatus.Ready, () => {
      const player = createAudioPlayer();
      const resource = createAudioResource(join(__dirname, '../../img/push-push.mp3'));

      player.play(resource);

      // Connetti il player alla connessione vocale
      connection.subscribe(player);
    });

    
  },
};

我有这个slashcommand发送和播放音频在一个通道语音不和谐的我的机器人。我的机器人发送了音频,但无法阻止它
我想文件被播放,但暂停或停止它的可能性

tez616oj

tez616oj1#

在这段代码中,player.pause()用于暂停播放,player.unpause()用于在必要时恢复播放,player.stop()用于完全停止声音的播放。您可以根据需要在BOT中取消注解相应的行。

connection.on(VoiceConnectionStatus.Ready, () => {
  const player = createAudioPlayer();
  const resource = createAudioResource(join(__dirname, '../../img/push-push.mp3'));

  player.play(resource);

  // Connect the player to the voice connection
  connection.subscribe(player);

  // To pause the sound
  player.pause();

  // To resume playback (if necessary)
  // player.unpause();

  // To stop playback
  // player.stop();
});

相关问题