javascript - 使用 .setSelfDeaf() 时如何修复此 ReferenceError?
问题描述
我试图在我的代码中使用 .setSelfDeaf() 但我无法弄清楚。我尝试的每一种方法最终都会在执行时使机器人崩溃。
有人可以帮我使用它吗?我想要的是机器人每次加入语音频道时都聋了。
编辑:根据要求添加了我收到的错误和更新的代码。
我得到的错误:
2021-03-04T06:25:06.238627+00:00 app[worker.1]: /app/code.js:53
2021-03-04T06:25:06.238628+00:00 app[worker.1]: connection.voice.setSelfDeaf(true);
2021-03-04T06:25:06.238629+00:00 app[worker.1]: ^
2021-03-04T06:25:06.238629+00:00 app[worker.1]:
2021-03-04T06:25:06.238629+00:00 app[worker.1]: ReferenceError: connection is not defined
2021-03-04T06:25:06.238630+00:00 app[worker.1]: at Client.<anonymous> (/app/code.js:53:5)
2021-03-04T06:25:06.238630+00:00 app[worker.1]: at Client.emit (events.js:327:22)
2021-03-04T06:25:06.238631+00:00 app[worker.1]: at VoiceStateUpdate.handle (/app/node_modules/discord.js/src/client/actions/VoiceStateUpdate.js:40:14)
2021-03-04T06:25:06.238632+00:00 app[worker.1]: at Object.module.exports [as VOICE_STATE_UPDATE] (/app/node_modules/discord.js/src/client/websocket/handlers/VOICE_STATE_UPDATE.js:4:35)
2021-03-04T06:25:06.238632+00:00 app[worker.1]: at WebSocketManager.handlePacket (/app/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
2021-03-04T06:25:06.238633+00:00 app[worker.1]: at WebSocketShard.onPacket (/app/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
2021-03-04T06:25:06.238633+00:00 app[worker.1]: at WebSocketShard.onMessage (/app/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
2021-03-04T06:25:06.238634+00:00 app[worker.1]: at WebSocket.onMessage (/app/node_modules/ws/lib/event-target.js:132:16)
2021-03-04T06:25:06.238634+00:00 app[worker.1]: at WebSocket.emit (events.js:315:20)
2021-03-04T06:25:06.238634+00:00 app[worker.1]: at Receiver.receiverOnMessage (/app/node_modules/ws/lib/websocket.js:825:20)
2021-03-04T06:25:06.279643+00:00 heroku[worker.1]: Process exited with status 1
2021-03-04T06:25:06.340294+00:00 heroku[worker.1]: State changed from up to crashed
更新后的代码:
const Discord = require('discord.js'),
DisTube = require('distube'),
client = new Discord.Client(),
config = {
prefix: "em!",
token: process.env.TOKEN || "[insert discord bot token]"
};
const distube = new DisTube(client, { searchSongs: true, emitNewSongOnly: true });
client.on('ready', () => {
console.log(`e-Music is up and running`);
client.user.setActivity(`em!play`)
});
client.on("message", async (message) => {
if (message.author.bot) return;
if (!message.content.startsWith(config.prefix)) return;
const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
const command = args.shift();
if (command == "play")
distube.play(message, args.join(" "));
if (["repeat", "loop"].includes(command))
distube.setRepeatMode(message, parseInt(args[0]));
if (command == "stop") {
distube.stop(message);
message.channel.send("Stopped the music!");
}
if (command == "skip")
distube.skip(message);
if (command == "queue") {
let queue = distube.getQueue(message);
message.channel.send('Current queue:\n' + queue.songs.map((song, id) =>
`**${id + 1}**. ${song.name} - \`${song.formattedDuration}\``
).slice(0, 10).join("\n"));
}
if ([`3d`, `bassboost`, `echo`, `karaoke`, `nightcore`, `vaporwave`].includes(command)) {
let filter = distube.setFilter(message, command);
message.channel.send("Current queue filter: " + (filter || "Off"));
}
});
client.on("voiceStateUpdate", (oldVoiceState, newVoiceState) => {
if (newVoiceState.id == client.user.id) {
connection.voice.setSelfDeaf(true);
};
});
const status = (queue) => `Volume: \`${queue.volume}%\` | Filter: \`${queue.filter || "Off"}\` | Loop: \`${queue.repeatMode ? queue.repeatMode == 2 ? "All Queue" : "This Song" : "Off"}\` | Autoplay: \`${queue.autoplay ? "On" : "Off"}\``;
distube
.on("playSong", (message, queue, song) => message.channel.send(
`Playing \`${song.name}\` - \`${song.formattedDuration}\`\nRequested by: ${song.user}\n${status(queue)}`
))
.on("addSong", (message, queue, song) => message.channel.send(
`Added ${song.name} - \`${song.formattedDuration}\` to the queue by ${song.user}`
))
.on("playList", (message, queue, playlist, song) => message.channel.send(
`Play \`${playlist.name}\` playlist (${playlist.songs.length} songs).\nRequested by: ${song.user}\nNow playing \`${song.name}\` - \`${song.formattedDuration}\`\n${status(queue)}`
))
.on("addList", (message, queue, playlist) => message.channel.send(
`Added \`${playlist.name}\` playlist (${playlist.songs.length} songs) to queue\n${status(queue)}`
))
.on("searchResult", (message, result) => {
let i = 0;
message.channel.send(`**Choose an option from below**\n${result.map(song => `**${++i}**. ${song.name} - \`${song.formattedDuration}\``).join("\n")}\n*Enter anything else or wait 60 seconds to cancel*`);
})
.on("searchCancel", (message) => message.channel.send(`Searching canceled`))
.on("error", (message, e) => {
console.error(e)
message.channel.send("An error encountered: " + e);
});
client.login(config.token);
解决方案
如果您阅读错误ReferenceError: connection is not defined
,它会告诉您哪里出了问题。connection
在以下代码中不是变量:
client.on("voiceStateUpdate", (oldVoiceState, newVoiceState) => {
if (newVoiceState.id == client.user.id) {
connection.voice.setSelfDeaf(true);
};
});
虽然有一个connection
属性,newVoiceState
但在这种情况下这不是您想要的。您可能希望使用以下.setSelfDeaf
方法VoiceState
:
client.on("voiceStateUpdate", (oldVoiceState, newVoiceState) => {
if (newVoiceState.id == client.user.id) {
newVoiceState.setSelfDeaf(true);
};
});
推荐阅读
- c++ - QTooltip 不需要的行为。按钮中的图标在工具提示中作为背景部分可见
- php - 如何在 yii2 中使用子模板中的树枝管理资产?
- angular - Angular:是否可以关闭离线支持并仅对 ServiceWorkers 使用 swUpdate 服务?
- python - pyenv 垫片有时不可执行
- python - Aiohttp 发出 400 错误请求。但请求工作正常
- java - 无法将 main 的 @Configuration 的 @Bean 自动装配到测试 @Component
- php - Docker 容器 Apache+MapServer 和 Apache+PHP+Mapscript
- sql - 将值合并到按项目 ID 分组的单行中
- java - 如何在 Java 中获取 Windows 的桌面位置
- vue.js - 修改代码后 Gridsome 丢失 $page