Skip to content

Commit

Permalink
✔ Discord Master Handle Message ~
Browse files Browse the repository at this point in the history
  • Loading branch information
bifeldy committed Oct 14, 2023
1 parent 2132f0c commit 900db65
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
2 changes: 1 addition & 1 deletion dist/fansubid/browser/ngsw.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"configVersion": 1,
"timestamp": 1697259411691,
"timestamp": 1697267113397,
"index": "/index.html",
"assetGroups": [
{
Expand Down
2 changes: 1 addition & 1 deletion dist/fansubid/server/main.js

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion src/api/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { ApiKeyService } from './repository/api-key.service';
import { GlobalService } from './services/global.service';
import { SocketIoService } from './services/socket-io.service';
import { ClusterMasterSlaveService } from './services/cluster-master-slave.service';
import { DiscordService } from './services/discord.service';

export async function ctx(): Promise<INestApplicationContext> {
return await NestFactory.createApplicationContext(AppModule);
Expand Down Expand Up @@ -107,7 +108,9 @@ if (moduleFilename === __filename || moduleFilename.includes('iisnode')) {
if (numCPUs > 1) {
try {
if (cluster.isMaster) {
(await ctx()).get(ClusterMasterSlaveService).masterHandleMessages();
const nestCtx = await ctx();
nestCtx.get(ClusterMasterSlaveService).masterHandleMessages();
await nestCtx.get(DiscordService).startBot();
gs.log('[APP_MASTER_PID] 💻', process.pid);
setupMaster(nestApp.getHttpServer(), {
loadBalancingMethod: 'least-connection'
Expand Down
20 changes: 16 additions & 4 deletions src/api/services/discord.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,14 @@ export class DiscordService {
private sosmedRepo: SocialMediaService
) {
if (environment.production) {
this.startBot();
this.setupBot();
if (cluster.isWorker) {
this.startBot();
}
}
}

startBot(): void {
setupBot(): void {
this.bot = new Client({
restRequestTimeout: 60 * 1000,
intents: [
Expand Down Expand Up @@ -90,7 +93,9 @@ export class DiscordService {
this.bot.on('messageCreate', (msg: Message) => {
if (msg.channel.id === environment.discord.channelBotId && msg.content.startsWith('~')) {
this.gs.log(`[${msg.guild.name}] 🎉 [${(msg.channel as TextChannel).name}] [${msg.author.username}#${msg.author.discriminator}] ${msg.content} 🎶`);
this.handleMessage(msg);
if (cluster.isMaster) {
this.handleMessage(msg);
}
}
});
this.bot.on('ready', async () => {
Expand All @@ -105,7 +110,14 @@ export class DiscordService {
this.gs.log(`[DISCORD_SERVICE-MEMBER_LEAVE] 🎉 ${member.user.username}#${member.user.discriminator} - ${member.user.id} 🎶`);
this.memberLeftRemoveVerifiedDemote(member);
});
this.bot.login(environment.discord.loginToken).catch(err => this.gs.log('[DISCORD_SERVICE-LOGIN] 🎉', err, 'error'));
}

async startBot(): Promise<void> {
try {
await this.bot.login(environment.discord.loginToken);
} catch (error) {
this.gs.log('[DISCORD_SERVICE-LOGIN] 🎉', error, 'error');
}
}

async sendNews(message: MessageOptions): Promise<void> {
Expand Down

0 comments on commit 900db65

Please sign in to comment.