discord.js-pagination icon indicating copy to clipboard operation
discord.js-pagination copied to clipboard

Errors when sending message via DMs

Open arnavguneta opened this issue 4 years ago • 1 comments

The error occurs when you send a paginated message to a user via DMs. Since the bot doesn't have perms to delete messages, reaction.users.remove(msg.author) and curPage.reactions.removeAll() throw this error:

DiscordAPIError: Cannot execute action on a DM channel

I fixed the issue by:

  1. add dispose: true to the createReactionCollector() so it looks like
curPage.createReactionCollector(
	(reaction, user) => emojiList.includes(reaction.emoji.name) && !user.bot,
	{ time: timeout, dispose: true }
);
  1. remove reaction.users.remove(msg.author) from the collect listener
  2. add a new listener for remove, and execute the same code from the collect listener
const managePageChange = (reaction) => {
	switch (reaction.emoji.name) {
		case emojiList[0]:
			page = page > 0 ? --page : pages.length - 1;
			break;
		case emojiList[1]:
			page = page + 1 < pages.length ? ++page : 0;
			break;
		default:
			break;
	}
	curPage.edit(pages[page].setFooter(\`Page ${page + 1} / ${pages.length}\`));
}
reactionCollector.on('collect', reaction => {
	managePageChange(reaction)
});
reactionCollector.on('remove', reaction => {
	managePageChange(reaction)
});
  1. in the end listener, check if channel type is dm, and deal with the timeout accordingly
if (!curPage.deleted) {
	if (msg.channel.type !== 'dm') curPage.reactions.removeAll()
	else curPage.edit(pages[page].setDescription(pages[page].description += '\`\`\`css\n[MESSAGE EXPIRED]\nResend command to view other pages\n\`\`\`'))
}

This accomplishes two things:

  • When a user reacts/unreacts, both act as a next/previous page action. Instead of removing user reactions manually and only listening for reacts, listen for react and unreact
  • For DMs, since you can't remove reactions, when the timeout occurs I edit the embed and add "Message Expired" to the end of the message.

arnavguneta avatar Jan 27 '21 07:01 arnavguneta

I also changed msg. I changed the params to accept a channel instead of a message, and wherever msg.channel was used, I replaced it with channel. If anyone has the same issue, feel free to make these changes.

arnavguneta avatar Jan 27 '21 13:01 arnavguneta