如何让Discord机器人加入语音频道时取消自身Deafen状态
解决Discord机器人加入语音频道后取消Deafen状态的问题
要让机器人加入语音频道时不处于自身Deafen状态,只需在joinVoiceChannel的配置参数中显式关闭默认的Deafen设置,同时确保客户端启用必要的语音状态意图,具体操作如下:
1. 修改joinVoiceChannel配置
@discordjs/voice的joinVoiceChannel方法默认会将机器人设置为自身Deafen(selfDeaf: true),你需要手动覆盖这个默认值:
const connection = joinVoiceChannel({ channelId: cha, guildId: gui, adapterCreator: client.guilds.cache.get("1080859608257671338").voiceAdapterCreator, selfDeaf: false, // 取消自身Deafen状态 selfMute: false // 可选:显式确保机器人未静音(默认已为false,设置后更稳妥) });
2. 添加语音状态意图
你的客户端缺少GuildVoiceStates意图,这会影响语音频道相关功能的正常运行,需将其加入intents数组:
const client = new Client({ intents: [ GatewayIntentBits.DirectMessages, GatewayIntentBits.Guilds, GatewayIntentBits.GuildBans, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildPresences, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildVoiceStates // 新增此意图 ] });
修改后的完整代码
require('dotenv').config(); //initialize dotenv require('discord-reply'); const { Client, GatewayIntentBits } = require('discord.js'); const client = new Client({ intents: [ GatewayIntentBits.DirectMessages, GatewayIntentBits.Guilds, GatewayIntentBits.GuildBans, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildPresences, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildVoiceStates // 新增语音状态意图 ] }); const { token, voicechannel, list } = require('./config.json'); const { joinVoiceChannel, getVoiceConnection, createAudioPlayer, createAudioResource } = require('@discordjs/voice'); client.on("messageCreate", (message) => { if(message.content.startsWith('!activetest')) { // cha and gui is a const i have somewhere else, it will define the channel id and the guild id const connection = joinVoiceChannel({ channelId: cha, guildId: gui, adapterCreator: client.guilds.cache.get("1080859608257671338").voiceAdapterCreator, selfDeaf: false, // 关闭自身Deafen selfMute: false // 确保未静音 }); } }); // 补充客户端登录代码(若之前未添加) client.login(token);
修改完成后,机器人加入语音频道时将不再处于Deafen状态,可正常播放声音。
内容的提问来源于stack exchange,提问作者rubble
相关产品推荐
相关产品推荐

