-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.ts
116 lines (94 loc) · 3.73 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
import { GuildMember, Message, Client, Intents } from "discord.js";
import * as dotenv from "dotenv";
import { CronJob } from "cron";
import SendWelcomeMessageUseCase from "./application/usecases/sendWelcomeMessageUseCase";
import FileMessageRepository from "./infrastructure/repository/fileMessageRepository";
import ChatService from "./domain/service/chatService";
import DiscordChatService from "./infrastructure/service/discordChatService";
import ConsoleLoggerService from "./infrastructure/service/consoleLoggerService";
import MessageRepository from "./domain/repository/messageRepository";
import LoggerService from "./domain/service/loggerService";
import CommandUseCaseResolver from "./domain/service/commandUseCaseResolver";
import ChannelResolver from "./domain/service/channelResolver";
import KataService from "./domain/service/kataService/kataService";
import CodewarsKataService from "./infrastructure/service/codewarsKataService";
import ContentAggregatorService from "./domain/service/contentAggregatorService/contentAggregatorService";
import LemmyContentAggregatorService from "./infrastructure/service/lemmyContentAggregatorService";
dotenv.config();
const { DISCORD_TOKEN } = process.env;
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MEMBERS, Intents.FLAGS.GUILD_MESSAGES],
});
const messageRepository: MessageRepository = new FileMessageRepository();
const chatService: ChatService = new DiscordChatService(client);
const loggerService: LoggerService = new ConsoleLoggerService();
const channelResolver: ChannelResolver = new ChannelResolver();
const kataService: KataService = new CodewarsKataService();
const lemmyContentAggregatorService: ContentAggregatorService = new LemmyContentAggregatorService();
const useCaseResolver = new CommandUseCaseResolver({
messageRepository,
chatService,
loggerService,
channelResolver,
kataService,
});
const checkForNewPosts = async () => {
loggerService.log("Checking for new posts on content aggregator...");
const FEED_CHANNEL_ID = "829694016156205056";
const lastPosts = await lemmyContentAggregatorService.fetchLastPosts();
const now = new Date();
const last5MinutesPosts = lastPosts.filter((post) => {
const diff = Math.abs(now.getTime() - post.getCreatedAt().getTime());
const minutes = Math.floor(diff / 1000 / 60);
return minutes <= 5;
});
const avoidEmbedInLink = (str: string): string => {
// convert links to avoid embed (by wrapping them in <>)
const regex = /\[([^\]]+)\]\((https?:\/\/[^)]+)\)/g;
return str.replace(regex, "[$1](<$2>)");
};
last5MinutesPosts.forEach((post) => {
const title = avoidEmbedInLink(post.getTitle());
const message = `Novo post no Lemmy: **${title}** (*${post?.getAuthorName()}*)
👉 Ver em: ${post?.getLink()}
`;
chatService.sendMessageToChannel(message, FEED_CHANNEL_ID);
});
loggerService.log(`Published ${last5MinutesPosts.length} new posts!`);
};
const setupCron = () => {
/* eslint-disable no-new */
new CronJob(
// run every 5 minutes
"0 */5 * * * *",
checkForNewPosts,
null,
true,
"UTC"
);
};
client.once("ready", () => {
loggerService.log("Ready!");
setupCron();
});
client.login(DISCORD_TOKEN);
client.on("guildMemberAdd", (member: GuildMember) =>
new SendWelcomeMessageUseCase({
messageRepository,
chatService,
loggerService,
channelResolver,
}).execute(member)
);
client.on("messageCreate", (messages: Message) => {
const COMMAND_PREFIX = "!";
if (!messages.content.startsWith(COMMAND_PREFIX)) return;
const command = messages.content.split(" ")[0];
try {
useCaseResolver.resolveByCommand(command, {
channelId: messages.channel.id,
});
} catch (error: unknown) {
loggerService.log(error);
}
});