我制作了一个抽奖不和谐机器人,该机器人允许用户DM该机器人的名称和抽奖条目数量。一旦设置了值,就可以通过DMing!enter开始抽奖的输入。一旦发生这种情况,将调用一个函数,然后该函数将启动一个for循环,for循环将基于指定的条目数量运行。我还添加了for循环中的延迟,这是因为获得抽奖券的服务需要一些时间(由于敏感的API信息,因此代码已针对SO Post进行了编辑)

完成此操作后,它将DM发送回原来已DM僵尸程序的用户。我面临的问题是,如果多个用户DM同时或从第一个DM运行时,变量将被请求bot的最新人员覆盖。

我假设每次用户DM都使用Discord.js机器人,它将创建脚本或节点进程的新实例?

僵尸程序调用一次DM后调用的函数是否有可能在主节点进程内创建新进程,从而不会被覆盖?

const Discord = require('discord.js');
const botconfig = require('./discordBotConfig.json');
const bot = new Discord.Client({disableEveryone: true});
const c = require('chalk');

// Chalk Theme
const ctx = new c.constructor({level: 2});
const error = c.red;
const waiting = c.magenta;
const success = c.green;
const discordBot = c.yellow;

// Current Raffles (API Link Later)
let activeRaffles = 'Raffle 1';

// User Parmas
let usrName = '';
let entryAmount = 0;

// Ticket
let raffleTicket = [];
let retryDelay = 3000;

function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

// Enter
const enterIn = async () => {
    console.log('User name', usrName);
    raffleTicket.push(Math.random(0, 50));
}

// Init Raffle Entry
const raffleInit = async (entryAmount) => {
    for (let i = 0; i < entryAmount; i++) {
        enterIn();
        await sleep(retryDelay);
    }
    dmUser();
}

const dmUser = () => {
    // Discord Message Complete
    let botCompleteMsg = new Discord.RichEmbed()
    .setTitle('Finished!')
    .setColor('#25E37A')
    .addField('Name: ', usrName)
    .addField('Tickets: ', raffleTicket)
    .addField('Last Update: ', bot.user.createdAt);

    bot.fetchUser(userID).then((user) => {
        user.send(botCompleteMsg);
    });

    return; // End the application
}

// Discord Bot Setup
bot.on('ready', async () => {
    console.log(discordBot(`${bot.user.username} is Online!`));
    bot.user.setActivity('Entering Raffle');
});

bot.on('message', async message => {
    if (message.channel.type === 'dm') {

    let prefix = botconfig.prefix;
    let messageArray = message.content.split(' ');

    let cmd = messageArray[0];
        if (cmd === `${prefix}name`) {
            if (messageArray.length === 3) {
                userID = message.author.id;
                usrName = messageArray[1];
                entryAmount = messageArray[2];

                // Raffle summary
                let raffleSummary = new Discord.RichEmbed()
                .setTitle('Entry Summary')
                .setColor('#8D06FF')
                .addField('Name: ', usrName)
                .addField('Entry Amount: ', entryAmount)
                return message.author.send(raffleSummary), message.author.send('Type **!start** to begin entry or type **!set** again to set the entry details again.');
            }
        }

        if (cmd === `${prefix}enter`) {
            // Raffle summary
            let startMessage = new Discord.RichEmbed()
            .setTitle('Entering raffle!')
            .setDescription('Thanks for entering! :)')
            .setColor('#8D06FF')
            return message.author.send(startMessage), raffleInit(entryAmount);
        }

    }
});

bot.login(botconfig.token);

最佳答案

您可以将用户数据存储在带有类的列表中。

var userData = [
{name: "sample#0000", entryNum: 0}
];

关于node.js - 值被最新人员覆盖以请求机器人?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54339012/

10-11 22:16