buddy

node MVC discord bot
Log | Files | Refs | README

TYPING_START.js (1450B)


      1 'use strict';
      2 
      3 const { Events } = require('../../../util/Constants');
      4 
      5 module.exports = (client, { d: data }) => {
      6   const channel = client.channels.cache.get(data.channel_id);
      7   const user = client.users.cache.get(data.user_id);
      8   const timestamp = new Date(data.timestamp * 1000);
      9 
     10   if (channel && user) {
     11     if (channel.type === 'voice') {
     12       client.emit(Events.WARN, `Discord sent a typing packet to a voice channel ${channel.id}`);
     13       return;
     14     }
     15 
     16     if (channel._typing.has(user.id)) {
     17       const typing = channel._typing.get(user.id);
     18 
     19       typing.lastTimestamp = timestamp;
     20       typing.elapsedTime = Date.now() - typing.since;
     21       client.clearTimeout(typing.timeout);
     22       typing.timeout = tooLate(channel, user);
     23     } else {
     24       const since = new Date();
     25       const lastTimestamp = new Date();
     26       channel._typing.set(user.id, {
     27         user,
     28         since,
     29         lastTimestamp,
     30         elapsedTime: Date.now() - since,
     31         timeout: tooLate(channel, user),
     32       });
     33 
     34       /**
     35        * Emitted whenever a user starts typing in a channel.
     36        * @event Client#typingStart
     37        * @param {Channel} channel The channel the user started typing in
     38        * @param {User} user The user that started typing
     39        */
     40       client.emit(Events.TYPING_START, channel, user);
     41     }
     42   }
     43 };
     44 
     45 function tooLate(channel, user) {
     46   return channel.client.setTimeout(() => {
     47     channel._typing.delete(user.id);
     48   }, 10000);
     49 }