Skip to content

Commit

Permalink
fix(twitch): properly format duration of timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
sogehige committed Oct 27, 2023
1 parent e661737 commit 55fbe25
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
34 changes: 34 additions & 0 deletions src/helpers/dayjsHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import dayjs from 'dayjs';
import customParseFormat from 'dayjs/plugin/customParseFormat.js';
import duration from 'dayjs/plugin/duration.js';
import localizedFormat from 'dayjs/plugin/localizedFormat.js';
import relativeTime from 'dayjs/plugin/relativeTime.js';
import { default as tz } from 'dayjs/plugin/timezone.js';
import utc from 'dayjs/plugin/utc.js';

dayjs.extend(utc);
dayjs.extend(tz);
dayjs.extend(relativeTime);
dayjs.extend(localizedFormat);
dayjs.extend(customParseFormat);
dayjs.extend(duration);

import('dayjs/locale/cs.js');
import('dayjs/locale/de.js');
import('dayjs/locale/en.js');
import('dayjs/locale/fr.js');
import('dayjs/locale/pt.js');
import('dayjs/locale/ru.js');
import('dayjs/locale/uk.js');

let timezone: string;
if (typeof process !== 'undefined') {
timezone = (process.env.TIMEZONE ?? 'system') === 'system' || !process.env.TIMEZONE ? dayjs.tz.guess() : process.env.TIMEZONE;
} else {
timezone = dayjs.tz.guess();
}
export const setLocale = (locale: string) => {
dayjs.locale(locale);
};

export { dayjs, timezone };
3 changes: 3 additions & 0 deletions src/helpers/locales.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { setLocale } from './dayjsHelper.js';

let _lang = 'en';

function setLang(lang: string) {
_lang = lang;
setLocale(lang);
}

function getLang() {
Expand Down
10 changes: 6 additions & 4 deletions src/services/twitch/eventSubLongPolling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { isAlreadyProcessed } from './eventsub/events.js';
import * as channelPoll from '~/helpers/api/channelPoll.js';
import * as channelPrediction from '~/helpers/api/channelPrediction.js';
import * as hypeTrain from '~/helpers/api/hypeTrain.js';
import { dayjs } from '~/helpers/dayjsHelper.js';
import { cheer } from '~/helpers/events/cheer.js';
import { follow } from '~/helpers/events/follow.js';
import { eventEmitter } from '~/helpers/events/index.js';
Expand Down Expand Up @@ -165,10 +166,11 @@ class EventSubLongPolling {
const createdBy = event.moderator_user_login;
const createdById = event.moderator_user_id;
const reason = event.reason;
const duration = event.ends_at ? new Date(event.ends_at) : null;
if (duration) {
timeout(`${ userName }#${ userId } by ${ createdBy }#${ createdById } for ${ duration } seconds`);
eventEmitter.emit('timeout', { userName, duration: duration.getTime() - Date.now() / 1000 });
const ends_at = event.ends_at ? dayjs(event.ends_at) : null;
if (ends_at) {
const duration = dayjs.duration(ends_at.diff(dayjs(event.banned_at)));
timeout(`${ userName }#${ userId } by ${ createdBy }#${ createdById } for ${ duration.asSeconds() } seconds`);
eventEmitter.emit('timeout', { userName, duration: duration.asSeconds() });
} else {
ban(`${ userName }#${ userId } by ${ createdBy }: ${ reason ? reason : '<no reason>' }`);
eventEmitter.emit('ban', { userName, reason: reason ? reason : '<no reason>' });
Expand Down
10 changes: 6 additions & 4 deletions src/services/twitch/eventSubWebsocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { isAlreadyProcessed } from './eventsub/events.js';
import * as channelPoll from '~/helpers/api/channelPoll.js';
import * as channelPrediction from '~/helpers/api/channelPrediction.js';
import * as hypeTrain from '~/helpers/api/hypeTrain.js';
import { dayjs } from '~/helpers/dayjsHelper.js';
import { isDebugEnabled } from '~/helpers/debug.js';
import { cheer } from '~/helpers/events/cheer.js';
import { follow } from '~/helpers/events/follow.js';
Expand Down Expand Up @@ -224,10 +225,11 @@ class EventSubWebsocket {
const createdBy = event.moderatorName;
const createdById = event.moderatorId;
const reason = event.reason;
const duration = event.endDate;
if (duration) {
timeout(`${ userName }#${ userId } by ${ createdBy }#${ createdById } for ${ duration } seconds`);
eventEmitter.emit('timeout', { userName, duration: duration.getTime() - Date.now() / 1000 });
const ends_at = dayjs(event.endDate);
if (ends_at) {
const duration = dayjs.duration(ends_at.diff(dayjs(event.startDate)));
timeout(`${ userName }#${ userId } by ${ createdBy }#${ createdById } for ${ duration.asSeconds() } seconds`);
eventEmitter.emit('timeout', { userName, duration: duration.asSeconds() });
} else {
ban(`${ userName }#${ userId } by ${ createdBy }: ${ reason ? reason : '<no reason>' }`);
eventEmitter.emit('ban', { userName, reason: reason ? reason : '<no reason>' });
Expand Down

0 comments on commit 55fbe25

Please sign in to comment.