Skip to content

Commit

Permalink
twitchemotes api changes
Browse files Browse the repository at this point in the history
clearchat bugfix
  • Loading branch information
night committed Jul 10, 2017
1 parent 5b2207a commit 16b793a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "BetterTTV",
"author": "Night",
"version": "7.0.17",
"version": "7.0.18",
"description": "BetterTTV enhances Twitch with new features, bug fixes, and reduced clutter.",
"main": "betterttv.js",
"scripts": {
Expand Down
46 changes: 27 additions & 19 deletions src/modules/channel_emotes_tip/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,57 @@ const html = require('../../utils/html');

class ChannelEmotesTipModule {
constructor() {
this.sets = {};
this.ids = {};
this.ids = null;

this.updateEmoteLists();
watcher.on('load.chat', () => this.checkEmoteSets());
}

updateEmoteLists() {
api.get('emotes/sets')
.then(({sets}) => {
this.sets = sets;
});

api.get('emotes/ids')
api.get('twitch_emotes/ids')
.then(({ids}) => {
this.ids = ids;
});
})
.then(() => this.checkEmoteSets());
}

getEmote(id, code) {
const channel = this.ids[id];
if (!this.ids) return null;

const name = this.ids[id];
if (!name) return null;

return {
channel,
channelURL: `https://www.twitch.tv/${channel}`,
id,
code,
channel: {
name,
url: `https://www.twitch.tv/${name}`
},
balloon: `
${html.escape(code)}<br>
${channel ? `Channel: ${html.escape(channel)}` : ''}
${name ? `Channel: ${html.escape(name)}` : ''}
`
};
}

checkEmoteSets() {
if (!this.ids) return null;

const room = twitch.getCurrentChat();
if (!room || !room.product || !Array.isArray(room.product.emoticons)) return;
if (!room || !room.product) return;

const {emoticons, name} = room.product;
if (!Array.isArray(emoticons) || !name) return;

const activeEmote = room.product.emoticons.find(emote => emote.state === 'active');
if (!activeEmote || this.sets[activeEmote.emoticon_set]) {
debug.log(`Channel set exists for ${room.product.name}, no need to give a tip`);
const emote = emoticons.find(({state}) => state === 'active');
if (!emote || this.ids[emote.id]) {
debug.log(`Channel set exists for ${name}, no need to give a tip`);
return;
}

api.post(`emotes/channel_tip/${room.product.name}`)
.then(() => debug.log(`Gave a channel tip for ${room.product.name}`));
api.post('twitch_emotes/channel_tip', {body: {name}})
.then(() => debug.log(`Gave a channel tip for ${name}`));
}
}

Expand Down
11 changes: 6 additions & 5 deletions src/modules/chat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class ChatModule {
}

messageReplacer($message, user) {
const currentChannel = twitch.getCurrentChannel();
const tokens = $message.contents();
for (let i = 0; i < tokens.length; i++) {
const node = tokens[i];
Expand All @@ -129,11 +130,11 @@ class ChatModule {
if (!$image) continue;
const code = $image.attr('alt');
const id = ($image.attr('src').split('emoticons/v1/')[1] || '').split('/')[0];
const emoteChannel = channelEmotesTip.getEmote(id, code);
if (emoteChannel.channel) {
$emote.find('.balloon').css('text-align', 'center').html(emoteChannel.balloon);
if (emoteChannel.channelURL === window.location.href) continue;
$emote.on('click', () => window.open(emoteChannel.channelURL, '_blank'));
const emote = channelEmotesTip.getEmote(id, code);
if (emote) {
$emote.find('.balloon').css('text-align', 'center').html(emote.balloon);
if (!currentChannel || emote.channel.name === currentChannel.name) continue;
$emote.on('click', () => window.open(emote.channel.url, '_blank'));
}
continue;
}
Expand Down
9 changes: 5 additions & 4 deletions src/modules/chat_deleted_messages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,11 @@ class ChatDeletedMessagesModule {
const tmiRoom = currentChat.tmiRoom;
const clearChatCallbacks = tmiRoom._events.clearchat;
if (!clearChatCallbacks || !clearChatCallbacks.length) return;
const defaultClearChatCallback = clearChatCallbacks.find(c => c && c !== onClearChat);
if (defaultClearChatCallback && !tmiRoom._bttvClearChatMonkeyPatched) {
twitchClearChat = defaultClearChatCallback;
}
if (clearChatCallbacks.find(c => c && c === onClearChat)) return;
const defaultClearChatCallback = clearChatCallbacks.find(c => typeof c === 'function' && c !== onClearChat);
if (!defaultClearChatCallback || tmiRoom._bttvClearChatMonkeyPatched) return;

twitchClearChat = defaultClearChatCallback;
delete tmiRoom._events.clearchat;
tmiRoom.on('clearchat', onClearChat);
tmiRoom._bttvClearChatMonkeyPatched = true;
Expand Down

0 comments on commit 16b793a

Please sign in to comment.