Skip to content
Advertisement

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

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});

Advertisement

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});
Advertisement