【问题标题】:How to take multiple input one by one in - discord.js如何在discord.js中一一接受多个输入
【发布时间】:2021-02-08 22:57:13
【问题描述】:

我如何多次获取用户的输入,存储它,然后发送一个嵌入的输入?

  1. 用户类型命令?start
  2. 机器人回复"Hi type your name here"
  3. 用户键入名称,然后将其存储在变量中
  4. 机器人再次询问"Type your favourite game now"
  5. 用户键入游戏,它再次存储在一个变量中
  6. 然后取变量,然后做成一个embed
const embed = new Discord.MessageEmbed()
    .setTitle("Hobbies")
    .setThumbnail(messsage.author.user.displayAvatarURL())
    .addDescription("<name>")
    .addDescription("<game>")
    .setTimestamp();
message.channel.send(embed);

【问题讨论】:

  • 告诉我们你到目前为止都做了什么。

标签: discord.js


【解决方案1】:

为了解决我创建的小“脚本”,只是为命令的每个状态预定义了例程

script.js

class Script {
    constructor (user, options, callback) {
        if (!user.send) {
            throw "Invalid Userhandle";
        }
        this.user = user;
        this.options = options;
        this.state = 0;
        this.callback = callback;
        this.responses = [];
        if (!!this.options.greeting) {
            this.user.send(this.options.greeting)
                .then()
                .catch(() => console.log(JSON.stringify(this.options.greeting)));
        }
    };
    interpretMessage(message) {
        if (!this.options.validator[this.state] || typeof this.options.validator[this.state] !== 'function') {
            if (!!this.callback) {
                this.callback(this.user, this.responses, false);
                return;
            } else {
                throw "Invalid User Gatherer Object";
            }
        }
        const [msg, steps] = this.options.validator[this.state](message, this.responses);
        this.user.send(msg)
            .then()
            .catch(() => console.error(msg));   
        if (steps > 0 || steps < 0) {
            if (!!this.responses && !!this.responses[this.state]) {
                this.responses[this.state] = message;
            } else {
                this.responses.push(message);
            }
            this.state += steps;
        }
        if (this.state >= this.options.validator.length) {
            this.callback(this.user, this.responses, false);
        }
    };
};

module.exports = Script;

我只在私人消息中使用此方法,这就是我命名的原因: msg_script.js

const Script = require('./classes/script');

let privateInfoGatherer = {};
let privateInfoGathererCallback = {};

function deletePrivateInfoGatherer(usr, out) {
    if (!usr || !usr.id) {
        return;
    }
    privateInfoGathererCallback[usr.id](usr, out);
    delete privateInfoGatherer[usr.id];
    delete privateInfoGathererCallback[usr.id];
};

function PrivateInfoGatherer (usr, opts, callback) {
    if (!usr || !usr.id || !opts || !callback) {
        return;
    }
    privateInfoGatherer[usr.id] = new Script(usr, opts, deletePrivateInfoGatherer);  
    privateInfoGathererCallback[usr.id] = callback;
};

function checkPrivateMessage(msg, args) {
    if (!msg || !msg.author || !privateInfoGatherer[msg.author.id] || msg.guild) {
        return;
    }
    privateInfoGatherer[msg.author.id].interpretMessage(msg.content);
};

module.exports = {
    provide: {
        PrivateInfoGatherer: PrivateInfoGatherer,
    },
    events: {
        message: checkPrivateMessage,
    }
};

我的最终用法是这样的:

const ressource = require('./classes/ressource');

function interpretAuth(msg, args, provider) {
    const usr = msg.author;

    const stage_1 = (msg) => {
        let steps = msg.match("^([A-Za-z0-9_ ]{4,32})$") ? 1 : 0;
        let ret;
        if (msg === 'abort') {
            steps = 100; // will result in ending the script
        } else {
            ret = steps === 1 ? 'And now your Password' : 'Gimme your username';
        }
        return [ret, steps]; 
    };

    const stage_2 = (msg) => {
        let steps = msg.match("^([A-Za-z0-9\\!\\@\\#\\%\\&\\_\\(\\)\\*\\-\\$\\^\\[\\]]+)$") ? 1 : 0;
        let ret;
        if (msg === 'abort') {
            steps = 100;
        } else {
            ret = steps === 1 ? 'I will check the Auth' : 'Your Password man...';
        }
        return [ret, steps]; 
    };

    const options = {
        greeting: 'Ok for Authrole i need your login, so first your username pls',
        validator: [
            stage_1,
            stage_2,
        ]
    };

    const callback = (usr, out) => {
        const [username, password] = out;
        // Now we have all, do what ever you want with it.
    };

    provider.PrivateInfoGatherer(usr, options, callback);
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-24
    • 2018-01-06
    • 2022-01-22
    相关资源
    最近更新 更多