Skip to content

Commit

Permalink
feat: badges
Browse files Browse the repository at this point in the history
  • Loading branch information
crashmax-dev committed Mar 17, 2024
1 parent 891b221 commit 0390544
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
63 changes: 60 additions & 3 deletions src/twir/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,32 @@ class Twir extends Addon {
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 >> Chat',
title: 'Command description',
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() {
Expand All @@ -33,6 +46,7 @@ class Twir extends Addon {
}

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

onDisable() {
Expand All @@ -43,7 +57,7 @@ class Twir extends Addon {
this.unregisterRoomCommands({ id: roomId });
}

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

getTabCommands(event) {
Expand Down Expand Up @@ -85,6 +99,49 @@ class Twir extends Addon {
}
})
}

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();
4 changes: 2 additions & 2 deletions src/twir/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
"enabled": true,
"requires": [],
"version": "0.0.1",
"short_name": "twir",
"short_name": "Twir",
"name": "Twir",
"author": "crashmax",
"description": "Twir command suggestions.",
"description": "Twir command suggestions and badges.",
"website": "https://twir.app",
"settings": "add_ons.twir",
"created": "2024-03-16T00:00:00.000Z",
Expand Down

0 comments on commit 0390544

Please sign in to comment.