Discord.js正在播放声音文件,但没有声音输出 [英] Discord.js Playing Sound file but no sound is coming out

查看:0
本文介绍了Discord.js正在播放声音文件,但没有声音输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

const Discord = require("discord.js");
require('dotenv').config();
const { joinVoiceChannel, createAudioPlayer, createAudioResource, AudioPlayerStatus } = require('@discordjs/voice');

const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"], partials: ["CHANNEL"] });

const player = createAudioPlayer();

var channelsToMonitor = ['902193996355485778'];

function joinChannel(channel) {
    const connection = joinVoiceChannel({
        channelId: channel.id,
        guildId: channel.guild.id,
        adapterCreator: channel.guild.voiceAdapterCreator,
    });
    return connection;
}

function playAudio(connection) {
    // Subscribe the connection to the audio player (will play audio on the voice connection)
    const resource = createAudioResource('./music/', 'alarm.mp3');
    resource.volume = 1;
    player.play(resource);

    connection.subscribe(player);

    player.on(AudioPlayerStatus.Playing, () => {
        console.log('ALRM');
    });
}

client.on('ready', () => {
    console.log('ready');
})

client.on('messageCreate', async msg => {
    try {
        if (channelsToMonitor.indexOf(msg.channel.id) !== -1) {
            if (msg.content == 'GOGOGO') {
                const guild = client.guilds.cache.get("857332849119723520");
                const channel = guild.channels.cache.get("921415774676058152");
                if (!channel) return console.error("The channel does not exist!");
                var connection = joinChannel(channel);
                await playAudio(connection);
            }
        } else {
            if (msg.author.bot) return;
        }
    } catch (err) {
        console.error(err.message);
    }
});


client.login(process.env.DISCORD_TOKEN_2);

我已从docs

设置了此设置

我找不到为什么没有音频输出!当我使用console.log(player)时,机器人加入频道并说它正在播放,但由于某种原因,它耳聋了,没有播放任何声音。

推荐答案

Bot现在默认情况下使用自聋加入。提供selfDeaf字段以停止此操作:

const connection = joinVoiceChannel({
    channelId: channel.id,
    guildId: channel.guild.id,
    adapterCreator: channel.guild.voiceAdapterCreator,
    selfDeaf: false,
    selfMute: false // this may also be needed
})

这篇关于Discord.js正在播放声音文件,但没有声音输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆