Comments (2)
Hello, I also would appreciate finally turning entire code into TypeScript base, or at least adding raw @types.
It would be also handy finally updating package on NPM.
I made TypeScript version of your code, @riyacchi. It's supports all mechanics you had before:
import { Emoji, Member, Message } from 'eris';
import { EventEmitter } from 'events';
export interface dataType {
msg: Message,
emoji: { name: string, id: string | null, animated?: boolean},
userID: string
}
export default class ReactionCollector extends EventEmitter {
constructor(message: Message, filter: (reactorID: string) => boolean, options?: { maxMatches?: number, time?: number }) {
super();
this.client = message.channel.client;
this.filter = filter;
this.message = message;
this.options = options ? options : {};
this.ended = false;
this.collected = [];
this.listener = (msg, emoji, reactor) => this.checkPreConditions(msg, emoji, reactor);
this.client.on('messageReactionAdd', this.listener);
if(this.options.time) setTimeout(() => this.stopListening('timeout'), options.time);
new Promise((resolve) => {
this.on('end', resolve);
});
}
private client; filter; message; ended;
private options: { maxMatches?: number, time?: number };
private collected: dataType[];
private listener: (msg: Message, emoji: Emoji, reactor: Member) => boolean;
private checkPreConditions(msg: Message, emoji: Emoji, reactor: Member): boolean {
if (this.message.id !== msg.id) {
return false;
}
if (this.filter(reactor.id)) {
this.collected.push({ msg, emoji, userID: reactor.id });
this.emit('collected', { msg, emoji, userID: reactor.id } as dataType);
if (this.options.maxMatches && this.collected.length >= this.options.maxMatches) {
this.stopListening('maxMatches');
return true;
}
}
return false;
}
private stopListening(reason: 'timeout' | 'maxMatches') {
if (this.ended) return;
this.ended = true;
this.client.removeListener('messageReactionAdd', this.listener);
this.emit('end', this.collected, reason);
}
}
Example usage with Eris:
require('dotenv').config();
import { Client } from 'eris';
import ReactionCollector, { dataType } from './client/reactionCollector';
const client = new Client(process.env.BOT_TOKEN);
client.on(`messageCreate`, async (msg) => {
if(msg.content === 'cookie') {
const m = await msg.channel.createMessage(`Click emoji to get cookie!`);
await m.addReaction('🍪');
const collector = new ReactionCollector(m, (userID) => userID === msg.author.id, { maxMatches: 1, time: 60000 });
collector.on('collected', async (data: dataType) => {
await msg.channel.createMessage(`Here, ${client.users.get(data.userID).username} grab your 🍪.`);
});
}
});
client.connect();
Like you can see, I removed "permanent" parameter. Generally it is quite stupid for me to create infinite await event in cache, but do like you want. You do not need to pass last parameter - options. If maxMatches
is not provided, then that means you do not care about amount, it gonna catch reactions infinitely. The same goes with time
. If you do not set time limit (in ms), then it gonna work infinitely.
from eris-reactions.
I'm not against TypeScript support, however I will not have the entire codebase in TypeScript like proposed above. Feel free to open a PR with typings to support TypeScript better (simple index.d.ts
with a few interfaces to stop the TS compiler from yelling).
from eris-reactions.
Related Issues (3)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from eris-reactions.