Discord.js 播放声音文件但没有声音出来

Posted

技术标签:

【中文标题】Discord.js 播放声音文件但没有声音出来【英文标题】:Discord.js Playing Sound file but no sound is coming out 【发布时间】:2022-01-20 12:52:46 【问题描述】:
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) 时,机器人加入频道并说它正在播放,但由于某种原因,它被聋了并且不播放任何声音。

【问题讨论】:

机器人是在你身边,还是在机器人身边? 从机器人方面@MrMythical 【参考方案1】:

机器人现在默认加入自聋。提供 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 播放声音文件但没有声音出来的主要内容,如果未能解决你的问题,请参考以下文章

discord.js 机器人在没有声音的情况下播放音频

解决:win10有提示音,但是播视频没有声音

使用 AudioToolBox 播放声音文件的类编译但没有声音

即使在 linux 中没有麦克风,声音输入中也存在声音输出

解决树莓派播放音频时耳机插线了但没有声音

是否可以通过安装在 Heroku 上的不和谐机器人播放音乐/声音?