Skip to content

Commit ae9090b

Browse files
committed
feat(twitch): wow twitch is back
1 parent b883478 commit ae9090b

File tree

4 files changed

+44
-30
lines changed

4 files changed

+44
-30
lines changed

src/db/schema.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
// To make it easier to work with the database, disable prettier and some eslint rules for this file
22
/* eslint-disable no-inline-comments */
33
/* eslint-disable prettier/prettier */
4-
import { pgTable, serial, text, boolean, timestamp, decimal, unique, index, pgEnum, jsonb, integer } from "drizzle-orm/pg-core";
4+
import { sql } from "drizzle-orm";
5+
import { pgTable, serial, text, boolean, timestamp, decimal, unique, index, pgEnum, jsonb, integer, check } from "drizzle-orm/pg-core";
56

67
export const dbDiscordTable = pgTable("discord", {
78
guildId: text("guild_id").primaryKey(),
89
allowedPublicSharing: boolean("allowed_public_sharing").notNull().default(false),
910
feedrUpdatesChannelId: text("feedr_updates_channel_id"),
1011
isInServer: boolean("is_in_server").notNull().default(true),
1112
memberCount: integer("member_count").notNull().default(0),
12-
});
13+
isDm: boolean("is_dm").notNull().default(false),
14+
}, (table) => [
15+
check("discord_is_dm_constraint",
16+
sql`NOT ${table.isDm} OR (
17+
${table.allowedPublicSharing} = false AND
18+
${table.feedrUpdatesChannelId} = ${table.guildId} AND
19+
${table.isInServer} = true AND
20+
${table.memberCount} = 1
21+
)`
22+
)
23+
]);
1324

1425
export const dbBlueskyTable = pgTable("bluesky", {
1526
blueskyUserId: text("bluesky_user_id").primaryKey(),

src/db/twitch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,4 @@ export async function twitchUpdateIsLive(
8585

8686
return { success: false };
8787
}
88-
}
88+
}

src/events/ready.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { Events } from "discord.js";
2-
import { CronJob } from "cron";
2+
// import { CronJob } from "cron";
33

44
import client from "../index";
55
import { config } from "../config";
6-
// import { checkIfStreamersAreLive } from "../utils/twitch/checkIfStreamerIsLive";
7-
import { cronUpdateBotInfo } from "../utils/cronJobs";
6+
// import { cronUpdateBotInfo } from "../utils/cronJobs";
87
import sendLatestUploads from "../utils/youtube/sendLatestUploads";
98
import fetchLatestUploads from "../utils/youtube/fetchLatestUploads";
9+
import { checkIfStreamersAreLive } from "../utils/twitch/checkIfStreamerIsLive";
1010

1111
// Log into the bot
1212
client.once(Events.ClientReady, async (bot) => {
@@ -24,8 +24,6 @@ client.once(Events.ClientReady, async (bot) => {
2424
sendLatestUploads();
2525
setInterval(sendLatestUploads, config.updateIntervalYouTube as number);
2626

27-
// TODO: Twitch integration is not ready yet
28-
// One at a time
29-
// checkIfStreamersAreLive();
30-
// setInterval(checkIfStreamersAreLive, config.updateIntervalTwitch as number);
27+
checkIfStreamersAreLive();
28+
setInterval(checkIfStreamersAreLive, config.updateIntervalTwitch as number);
3129
});

src/utils/twitch/checkIfStreamerIsLive.ts

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ import type { TextChannel } from "discord.js";
22

33
import { env } from "../../config";
44
import client from "../..";
5+
import {
6+
dbTwitchGetAllChannelsToTrack,
7+
twitchUpdateIsLive,
8+
} from "../../db/twitch";
9+
import { discordGetAllGuildsTrackingChannel } from "../../db/discord";
10+
import { Platform } from "../../types/types.d";
511

612
import { twitchToken } from "./auth";
713
import { getStreamerName } from "./getStreamerName";
@@ -48,22 +54,19 @@ export async function checkIfStreamersAreLive(): Promise<void> {
4854
return;
4955
}
5056

51-
const allStreamerIds = await twitchGetAllChannelsToTrack();
57+
const allStreamerIds = await dbTwitchGetAllChannelsToTrack();
5258
const chunkSize = 100;
5359
const chunks = [];
5460

55-
for (let i = 0; i < allStreamerIds.length; i += chunkSize) {
56-
const chunk = allStreamerIds.slice(i, i + chunkSize);
61+
for (let i = 0; i < allStreamerIds.data.length; i += chunkSize) {
62+
const chunk = allStreamerIds.data.slice(i, i + chunkSize);
5763

5864
chunks.push(chunk);
5965
}
6066

6167
for (const chunk of chunks) {
6268
const urlQueries = chunk
63-
.map(
64-
(streamerId: dbTwitch) =>
65-
`user_id=${streamerId.twitch_channel_id}`,
66-
)
69+
.map((streamerId) => `user_id=${streamerId.twitchChannelId}`)
6770
.join("&");
6871
const res = await fetch(
6972
`https://api.twitch.tv/helix/streams?${urlQueries}`,
@@ -89,51 +92,53 @@ export async function checkIfStreamersAreLive(): Promise<void> {
8992

9093
for (const streamerId of chunk) {
9194
const isLive = allLiveStreamers.includes(
92-
streamerId.twitch_channel_id,
95+
streamerId.twitchChannelId,
9396
);
94-
const needsUpdate = isLive !== Boolean(streamerId.is_live);
97+
const needsUpdate =
98+
isLive !== Boolean(streamerId.twitchChannelIsLive);
9599

96100
console.log(
97-
`[Twitch] ${streamerId.twitch_channel_id} is live:`,
101+
`[Twitch] ${streamerId.twitchChannelId} is live:`,
98102
isLive,
99103
". Was live:",
100-
Boolean(streamerId.is_live),
104+
Boolean(streamerId.twitchChannelIsLive),
101105
". Needs update:",
102106
needsUpdate,
103107
);
104108

105109
if (needsUpdate) {
106110
// Update the database
107111
console.log(
108-
`Updating ${streamerId.twitch_channel_id} to be ${isLive ? "live" : "offline"}`,
112+
`Updating ${streamerId.twitchChannelId} to be ${isLive ? "live" : "offline"}`,
109113
);
110-
await twitchUpdateIsLive(streamerId.twitch_channel_id, isLive);
114+
await twitchUpdateIsLive(streamerId.twitchChannelId, isLive);
111115

112116
if (isLive) {
113117
// Get the streamer's name
114118
const streamerName = await getStreamerName(
115-
streamerId.twitch_channel_id,
119+
streamerId.twitchChannelId,
116120
);
117121

118122
// Get all guilds that are tracking this streamer
119123
const guildsTrackingStreamer =
120-
await twitchGetGuildsTrackingChannel(
121-
streamerId.twitch_channel_id,
124+
await discordGetAllGuildsTrackingChannel(
125+
Platform.Twitch,
126+
streamerId.twitchChannelId,
122127
);
123128

124-
for (const guild of guildsTrackingStreamer) {
129+
for (const guild of guildsTrackingStreamer.data) {
125130
// Send a message to the channel
126131
const channel = await client.channels.fetch(
127-
guild.guild_channel_id,
132+
guild.guildId,
128133
);
129134

130135
await (channel as TextChannel).send(
131-
`${guild.guild_ping_role ? `<@&${guild.guild_ping_role}>` : ""} ${streamerName} is now live <https://twitch.tv/${streamerName}>!`,
136+
`${guild.notificationRoleId ? `<@&${guild.notificationRoleId}>` : ""} ${streamerName} is now live <https://twitch.tv/${streamerName}>!`,
132137
);
133138
}
134139
} else {
135140
console.log(
136-
`[Twitch] ${streamerId.twitch_channel_id} is offline!`,
141+
`[Twitch] ${streamerId.twitchChannelId} is offline!`,
137142
);
138143
}
139144
}

0 commit comments

Comments
 (0)