-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.ts
300 lines (241 loc) · 9.24 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import { CronJob } from 'cron';
import dayjs from 'dayjs';
import isBetween from 'dayjs/plugin/isBetween';
import timezone from 'dayjs/plugin/timezone';
import utc from 'dayjs/plugin/utc';
import { BaseMessageOptions, Client, CommandInteraction, Events, GatewayIntentBits, Message, TextChannel } from 'discord.js';
import dotenv from 'dotenv';
dotenv.config();
dayjs.extend(isBetween);
dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.tz.setDefault('America/Bogota');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMessageReactions,
],
});
const LAUGH_EMOJIS = [
'e17451fcdbba5089cb76', // :rofl:
'af51e8d03e05a1c14355', // :joy:
'58a496c6d67a070ade5c', // :first_place:
'956966036354265180', // :pepehardlaugh:
'974777892418519081', // :doggokek:
'954075635310035024', // :kekw:
'956966037063106580', // :pepelaugh:
// Ensure all emoji IDs are valid strings
];
const BONE_EMOJI = ['🦴'];
const SCRAP_MESSAGES_COMMAND = 'gettop';
const MEME_OF_THE_YEAR_COMMAND = 'memeoftheyear';
// Discord Bot Login
client
.login(process.env.DISCORD_BOT_TOKEN)
.then(() => {
console.log('Bot logged in!');
})
.catch((err) => {
console.error('Failed to log in:', err);
});
client.once('ready', () => {
console.log('Bot is ready!');
// Schedule the command to run every Friday at 11:40 AM Bogota time
const job = new CronJob(
'40 11 * * 5', // cronTime
async () => { // onTick
console.log('Running scheduled gettop command...');
const guild = client.guilds.cache.first();
if (!guild) return;
const channel = guild.channels.cache.get(process.env.MEME_CHANNEL_ID as string) as TextChannel;
if (!channel) return;
try {
const fakeInteraction = {
reply: async (msg: string) => {
await channel.send(msg);
},
followUp: async (msg: string | BaseMessageOptions) => {
await channel.send(msg);
},
deferred: false,
replied: false,
editReply: async (msg: string) => {
await channel.send(msg);
},
isCommand: () => true,
commandName: SCRAP_MESSAGES_COMMAND,
} as unknown as CommandInteraction;
await processMessages(fakeInteraction);
} catch (error) {
console.error('Error in scheduled command:', error);
}
},
null, // onComplete
true, // start
'America/Bogota' // timeZone
);
job.start();
});
client.on(Events.InteractionCreate, async (interaction) => {
if (!interaction.isCommand()) return;
try {
if (interaction.commandName === SCRAP_MESSAGES_COMMAND) {
await processMessages(interaction);
} else if (interaction.commandName === MEME_OF_THE_YEAR_COMMAND) {
await interaction.deferReply();
const channel = interaction.channel as TextChannel;
const startDate = dayjs.tz('2024-01-01', 'America/Bogota').startOf('day');
const endDate = dayjs.tz('2024-12-31', 'America/Bogota').endOf('day');
const messages = await fetchMessagesInRange(channel, startDate, endDate);
const winners = getTopMessages(messages, LAUGH_EMOJIS);
await announceYearWinners(interaction, winners);
}
} catch (error) {
console.error('Error processing command:', error);
const errorMessage = 'There was an error processing your command.';
if (interaction.deferred) {
await interaction.editReply(errorMessage);
} else if (!interaction.replied) {
await interaction.reply(errorMessage);
}
}
});
async function processMessages(interaction: CommandInteraction): Promise<void> {
const channelId = process.env.MEME_CHANNEL_ID;
if (!channelId) {
await interaction.followUp('Channel ID is not set in the environment variables.');
return;
}
const channel = client.channels.cache.get(channelId) as TextChannel;
if (!channel) {
await interaction.followUp('Channel not found.');
return;
}
const now = dayjs().tz('America/Bogota');
const lastFriday = getLastFridayAtNoon();
const thisFriday = lastFriday.add(7, 'day');
const endDate = now.isBefore(thisFriday) ? now : thisFriday;
console.log(`Fetching messages from ${lastFriday.format()} to ${endDate.format()}`);
const allMessages = await fetchMessagesInRange(channel, lastFriday, endDate);
if (allMessages.length === 0) {
await interaction.followUp('No messages found in the specified date range.');
return;
}
const topMemes = getTopMessages(allMessages, LAUGH_EMOJIS);
const topBones = getTopMessages(allMessages, BONE_EMOJI);
await announceWinners(interaction, topMemes, 'meme');
await announceWinners(interaction, topBones, 'bone');
await interaction.followUp('Ganadores anunciados!');
}
function getLastFridayAtNoon(): dayjs.Dayjs {
const now = dayjs().tz('America/Bogota');
let lastFriday = now.day(5).hour(12).minute(0).second(0).millisecond(0); // 5 represents Friday
if (now.isBefore(lastFriday)) {
lastFriday = lastFriday.subtract(1, 'week');
}
return lastFriday;
}
async function fetchMessagesInRange(
channel: TextChannel,
startDate: dayjs.Dayjs,
endDate: dayjs.Dayjs
): Promise<Message[]> {
let messages: Message[] = [];
let lastMessageId: string | undefined;
let hasMoreMessages = true;
let iteration = 0;
while (hasMoreMessages) {
console.log(`Fetching messages, iteration ${iteration}`);
const options: { limit: number; before?: string } = { limit: 100 };
if (lastMessageId) options.before = lastMessageId;
const fetchedMessages = await channel.messages.fetch(options);
console.log(`Fetched ${fetchedMessages.size} messages`);
if (fetchedMessages.size === 0) {
hasMoreMessages = false;
break;
}
const filteredMessages = fetchedMessages.filter((msg) => {
const msgDate = dayjs(msg.createdAt);
return msgDate.isBetween(startDate, endDate, null, '[)');
});
console.log(`Filtered ${filteredMessages.size} messages in date range`);
messages.push(...filteredMessages.values());
lastMessageId = fetchedMessages.last()?.id;
const oldestMessageDate = dayjs(fetchedMessages.last()?.createdAt);
if (oldestMessageDate.isBefore(startDate)) {
console.log('Oldest message is before start date, breaking loop');
break;
}
iteration++;
}
console.log(`Total messages collected: ${messages.length}`);
return messages;
}
function getTopMessages(
messages: Message[],
reactionEmojis: string[]
): { message: Message; count: number }[] {
const messageReactionCounts = messages.map((message) => {
const count = message.reactions.cache.reduce((acc, reaction) => {
if (reactionEmojis.includes(reaction.emoji.name ?? '') || reactionEmojis.includes(reaction.emoji.id ?? '')) {
return acc + reaction.count;
}
return acc;
}, 0);
return { message, count };
});
const messagesWithReactions = messageReactionCounts.filter((item) => item.count > 0);
messagesWithReactions.sort((a, b) => b.count - a.count);
return messagesWithReactions.slice(0, 3);
}
async function announceWinners(
interaction: CommandInteraction,
winners: { message: Message; count: number }[],
contestType: string
): Promise<void> {
if (winners.length === 0) {
await interaction.followUp(`No winners found for ${contestType}.`);
return;
}
const emoji = contestType === 'meme' ? '🎉' : '🦴';
const contestName = contestType === 'meme' ? 'Meme de la semana' : 'Hueso de la semana';
let messageContent = `${emoji} **Ganadores del "${contestName}"** ${emoji}\n\n`;
const attachments: { attachment: string; name: string }[] = [];
for (const [index, winnerData] of winners.entries()) {
const { message, count } = winnerData;
const winnerLink = message.url;
const line = `**#${index + 1}** - Felicitaciones, ${message.author}! Tu post ha ganado con ${count} reacciones. [Ver mensaje](${winnerLink})`;
messageContent += line + '\n';
const attachment = message.attachments.first();
if (attachment) {
attachments.push({ attachment: attachment.url, name: attachment.name });
}
}
const messageOptions: BaseMessageOptions = { content: messageContent };
if (attachments.length > 0) {
messageOptions.files = attachments.map((a) => a.attachment);
}
await interaction.followUp(messageOptions);
}
async function announceYearWinners(
interaction: CommandInteraction,
winners: { message: Message; count: number }[]
): Promise<void> {
if (winners.length === 0) {
await interaction.followUp('No se encontraron memes para el año 2024 😢');
return;
}
let messageContent = `🏆 **LOS MEJORES MEMES DEL 2024** 🏆\n\n`;
for (const [index, winnerData] of winners.entries()) {
const { message, count } = winnerData;
const medal = index === 0 ? '👑' : index === 1 ? '🥈' : '🥉';
const winnerLink = message.url;
const line = `${medal} **${index + 1}° Lugar** - ¡Felicitaciones ${message.author}! Tu meme alcanzó ${count} reacciones\n${winnerLink}\n`;
messageContent += line + '\n';
}
messageContent += '¡Gracias a todos por otro año lleno de risas! 🎉';
const messageOptions: BaseMessageOptions = { content: messageContent };
await interaction.followUp(messageOptions);
}