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

时间:2020-04-26 08:45:33

标签: javascript discord.js

我为Discord编写了一个ApplicationBot(就像这个https://top.gg/bot/applicationbot一样),但是如果您回答问题并重新发送信件,则Bot会重新发送完整的应用程序。我不知道为什么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);

然后从application-questions.js中提出问题:

module.exports =  [
    "question 1 ",
    "question 2 "
];

还有config.js:

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

1 个答案:

答案 0 :(得分:0)

这段代码有点草率,到处都是,我真的建议改用模块,而不要切换语句。

无论如何,这是我编写的代码,因为我希望您选择以下选项,所以并不能完全完成所有代码: https://github.com/azul-i/shuxue/blob/master/commands/general/apply.js

尽管它是基于模块化的,并且.apply仅适用于服务器通道,但是两者都可以轻松更改。

相关的config-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"
        ]
    }
}