Javascript Discord.js应用程序Bot重新发送消息

Javascript Discord.js应用程序Bot重新发送消息,javascript,discord.js,Javascript,Discord.js,我为Discord编写了一个应用程序Bot(就像这一个),但是如果您回答了问题并重新发送了一封信,则Bot会重新发送完整的应用程序。我不明白为什么机器人会这么做。机器人有一个特殊字符的问题,我试图用utf8参数来解决,但它不能正常工作。我的意思是他有时可以展示,但不是一直都可以 const Discord = require('discord.js'); const config = require('./config.js'); let applicationQuestions = requi

我为Discord编写了一个应用程序Bot(就像这一个),但是如果您回答了问题并重新发送了一封信,则Bot会重新发送完整的应用程序。我不明白为什么机器人会这么做。机器人有一个特殊字符的问题,我试图用utf8参数来解决,但它不能正常工作。我的意思是他有时可以展示,但不是一直都可以

const Discord = require('discord.js');
const config = require('./config.js');
let applicationQuestions = require('./application-questions.js', 'utf8');

const client = new Discord.Client();
const botChar = config.prefix;
let usersApplicationStatus = [];
let appNewForm = [];
let isSettingFormUp = false;

var d = new Date(),
    dformat = [d.getMonth()+1,
               d.getDate(),
               d.getFullYear()].join('/')+' '+
              [d.getHours(),
               d.getMinutes(),
               d.getSeconds()].join(':');


const applicationFormCompleted = (data) => {
    let i = 0, answers = "";

    for (; i < applicationQuestions.length; i++) {
        answers += `${applicationQuestions[i]}: ${data.answers[i]}\n`;
    }
    client.channels.get(config.channel).send(`\`\`\` ----------NEW-Member---------- \n\n${answers} \nDate of Application: ${dformat} \nName of Applicant: ${data.user.tag}\`\`\``);
    console.log(`[${dformat}] » Application from ${data.user.tag} has been sent in Application Channel`); //log funktion
};

const sendUserApplyForm = msg => {
    const user = usersApplicationStatus.find(user => user.id === msg.author.id);

    if (!user) {
        client.channels.get(config.applychannel).send(`\`\`\` Application started in DM by ${msg.author.username}\`\`\``);
        console.log(`[${dformat}] » Application started by ${msg.author.tag}`); //log funktion

        msg.author.send(`Application commands: \`\`\`${botChar}cancel, ${botChar}redo\`\`\``);
        msg.author.send(applicationQuestions[0]);
        usersApplicationStatus.push({id: msg.author.id, currentStep: 0, answers: [], user: msg.author});
        console.log(`[${dformat}] » DM sent to ${msg.author.tag}`); //log funktion
    } else {
        msg.author.send(applicationQuestions[user.currentStep]);
        console.log(`step`); //dev

    }
};

const cancelUserApplicationForm = (msg, isRedo = false) => {
    const user = usersApplicationStatus.find(user => user.id === msg.author.id);

    if (user) {
        usersApplicationStatus = usersApplicationStatus.filter(el => el.id !== user.id);
        msg.reply("Application canceled.");
        console.log(`[${dformat}] » Application canceled in DM by ${msg.author.tag}`); //log funktion
    } else if (!isRedo) {
        msg.reply("You have not started an application form yet.");
    }
};

/*const applicationFormSetup = (msg) => {
    if (!msg.guild) {
        msg.reply("This command can only be used in a guild.");
        return;
    }

    if (!msg.member.roles.find("name", "Admin")) {
        msg.reply("This command can only be used by an admin.");
        return;
    }

    if (isSettingFormUp) {
        msg.reply("Someone else is already configuring the form.");
        return;
    }

    appNewForm = [];
    isSettingFormUp = msg.author.id;

    msg.author.send(`Enter questions and enter \`${botChar}endsetup\` when done.`);
};

const endApplicationFormSetup = (msg) => {
    if (isSettingFormUp !== msg.author.id) {
        msg.reply("You are not the one setting the form up.");
        return;
    }

    isSettingFormUp = false;
    applicationQuestions = appNewForm;
}; */

//status
client.on('ready', () => {
    console.log(`[${dformat}] » Logged in as ${client.user.tag}!`); //log funktion
    client.user.setActivity(`${config.prefix}help`, {
    type: "STREAMING",
    url: "https://www.twitch.tv/routerabfrage"
    });
});


client.on('message', msg => {
    if (msg.content.charAt(0) === botChar) {
        const request = msg.content.substr(1);
        let command, parameters = [];

        if (request.indexOf(" ") !== -1) {
            command = request.substr(0, request.indexOf(" "));
            parameters = request.split(" ");
            parameters.shift();
        } else {
            command = request;
        }

        switch (command.toLowerCase()) {
            case "apply":
                sendUserApplyForm(msg);
                break;
            case "cancel":
                cancelUserApplicationForm(msg);
                break;
            case "redo":
                cancelUserApplicationForm(msg, true);
                sendUserApplyForm(msg);
                break;
            /*case "setup":
                applicationFormSetup(msg);
                break;
            case "endsetup":
                endApplicationFormSetup(msg);
                break; */
            case "help":
                msg.reply(`Available commands: \`\`\`${botChar}apply, ${botChar}help\`\`\``);
                break;
            default:
                msg.reply("I do not know this command.");
                console.log(`[${dformat}] » Unknown Command from ${msg.author.tag} -> ${msg.content}`); //log funktion
        }
    } else {
        if (msg.channel.type === "dm") {
            if (msg.author.id === isSettingFormUp) {
                appNewForm.push(msg.content);
            } else {
                const user = usersApplicationStatus.find(user => user.id === msg.author.id);

                if (user && msg.content) {
                    user.answers.push(msg.content);
                    user.currentStep++;

                    if (user.currentStep >= applicationQuestions.length) {
                        applicationFormCompleted(user);
                        msg.author.send("Congratulations your application has been sent!");
                    } else {
                        msg.author.send(applicationQuestions[user.currentStep]); // bot sendet nächste frage
                    }
                }
            }
        }
    }
});

client.login(config.token);
以及config.js:

module.exports = {
  token: "",
  prefix: "!",
  channel: "703217993597059073",
  applychannel: "703218013721067550"
};

这段代码有点草率,到处都是,我真的建议改为使用模块,而不是switch语句

无论如何,这是我写的代码,并没有完全完成所有代码,因为我希望您能够:

不过它是基于模块的,而且.apply仅适用于服务器通道,但两者都很容易更改

相关配置json:

{
    "application": {
        "channelId": "704158687375261706",
        "questions": [
            "How's your wife",
            "How many times a day do you",
            "What do you think is the purpose of life"
        ]
    }
}

这段代码有点草率,到处都是,我真的建议改为使用模块,而不是switch语句

无论如何,这是我写的代码,并没有完全完成所有代码,因为我希望您能够:

不过它是基于模块的,而且.apply仅适用于服务器通道,但两者都很容易更改

相关配置json:

{
    "application": {
        "channelId": "704158687375261706",
        "questions": [
            "How's your wife",
            "How many times a day do you",
            "What do you think is the purpose of life"
        ]
    }
}

注释掉的代码是否相关?如果没有,您可以删除它,以便更容易阅读注释掉的代码是否相关?如果没有,你可以删除它,以便更容易阅读