Я кодирую бота, используя Discord. js, а в моем коде al oop функция обратного вызова для события "message" не вызывается внутри него, но работает везде во время выполнения. Я сделал тесты, используя инструкции console.log, но не могу найти решение.
Вот код: l oop находится внизу этого блока
module.exports.play = async (client, channel, players) => {
try {
let playersCards = [];
let pyramideCards = [];
emitter.on("ok", message => {
console.log(message.author.username + " : " + message.content);
});
stopEmitter.on("stop", async () => {
await channel.delete();
console.log("jeu fini");
});
await beginMessage(players, channel);
await beginGame(players, playersCards, pyramideCards);
//attendre que les messages se suppriment
await sleep(30000);
for(let i = 0; i < pyramideCards.length; i++)
await continueGame(pyramideCards[i], i, channel, players);
}
catch(e) {
console.error(e);
}
};
async function beginMessage(players, channel) {
let message = "que le jeu commence ";
players.forEach(player => {
message += `${player.toString()} `;
});
await removeSendingMessages(players, channel);
await channel.send(message);
}
async function beginGame(players, playersCards, pyramideCards) {
return new Promise(async (resolve, reject) => {
fs.readFile('Utils/cartes.json', 'utf8', async (err, data) => {
if(err) return reject(err);
let cartes = JSON.parse(data);
cartes = cartes.cartes;
await asyncForEach(players, async player => {
let playerCards = await distributeCards(player, cartes);
playersCards.push(playerCards);
});
distributePyramideCards(pyramideCards, cartes);
if(pyramideCards.length > 0 && playersCards.length > 0)
resolve();
});
});
}
async function distributeCards(player, cartes) {
let playerCards = {
name : player.username,
cards : [],
};
for(let i = 0; i < 4; i++) {
let carte = getRandomCard(cartes);
playerCards.cards.push(carte.name);
playerCards.value = carte.value;
}
playerCards.cards.sort((a, b) => a - b);
let dmChannel = await player.createDM();
let messageSend = await dmChannel.send(`Voici tes cartes retiens les bien tu as 30sec : ${playerCards.cards}`);
let deletedMessage = setTimeout(async () => {await messageSend.delete()},30000);
return playerCards;
}
function distributePyramideCards(pyramideCards, cartes, base = 4) {
for(let i = base; i > 0; i--) {
pyramideCards[pyramideCards.length] = [];
for(let j = 0; j < i; j++) {
pyramideCards[pyramideCards.length-1].push(getRandomCard(cartes));
}
}
}
function getRandomCard(cartes) {
let carte = cartes[Maths.getRandomNumber(0,12)];
let valid = carte.count > 0;
while(!valid) {
carte = cartes[Maths.getRandomNumber(0,12)];
}
carte.count--;
return carte;
}
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
async function continueGame(rangee, numRangee, channel, players) {
let voteMess = "";
for(let i = 0; i < players.length; i++) {
voteMess += `pour attaquer ${players[i].username} envoie ${i+1}\n`;
}
await channel.send(new Discord.MessageEmbed()
.setColor("GREY")
.addFields({
name : "règles",
value : `rangée ${numRangee + 1} : ${numRangee + 1} coups en jeu`,
inline : true
},
{
name : "joueurs",
value : voteMess
})
);
for(let i = 0; i < rangee.length; i++)
await playRound(rangee[i], channel, players);
}
async function playRound(carte, channel, players) {
await channel.send(new Discord.MessageEmbed()
.setTitle("prochaine carte")
.setColor("RED")
.addField("carte", carte.name)
);
let playersCopy = [];
players.forEach(player => {
playersCopy.push({
name : player.username,
ok : false
});
});
await attack(carte, channel, players, playersCopy);
}
async function attack(carte, channel, players, playersCopy) {
let valid = playersCopy.every(player => player.ok == true);
await addSendingMessages(players, channel);
let me = await channel.send("phase d'attaque quand vous avez fini envoyer 'ok'");
while(!valid) {
// !!!!!! in this loop !!!!
let valid = playersCopy.every(player => player.ok == true);
}
}
и вот функция обратного вызова