Skip to content

Commit

Permalink
chore: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
crashmax-dev committed Apr 14, 2024
1 parent f250b8a commit a01b225
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 69 deletions.
6 changes: 1 addition & 5 deletions src/twir/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ export class Api extends FrankerFaceZ.utilities.module.Module {

async request(path) {
try {
const response = await fetch(`${this.apiBase}/${path}`, {
method: 'GET',
headers: { 'Content-Type': 'application/json' },
});

const response = await fetch(`${this.apiBase}/${path}`);
if (response.ok) {
return await response.json();
}
Expand Down
18 changes: 8 additions & 10 deletions src/twir/badges.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// https://twitch.tv/twirapp
const TWIR_APP_ID = 870280719;
import { TWIR_APP_ID } from './settings.js';

export class Badges extends FrankerFaceZ.utilities.module.Module {
constructor(...args) {
Expand All @@ -11,10 +10,15 @@ export class Badges extends FrankerFaceZ.utilities.module.Module {

this.badgeIds = new Set();

this.updateBadges = this.updateBadges.bind(this);
// twitchbot badge for TwirApp
this.chat.getUser(TWIR_APP_ID).addBadge('ffz', 2);
}

onDisable() {
this.unloadBadges();
}

updateBadges(enabled) {
updateSettingBadges(enabled) {
if (enabled) {
this.loadBadges();
} else {
Expand All @@ -31,12 +35,6 @@ export class Badges extends FrankerFaceZ.utilities.module.Module {
}

async loadBadges() {
const showUserBadges = this.settings.get('addon.twir.user_badges');
if (!showUserBadges) return;

// twitchbot badge for TwirApp
this.chat.getUser(TWIR_APP_ID).addBadge('ffz', 2);

const badges = await this.parent.api.badges.getBadges();
for (const badge of badges) {
if (!badge.users.length) return;
Expand Down
30 changes: 26 additions & 4 deletions src/twir/commands.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { SETTING_KEYS } from './settings.js';

export class Commands extends FrankerFaceZ.utilities.module.Module {
constructor(...args) {
super(...args);
Expand All @@ -6,10 +8,30 @@ export class Commands extends FrankerFaceZ.utilities.module.Module {
this.inject('settings');

this.roomCommands = new Map();
}

onEnable() {
this.on('chat:room-add', this.registerRoomCommands, this);
this.on('chat:room-remove', this.unregisterRoomCommands, this);

for (const room of this.chat.iterateRooms()) {
if (room) {
this.registerRoomCommands(room);
}
}

this.on('chat:get-tab-commands', this.getTabCommands, this);
}

onDisable() {
this.off('chat:room-add', this.registerRoomCommands, this);
this.off('chat:room-remove', this.unregisterRoomCommands, this);

for (const roomId of this.roomCommands.keys()) {
this.unregisterRoomCommands({ id: roomId });
}

this.getTabCommands = this.getTabCommands.bind(this);
this.registerRoomCommands = this.registerRoomCommands.bind(this);
this.unregisterRoomCommands = this.unregisterRoomCommands.bind(this);
this.off('chat:get-tab-commands', this.getTabCommands, this);
}

getTabCommands(event) {
Expand All @@ -36,7 +58,7 @@ export class Commands extends FrankerFaceZ.utilities.module.Module {
const commands = this.roomCommands.get(room.id);
if (!commands) return;

const showCommandDescription = this.settings.get('addon.twir.command_description');
const showCommandDescription = this.settings.get(SETTING_KEYS.commandDescription);

return commands.map(command => {
const description = command.description || command.responses?.map(response => response.text).join(' | ');
Expand Down
54 changes: 4 additions & 50 deletions src/twir/index.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,16 @@
import { Api } from './api.js';
import { Badges } from './badges.js';
import { Commands } from './commands.js';
import { Badges } from './badges.js';
import { Settings } from './settings.js';

class Twir extends Addon {
constructor(...args) {
super(...args);

this.inject(Api);
this.inject(Badges);
this.inject(Commands);

this.inject('chat');
this.inject('settings');

this.settings.add('addon.twir.command_description', {
default: true,
ui: {
path: 'Add-Ons > Twir >> Commands',
title: 'Description',
description: 'Show command description or responses.',
component: 'setting-check-box',
}
});

this.settings.add('addon.twir.user_badges', {
default: true,
ui: {
path: 'Add-Ons > Twir >> User Cosmetics',
title: 'Badges',
description: 'Show user badges.\n\n(Per-badge visibilty can be set in [Chat >> Badges > Visibilty > Add-Ons](~chat.badges.tabs.visibility))',
component: 'setting-check-box',
}
});
}

onEnable() {
this.on('chat:room-add', this.commands.registerRoomCommands);
this.on('chat:room-remove', this.commands.unregisterRoomCommands);

for (const room of this.chat.iterateRooms()) {
if (room) {
this.commands.registerRoomCommands(room);
}
}

this.on('chat:get-tab-commands', this.commands.getTabCommands);
this.settings.getChanges('addon.twir.user_badges', this.badges.updateBadges);
}

onDisable() {
this.off('chat:room-add', this.commands.registerRoomCommands);
this.off('chat:room-remove', this.commands.unregisterRoomCommands);

for (const roomId of this.roomCommands.keys()) {
this.commands.unregisterRoomCommands({ id: roomId });
}

this.badges.unloadBadges();
this.inject(Badges);
this.inject(Settings);
}
}

Expand Down
39 changes: 39 additions & 0 deletions src/twir/settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
export const SETTING_KEYS = {
commandDescription: 'addon.twir.command_description',
userBadges: 'addon.twir.user_badges',
};

// https://twitch.tv/twirapp
export const TWIR_APP_ID = 870280719;

export class Settings extends FrankerFaceZ.utilities.module.Module {
constructor(...args) {
super(...args);

this.inject('settings');

this.settings.add(SETTING_KEYS.commandDescription, {
default: true,
ui: {
path: 'Add-Ons > Twir >> Commands',
title: 'Description',
description: 'Show command description or responses.',
component: 'setting-check-box',
}
});

this.settings.add(SETTING_KEYS.userBadges, {
default: true,
ui: {
path: 'Add-Ons > Twir >> User Cosmetics',
title: 'Badges',
description: 'Show user badges.\n\n(Per-badge visibilty can be set in [Chat >> Badges > Visibilty > Add-Ons](~chat.badges.tabs.visibility))',
component: 'setting-check-box',
}
});

this.settings.getChanges(SETTING_KEYS.userBadges, enabled => {
this.parent.badges.updateSettingBadges(enabled);
});
}
}

0 comments on commit a01b225

Please sign in to comment.