.join().then() 中的代码未执行
Code within .join().then() not executing
这是我遇到的问题。
当我键入 !join
时,我的机器人会按预期加入正确的语音频道。但是 console.log("Successfully connected.");
消息不会出现。我放在 join().then(connection => { //code here});
正文中的任何代码都没有执行。
如果我在机器人已经在语音通道中时再次输入 !join
,机器人会说 console.log("Already connected");
,这是正确的。
我的 join().then()
方法有什么问题吗?
我使用的是 discord.js 文档 https://discord.js.org/#/docs/main/master/class/VoiceConnection
中显示的确切示例
注意:所有这些代码都包含在一个 async
函数中,如下所示 bot.on("message", async message => {// code here. });
if (cmd === `${prefix}join`) {
let channel = message.guild.channels.find(`name`, "bot-commands");
if (message.member.voiceChannel) {
if (!message.guild.me.voiceChannel) {
message.member.voiceChannel.join()
.then(connection => {
// Code here doesn't execute, but the bot still joins.
// All code here should execute when the bot joins a voice channel.
console.log("Successfully connected.");
}).catch(e => {
// This error isn't being thrown when the bot joins.
console.log(e);
});
} else {
// This will execute when I type !join when the bot is already in the VC.
console.log("Already connected");
}
}
}
这也是我的package.json
{
"name": "discordbottest",
"version": "1.0.0",
"description": "A test bot",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Me",
"license": "ISC",
"dependencies": {
"discord.js": "^11.3.2",
"ffmpeg-binaries": "^3.2.2-3",
"opusscript": "0.0.6"
}
}
谢谢。
编辑:
尝试按照建议使用 await
:
if (cmd === `${prefix}join`) {
let channel = message.guild.channels.find(`name`, "bot-commands");
if (message.member.voiceChannel) {
if (!message.guild.me.voiceChannel) {
console.log("Before connection"); //This line is executed.
let connection = await message.member.voiceChannel.join();
console.log("After connection"); //This line is not executed.
} else {
console.log("Already connected");
return message.channel.send("Hello! I'm already here!");
}
}
}
原来这是我本地机器上的问题。我在工作的业余时间开发了这个机器人,我发现我工作的网络正在阻止语音通道连接完全完成。当我自己加入一个语音频道时,它会卡住说 "RTC Connection" 然后是 "No route" 并且会在这两个消息之间循环。我在家里测试了这个机器人,它没有任何问题。感谢大家在整个过程中提出的建议,我能够了解更多关于 discord.js :)
这是我遇到的问题。
当我键入 !join
时,我的机器人会按预期加入正确的语音频道。但是 console.log("Successfully connected.");
消息不会出现。我放在 join().then(connection => { //code here});
正文中的任何代码都没有执行。
如果我在机器人已经在语音通道中时再次输入 !join
,机器人会说 console.log("Already connected");
,这是正确的。
我的 join().then()
方法有什么问题吗?
我使用的是 discord.js 文档 https://discord.js.org/#/docs/main/master/class/VoiceConnection
中显示的确切示例注意:所有这些代码都包含在一个 async
函数中,如下所示 bot.on("message", async message => {// code here. });
if (cmd === `${prefix}join`) {
let channel = message.guild.channels.find(`name`, "bot-commands");
if (message.member.voiceChannel) {
if (!message.guild.me.voiceChannel) {
message.member.voiceChannel.join()
.then(connection => {
// Code here doesn't execute, but the bot still joins.
// All code here should execute when the bot joins a voice channel.
console.log("Successfully connected.");
}).catch(e => {
// This error isn't being thrown when the bot joins.
console.log(e);
});
} else {
// This will execute when I type !join when the bot is already in the VC.
console.log("Already connected");
}
}
}
这也是我的package.json
{
"name": "discordbottest",
"version": "1.0.0",
"description": "A test bot",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Me",
"license": "ISC",
"dependencies": {
"discord.js": "^11.3.2",
"ffmpeg-binaries": "^3.2.2-3",
"opusscript": "0.0.6"
}
}
谢谢。
编辑:
尝试按照建议使用 await
:
if (cmd === `${prefix}join`) {
let channel = message.guild.channels.find(`name`, "bot-commands");
if (message.member.voiceChannel) {
if (!message.guild.me.voiceChannel) {
console.log("Before connection"); //This line is executed.
let connection = await message.member.voiceChannel.join();
console.log("After connection"); //This line is not executed.
} else {
console.log("Already connected");
return message.channel.send("Hello! I'm already here!");
}
}
}
原来这是我本地机器上的问题。我在工作的业余时间开发了这个机器人,我发现我工作的网络正在阻止语音通道连接完全完成。当我自己加入一个语音频道时,它会卡住说 "RTC Connection" 然后是 "No route" 并且会在这两个消息之间循环。我在家里测试了这个机器人,它没有任何问题。感谢大家在整个过程中提出的建议,我能够了解更多关于 discord.js :)