最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Reaction in a order discord js - Stack Overflow

programmeradmin1浏览0评论

i want to add a message with reactions but i want reaction to have certain order. The problem is when i execute mand to add that message, the reactions add randomly.

Code:

        case "helptest":
	indexTest = 0;
	andUser = message.author.username;
	message.channel.send(mesajeTest[indexTest]).then(function (message) {
		message.react("⏪")
		message.react("▶")
		message.react("◀")
		message.react("⏩")
	});
	break;

i want to add a message with reactions but i want reaction to have certain order. The problem is when i execute mand to add that message, the reactions add randomly.

Code:

        case "helptest":
	indexTest = 0;
	andUser = message.author.username;
	message.channel.send(mesajeTest[indexTest]).then(function (message) {
		message.react("⏪")
		message.react("▶")
		message.react("◀")
		message.react("⏩")
	});
	break;

I want that order which is in code to be in reactions message, any solution ?

Share Improve this question edited Mar 29, 2018 at 18:30 André 4,4974 gold badges32 silver badges58 bronze badges asked Sep 4, 2017 at 22:56 Gradin98Gradin98 3722 silver badges15 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

Solved the problem with async function

		case "helptest":
	indexTest = 0;
	andUser = message.author.username;
	message.channel.send(mesajeTest[indexTest]).then(async function (message) {
		await message.react("⏪")
		await message.react("◀")
		await message.react("▶")
		await message.react("⏩")
	});
	break;

What exactly does your send function return? A Promise of what? Maybe multiple Promises get resolved before the callback of the first one is finished.

Did you try using Promise.each()?

With Promise.each() Iteration happens serially. If the iterator function returns a promise or a thenable, then the result of the promise is awaited before continuing with next iteration.

As OP states, the accepted answer works for this specific case, however i just ran into a similar problem, however my emoji:s are generated, so typing them out on a line isn't an option. So i naturally tried to look over them all and await each in time. However node seems to smart for this and optimized the code to run in parallel instead of sequentially. I ended up having to fold all the promises into a single promise using reduce like this:

[...array of emojis...].reduce((promise, emoji) => promise.then(() => message.react(emoji)), Promise.resolve());

Even though there's an accepted answer I'm going to leave this answer here in hopes that others might find it helpful.

发布评论

评论列表(0)

  1. 暂无评论