Skip to content

Commit 0f4da00

Browse files
committed
resolve conflicts (hopefully)
1 parent 6458eb5 commit 0f4da00

File tree

3 files changed

+112
-70
lines changed

3 files changed

+112
-70
lines changed

perftesting.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import Bun from "bun";
2+
3+
export enum PlaylistType {
4+
Video = "video",
5+
Short = "short",
6+
Stream = "stream",
7+
}
8+
9+
const playlistIdPrefixes: Record<PlaylistType, string> = {
10+
[PlaylistType.Video]: "UULF",
11+
[PlaylistType.Short]: "UUSH",
12+
[PlaylistType.Stream]: "UULV",
13+
};
14+
15+
export default async function test(
16+
channelId: string,
17+
playlistType?: PlaylistType,
18+
): Promise<string | null> {
19+
const playlistIdPrefix = !playlistType
20+
? "UU"
21+
: playlistIdPrefixes[playlistType];
22+
23+
if (!channelId.startsWith("UC")) {
24+
return null;
25+
}
26+
27+
// Measure performance of method 1
28+
const start1 = Bun.nanoseconds();
29+
const playlistId = playlistIdPrefix + channelId.slice(2);
30+
const end1 = Bun.nanoseconds();
31+
32+
// Measure performance of method 2
33+
const start2 = Bun.nanoseconds();
34+
const playlistId2 = channelId.replace("UC", playlistIdPrefix);
35+
const end2 = Bun.nanoseconds();
36+
37+
console.log(
38+
`Method 1 (slice): ${end1 - start1} ns, Method 2 (replace): ${end2 - start2} ns`
39+
);
40+
41+
return playlistId;
42+
}
43+
44+
test("UC1234567890", PlaylistType.Video);
45+

src/events/ready.ts

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,29 @@
1-
import { ActivityType, Events, PresenceUpdateStatus } from "discord.js";
1+
import { Events } from "discord.js";
2+
import { CronJob } from "cron";
23

34
import client from "../index";
4-
import fetchLatestUploads from "../utils/youtube/fetchLatestUploads";
55
import { config } from "../config";
6-
import { updateBotInfo } from "../utils/database";
7-
8-
// update the bot's presence
9-
async function updatePresence() {
10-
if (!client?.user) return;
11-
12-
const servers = client.guilds.cache.size;
13-
const members = client.guilds.cache.reduce(
14-
(acc, guild) => acc + guild.memberCount,
15-
0,
16-
);
17-
18-
await updateBotInfo(servers, members);
19-
client.user.setPresence({
20-
activities: [
21-
{
22-
name: `Notifying ${servers.toLocaleString()} servers [${members.toLocaleString()} members]`,
23-
type: ActivityType.Custom,
24-
},
25-
],
26-
status: PresenceUpdateStatus.Online,
27-
});
28-
}
6+
// import { checkIfStreamersAreLive } from "../utils/twitch/checkIfStreamerIsLive";
7+
import { cronUpdateBotInfo } from "../utils/cronJobs";
8+
import sendLatestUploads from "../utils/youtube/sendLatestUploads";
9+
import fetchLatestUploads from "../utils/youtube/fetchLatestUploads";
2910

3011
// Log into the bot
3112
client.once(Events.ClientReady, async (bot) => {
3213
console.log(`Ready! Logged in as ${bot.user?.tag}`);
3314

34-
// Set the bot's presence and update it every minute
35-
await updatePresence();
15+
await cronUpdateBotInfo();
16+
new CronJob("0 * * * * *", async () => {
17+
await cronUpdateBotInfo();
18+
}).start();
19+
3620
fetchLatestUploads();
37-
setInterval(updatePresence, 60000);
3821
setInterval(fetchLatestUploads, config.updateIntervalYouTube as number);
22+
23+
sendLatestUploads();
24+
setInterval(sendLatestUploads, config.updateIntervalYouTube as number);
25+
3926
// One at a time
4027
// checkIfStreamersAreLive();
4128
// setInterval(checkIfStreamersAreLive, config.updateIntervalTwitch as number);
42-
});
29+
});

src/utils/youtube/fetchLatestUploads.ts

Lines changed: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
import type { dbYouTube } from "../../types/database";
2-
3-
import { ChannelType, TextChannel } from "discord.js";
1+
import type { dbDiscordTable, dbYouTube } from "../../types/database";
42

53
import { env } from "../../config";
64
import { getGuildsTrackingChannel, updateVideoId } from "../database";
7-
import client from "../..";
85
import { dbYouTubeGetAllChannelsToTrack } from "../db/youtube";
96

107
import getChannelDetails from "./getChannelDetails";
118

9+
export const updates = new Map<
10+
string,
11+
{
12+
channelInfo: Awaited<ReturnType<typeof getChannelDetails>>;
13+
discordGuildsToUpdate: dbDiscordTable[];
14+
}
15+
>();
16+
1217
export default async function fetchLatestUploads() {
1318
console.log("Fetching latest uploads...");
1419

@@ -99,43 +104,48 @@ export default async function fetchLatestUploads() {
99104

100105
const channelInfo = await getChannelDetails(channelId);
101106

102-
console.log("Discord guilds to update:", discordGuildsToUpdate);
103-
for (const guild of discordGuildsToUpdate) {
104-
try {
105-
const channelObj = await client.channels.fetch(
106-
guild.guild_channel_id,
107-
);
108-
109-
if (
110-
!channelObj ||
111-
(channelObj.type !== ChannelType.GuildText &&
112-
channelObj.type !==
113-
ChannelType.GuildAnnouncement)
114-
) {
115-
console.error(
116-
"Invalid channel or not a text channel in fetchLatestUploads",
117-
);
118-
continue;
119-
}
120-
121-
await (channelObj as TextChannel).send({
122-
content:
123-
guild.guild_ping_role && channelInfo
124-
? `<@&${guild.guild_ping_role}> New video uploaded for ${channelInfo?.channelName}! https://www.youtube.com/watch?v=${videoId}`
125-
: guild.guild_ping_role
126-
? `<@&${guild.guild_ping_role}> New video uploaded! https://www.youtube.com/watch?v=${videoId}`
127-
: channelInfo
128-
? `New video uploaded for ${channelInfo.channelName}! https://www.youtube.com/watch?v=${videoId}`
129-
: `New video uploaded! https://www.youtube.com/watch?v=${videoId}`,
130-
});
131-
} catch (error) {
132-
console.error(
133-
"Error fetching or sending message to channel in fetchLatestUploads:",
134-
error,
135-
);
136-
}
137-
}
107+
updates.set(videoId, {
108+
channelInfo,
109+
discordGuildsToUpdate,
110+
});
111+
112+
// console.log("Discord guilds to update:", discordGuildsToUpdate);
113+
// for (const guild of discordGuildsToUpdate) {
114+
// try {
115+
// const channelObj = await client.channels.fetch(
116+
// guild.guild_channel_id,
117+
// );
118+
119+
// if (
120+
// !channelObj ||
121+
// (channelObj.type !== ChannelType.GuildText &&
122+
// channelObj.type !==
123+
// ChannelType.GuildAnnouncement)
124+
// ) {
125+
// console.error(
126+
// "Invalid channel or not a text channel in fetchLatestUploads",
127+
// );
128+
// continue;
129+
// }
130+
131+
// await (channelObj as TextChannel).send({
132+
// content:
133+
// guild.guild_ping_role && channelInfo
134+
// ? `<@&${guild.guild_ping_role}> New video uploaded for ${channelInfo?.channelName}! https://www.youtube.com/watch?v=${videoId}`
135+
// : guild.guild_ping_role
136+
// ? `<@&${guild.guild_ping_role}> New video uploaded! https://www.youtube.com/watch?v=${videoId}`
137+
// : channelInfo
138+
// ? `New video uploaded for ${channelInfo.channelName}! https://www.youtube.com/watch?v=${videoId}`
139+
// : `New video uploaded! https://www.youtube.com/watch?v=${videoId}`,
140+
// });
141+
// } catch (error) {
142+
// console.error(
143+
// "Error fetching or sending message to channel in fetchLatestUploads:",
144+
// error,
145+
// );
146+
// }
147+
// }
138148
}
139149
}
140150
}
141-
}
151+
}

0 commit comments

Comments
 (0)