Skip to content

Commit

Permalink
Use only tmi to manage twitch chat messages
Browse files Browse the repository at this point in the history
  • Loading branch information
lucianoratamero committed Aug 23, 2024
1 parent d521645 commit 77837df
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
3 changes: 1 addition & 2 deletions src/routes/twitch-chat/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@
return;
}
const { ComfyJS, client } = init({ channel, twitch_id, message_screen_time });
const { client } = init({ channel, twitch_id, message_screen_time });
return () => {
ComfyJS.Disconnect();
client.disconnect();
};
});
Expand Down
32 changes: 19 additions & 13 deletions src/routes/twitch-chat/lib.svelte.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ComfyJS, { type OnMessageExtra } from 'comfy.js';
import { type OnMessageExtra } from 'comfy.js';
// @ts-expect-error
import tmi from 'tmi.js';

Expand Down Expand Up @@ -138,8 +138,6 @@ export function init({
throw err;
});

ComfyJS.Init(channel);

//chat moderation tools
const client = new tmi.Client({
connection: {
Expand All @@ -150,6 +148,10 @@ export function init({
});
client.connect();

client.on('message', (_channel: string, tags: any, message: string, _self: boolean) => {
processPronouns(message, tags['username'], tags);
});

client.on('clearchat', () => {
console.log('chat cleared');
collection.messages = [];
Expand Down Expand Up @@ -200,18 +202,18 @@ export function init({
});

//API calling

ComfyJS.onChat = (user, message, _flags, _self, extra) => {
processPronouns(message, user, extra);
};

return {
ComfyJS,
client
};
}

export function processPronouns(message: string, user: string, extra: OnMessageExtra) {
export function processPronouns(
message: string,
user: string,
extra: OnMessageExtra & {
badges: Record<string, any>;
}
) {
let pronounFilter = pronounCache.filter(function (x: { name: string }) {
return x.name == user;
});
Expand All @@ -237,7 +239,11 @@ export function processPronouns(message: string, user: string, extra: OnMessageE
}
}

function processMessage(message: string, user: string, extra: OnMessageExtra) {
function processMessage(
message: string,
user: string,
extra: OnMessageExtra & { badges: Record<string, any> }
) {
// Looks for popular chatbots and filters them out
if (
user === 'StreamElements' ||
Expand Down Expand Up @@ -269,10 +275,10 @@ function processMessage(message: string, user: string, extra: OnMessageExtra) {
}
}
// Adds badges to the name if enabled in config.js--
if (!extra.userBadges || Object.keys(extra.userBadges).length === 0) {
if (!extra.badges || Object.keys(extra.badges).length === 0) {
console.log('No user badge');
} else {
let values = Object.keys(extra.userBadges);
let values = Object.keys(extra.badges);
// use Twitch API with badgesList, to make things future prove
for (let i = values.length; i >= 0; i--) {
let badgeID = values[i];
Expand Down

0 comments on commit 77837df

Please sign in to comment.