【发布时间】:2020-12-03 18:52:39
【问题描述】:
我正在创建一个 discord.js 机器人,但我想为应用程序添加更多命令。例如,我想要一个命令!apply1 和命令!apply2。当我复制代码、再次粘贴、更改命令名称并键入命令时,它会发送两三个问题而不是一个。我应该改变什么?
如果您不明白并想查看我在说什么,请加入这个虚假的不和谐服务器:https://discord.gg/fVsQaa6(它每次发送 2 个问题,我想 1 接 1 发送。另外,它会将答案发送到他们两个的频道相同,我检查了频道ID是否正确。)
代码:
// Requires \\
const botSettings = require("./botsettings.json");
const Discord = require("discord.js");
const prefix = botSettings.prefix;
const bot = new Discord.Client({disableEveryone: true});
// Bot On Ready Console \\
bot.on('ready', () => {
console.log("Bot Is Ready");
bot.user.setActivity("Great", {type: "WATCHING"})
})
// Staff \\
let userApplications = {}
bot.on("message", function(message) {
if (message.author.equals(bot.user)) return;
let authorId = message.author.id;
if (message.content === "!apply staff") {
console.log(`Apply begin for authorId ${authorId}`);
if (!(authorId in userApplications)) {
userApplications[authorId] = { "step" : 1}
message.author.send(" ")
message.author.send(">>> Staff Application")
message.author.send(">>> Question 1:");
}
} else {
if (message.channel.type === "dm" && authorId in userApplications) {
let authorApplication = userApplications[authorId];
if (authorApplication.step == 1 ) {
authorApplication.answer1 = message.content;
message.author.send(">>> Question 2:");
authorApplication.step ++;
}
else if (authorApplication.step == 2) {
authorApplication.answer2 = message.content;
message.author.send(">>> Question 3:");
authorApplication.step ++;
}
else if (authorApplication.step == 3) {
authorApplication.answer3 = message.content;
message.author.send(">>> Question 4:");
authorApplication.step ++;
}
else if (authorApplication.step == 4) {
authorApplication.answer4 = message.content;
message.author.send(">>> Question 5:");
authorApplication.step ++;
}
else if (authorApplication.step == 5) {
authorApplication.answer5 = message.content;
message.author.send(">>> Question 6:");
authorApplication.step ++;
}
else if (authorApplication.step == 6) {
authorApplication.answer6 = message.content;
message.author.send(">>> Question 7:");
authorApplication.step ++;
}
else if (authorApplication.step == 7) {
authorApplication.answer7 = message.content;
message.author.send(">>> Question 8:");
authorApplication.step ++;
}
else if (authorApplication.step == 8) {
authorApplication.answer8 = message.content;
message.author.send(">>> Thank You!");
bot.channels.cache.get("743550883589259287")
.send(`${message.author}\n >>> Question 1: | ${authorApplication.answer1}\n Question 2: | ${authorApplication.answer2}\n Question 3: | ${authorApplication.answer3}\n Question 4: | ${authorApplication.answer4}\n Question 5: | ${authorApplication.answer5}\n Question 6: | ${authorApplication.answer6}\n Question 7: | ${authorApplication.answer7}\n Question 8: | ${authorApplication.answer8}`);
delete userApplications[authorId];
}
}
}
});
// Police \\
bot.on("message", function(message) {
if (message.author.equals(bot.user)) return;
let authorId = message.author.id;
if (message.content === "!apply police") {
console.log(`Apply begin for authorId ${authorId}`);
if (!(authorId in userApplications)) {
userApplications[authorId] = { "step" : 1}
message.author.send(" ")
message.author.send(">>> Police Application")
message.author.send(">>> Question 1:");
}
} else {
if (message.channel.type === "dm" && authorId in userApplications) {
let authorApplication = userApplications[authorId];
if (authorApplication.step == 1 ) {
authorApplication.answer1 = message.content;
message.author.send(">>> Question 2:");
authorApplication.step ++;
}
else if (authorApplication.step == 2) {
authorApplication.answer2 = message.content;
message.author.send(">>> Question 3:");
authorApplication.step ++;
}
else if (authorApplication.step == 3) {
authorApplication.answer3 = message.content;
message.author.send(">>> Question 4:");
authorApplication.step ++;
}
else if (authorApplication.step == 4) {
authorApplication.answer4 = message.content;
message.author.send(">>> Question 5:");
authorApplication.step ++;
}
else if (authorApplication.step == 5) {
authorApplication.answer5 = message.content;
message.author.send(">>> Question 6:");
authorApplication.step ++;
}
else if (authorApplication.step == 6) {
authorApplication.answer6 = message.content;
message.author.send(">>> Question 7:");
authorApplication.step ++;
}
else if (authorApplication.step == 7) {
authorApplication.answer7 = message.content;
message.author.send(">>> Question 8:");
authorApplication.step ++;
}
else if (authorApplication.step == 8) {
authorApplication.answer8 = message.content;
message.author.send(">>> Thank You!");
bot.channels.cache.get("743558960677912636")
.send(`${message.author}\n >>> Question 1: | ${authorApplication.answer1}\n Question 2: | ${authorApplication.answer2}\n Question 3: | ${authorApplication.answer3}\n Question 4: | ${authorApplication.answer4}\n Question 5: | ${authorApplication.answer5}\n Question 6: | ${authorApplication.answer6}\n Question 7: | ${authorApplication.answer7}\n Question 8: | ${authorApplication.answer8}`);
delete userApplications[authorId];
}
}
}
});
bot.login(botSettings.token);
【问题讨论】:
-
@Lioness100 为什么?他们正在工作,但是当我输入其中一个时,机器人一次发送 2 或 3 个问题,而不是必须的。但是当我有 1 个命令时,它可以完美运行
-
它应该可以工作。当您在 dms 中发送 1 或 2 时,它应该发送好的问题。你的代码是正确的。
-
@Androz2091 每次发2个问题,我要1个1发
-
关于您上次的编辑,请阅读How should we treat posts that are vandalized by their original authors? 请不要通过破坏您的帖子来为其他人做更多的工作。通过在 Stack Overflow 上发帖,您已根据 CC BY-SA 4.0 许可授予 Stack Overflow 分发该内容的不可撤销权利。根据 Stack Overflow 政策,任何破坏行为都将被撤销。
标签: javascript discord.js