diff --git a/src/twir/api.js b/src/twir/api.js index c170e056..f52e977e 100644 --- a/src/twir/api.js +++ b/src/twir/api.js @@ -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(); } diff --git a/src/twir/badges.js b/src/twir/badges.js index 38fb5b01..d0227872 100644 --- a/src/twir/badges.js +++ b/src/twir/badges.js @@ -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) { @@ -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 { @@ -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; diff --git a/src/twir/commands.js b/src/twir/commands.js index 42da5423..0e2e79c3 100644 --- a/src/twir/commands.js +++ b/src/twir/commands.js @@ -1,3 +1,5 @@ +import { SETTING_KEYS } from './settings.js'; + export class Commands extends FrankerFaceZ.utilities.module.Module { constructor(...args) { super(...args); @@ -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) { @@ -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(' | '); diff --git a/src/twir/index.js b/src/twir/index.js index 0de9b1c7..84348d83 100644 --- a/src/twir/index.js +++ b/src/twir/index.js @@ -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); } } diff --git a/src/twir/settings.js b/src/twir/settings.js new file mode 100644 index 00000000..dc31b59e --- /dev/null +++ b/src/twir/settings.js @@ -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); + }); + } +}