不和谐机器人嵌套等待消息

时间:2021-03-14 13:23:51

标签: javascript discord discord.js

尝试使用节点 js 与机器人不和谐地实现德州扑克。 目前有问题尝试嵌套 client.on('message', message => { 调用。这些背后的想法是,第一个查找 !poker 命令,随后的监听加入玩家、玩家下注等。

代码:

const Discord = require('discord.js');
const dotenv = require('dotenv').config();
const Game = require("./classes.js").Game
const Player = require("./classes.js").Player

// create a new Discord client
const client = new Discord.Client();

// when the client is ready, run this code
// this event will only trigger one time after logging in
client.once('ready', () => {
    console.log('Ready!');
});

let prefix = '!';

client.on('message', message => {
    //prevent feedback loops
    if (!message.content.startsWith(prefix) || message.author.bot) return;

    if (message.content.startsWith(`${prefix}poker`)){
        //getting args
        const args = message.content.slice(prefix.length).trim().split(' ');  
        const command = args.shift().toLowerCase();

        //converting to relevant data type
        let no_players = parseInt(args[0]);
        let money = parseInt(args[1]) 

        //initialising game object and card deck
        let game = new Game();
        game.deck = game.createDeck();

        //list to contain players (will be objects from the Player class)
        let players = [];
        //list to contain player usernames, will be used to identify if someone has already joined
        let player_users = [];

        // loop until we have enough players, as specified by the first input arguemnt
        while (players.length <= no_players){
            //now wait for a join message
            client.on('message', message => {
                //if message == join, and the player has not already joined
                if (message.content.startsWith('join')){
                    if (!players.includes(message.author.username)){
                        let newPlayer = new Player(message.author.username,money);
                        players.push(newPlayer);
                        player_users.push(message.author.username);
                    }
                }
            });
        }
        //debugging purposes
        console.log(players)
    }  
    if (message.content.startsWith(`${prefix}hands`)){
        message.channel.send("Here are the poker hands", {files: ["poker-hand-rankings-mobile.png"]});
    }
    
});

client.login(process.env.TOKEN);

discord 中的命令语法看起来像 !poker {number of players} {betting money for each player} 创建卡组等中的其他功能运行良好。

当我在调试器中运行时,它似乎没有进入第二个 client.on 代码块,我在运行 FATAL ERROR: MarkCompactCollector: young object promotion failed Allocation failed - JavaScript heap out of memory 时收到此错误

不确定我的整体方法是否存在缺陷,即 discord.js 不能有两个进程同时等待消息运行。或者如果定义消息之间存在变量重叠。

考虑废弃并迁移到 python,因此将不胜感激任何帮助。

0 个答案:

没有答案