Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Twir add-on #207

Merged
merged 9 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/twir/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export class Api extends FrankerFaceZ.utilities.module.Module {
constructor(...args) {
super(...args);

this.inject(Commands);

this.apiBase = 'https://twir.app/api/v1/api.';
}

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

if (response.ok) {
const json = await response.json();
return json;
}
} catch (err) {
this.log.error(err);
}

return null;
}
}

export class Commands extends FrankerFaceZ.utilities.module.Module {
// https://twir.app/api/v1/api.UnProtected/GetChannelCommands
getChannelCommands(channelId) {
return this.parent.request('UnProtected/GetChannelCommands', {
channelId,
});
}
}
147 changes: 147 additions & 0 deletions src/twir/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import { Api } from './api.js';

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

this.inject(Api);

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

this.roomCommands = new Map();

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',
}
});

this.loadBadges();
}

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

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

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

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

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

this.unloadBadges();
}

getTabCommands(event) {
for (const room of this.chat.iterateRooms()) {
if (room) {
const commands = this.getRoomCommands(room);
if (commands) {
event.commands.push(...commands);
}
}
}
}

async registerRoomCommands(room) {
const commandsResponse = await this.api.commands.getChannelCommands(room.id);
if (!commandsResponse) return;
this.roomCommands.set(room.id, commandsResponse.commands);
}

unregisterRoomCommands(room) {
this.roomCommands.delete(room.id);
}

getRoomCommands(room) {
const commands = this.roomCommands.get(room.id);
if (!commands) return;

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

return commands.map(command => {
const description = command.description || command.responses.join(' | ');

return {
prefix: '!',
name: command.name,
description: showCommandDescription ? description : '',
permissionLevel: 0,
ffz_group: `Twir (${command.group ?? command.module})`,
}
})
}

updateBadges(enabled) {
if (!enabled) {
this.unloadBadges();
} else {
this.loadBadges();
}
}

unloadBadges() {
this.badges.removeBadge('addon.twir.badge_contributor');
this.emit('chat:update-lines');
}

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

this.badges.loadBadgeData('addon.twir.badge_contributor', {
id: 'contributor',
name: 'Twir Contributor',
title: 'Twir Contributor',
click_url: 'https://twir.app',
image: 'https://twir.app/twir.svg',
slot: 100,
svg: true,
});

try {
const response = await fetch('https://raw.githubusercontent.com/twirapp/.github/main/contributors.json');
if (!response.ok) return;

const contributors = await response.json();
for (const contributor of contributors) {
const user = this.chat.getUser(contributor.id);
user.addBadge('addon.twir', 'addon.twir.badge_contributor');
}
} catch (err) {
this.log.error(err);
}

this.emit('chat:update-lines');
}
}

Twir.register();
Binary file added src/twir/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions src/twir/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"enabled": true,
"requires": [],
"version": "1.0.0",
"short_name": "Twir",
"name": "Twir",
"author": "crashmax",
"description": "Twir command suggestions and badges.",
"website": "https://twir.app",
"settings": "add_ons.twir",
"created": "2024-03-16T00:00:00.000Z",
"updated": "2024-03-16T00:00:00.000Z"
}
Loading