Is there a way to make this code shorter? (reaction collector)

Tags: , , ,



const backwardsFilter = (reaction, user) => reaction.emoji.name === 'âª' && user.id === message.author.id;
const forwardsFilter = (reaction, user) => reaction.emoji.name === 'â©' && user.id === message.author.id;
const backwards = msg.createReactionCollector(backwardsFilter, {time: 90000});
const forwards = msg.createReactionCollector(forwardsFilter, {time: 90000});

I tried to make one filter for both collectors but still have to type this (x,y,z) => filter(x,y,z,'âª')

const filter = (reaction, user, c, emoji) => reaction.emoji.name === emoji && user.id === message.author.id;
const backwards = msg.createReactionCollector((x,y,z) => filter(x,y,z,'âª'), {time: 90000});
const forwards = msg.createReactionCollector((x,y,z) => filter(x,y,z,'â©'), {time: 90000});

Answer

You can make a higher-order function, one that takes in the character you’re looking for and returns a function that takes three arguments (reaction, user, and c, corresponding to your current (x, y, x) =>) and returns the appropriate filter operation.

Also, it doesn’t look like c (same as z) is being used at all in any code here, so feel free to remove it from the argument list.

const makeFilter = emoji => (reaction, user) => reaction.emoji.name === emoji && user.id === message.author.id;
const backwards = msg.createReactionCollector(makeFilter('âª'), {time: 90000});
const forwards = msg.createReactionCollector(makeFilter('â©'), {time: 90000});


Source: stackoverflow