Skip to content

Commit

Permalink
✔ Github Stats ~
Browse files Browse the repository at this point in the history
  • Loading branch information
bifeldy committed Oct 14, 2023
1 parent 159d05d commit 092088b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 20 deletions.
2 changes: 1 addition & 1 deletion dist/fansubid/server/main.js

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions src/api/services/cluster-master-slave.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ export class ClusterMasterSlaveService {
}
});

this.messages.on('CFG_GITHUB_SET', (obj, callback) => {
this.gs.log(`[MASTER_CFG_GITHUB_SET]`, obj);
try {
this.cfg.githubSet(obj);
callback({ error: null, data: null });
} catch (e) {
callback({ error: e, data: null });
}
});

this.messages.on('CFG_GITHUB_GET', (_, callback) => {
this.gs.log(`[MASTER_CFG_GITHUB_GET]`, _);
try {
Expand Down
2 changes: 1 addition & 1 deletion src/api/services/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class ConfigService {
return this.github;
}

githubSet(data): any {
githubSet(data): void {
this.github = data;
}

Expand Down
48 changes: 30 additions & 18 deletions src/api/services/discord.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ import { FansubMemberService } from '../repository/fansub-member.service';
import { UserService } from '../repository/user.service';
import { DdlFileService } from '../repository/ddl-file';
import { SocialMediaService } from '../repository/social-media.service';
import { ClusterMasterSlaveService } from './cluster-master-slave.service';

@Injectable()
export class DiscordService {

bot: Client = null;

constructor(
private cms: ClusterMasterSlaveService,
private api: ApiService,
private cfg: ConfigService,
private cs: CryptoService,
Expand All @@ -62,6 +64,16 @@ export class DiscordService {
}
}

async cfgGithubSet(data): Promise<void> {
if (cluster.isMaster) {
this.cfg.githubSet(data);
} else {
await this.cms.sendMessageToMaster('CFG_GITHUB_SET', data);
}
}

/** */

setupBot(): void {
this.bot = new Client({
restRequestTimeout: 60 * 1000,
Expand Down Expand Up @@ -101,7 +113,9 @@ export class DiscordService {
this.bot.on('ready', async () => {
try {
this.gs.log(`[DISCORD_SERVICE-READY] 🎉 ${this.bot.user.username}#${this.bot.user.discriminator} - ${this.bot.user.id} 🎶`);
await this.changeBotNickname();
if (cluster.isMaster) {
await this.changeBotNickname();
}
} catch (error) {
this.gs.log('[DISCORD_SERVICE-FAILED] 🎉', error, 'error');
}
Expand Down Expand Up @@ -285,26 +299,24 @@ export class DiscordService {
}

async changeBotNickname(): Promise<void> {
if (cluster.isMaster) {
try {
const url = new URL(`https://api.github.com/repos/${environment.author}/${environment.siteName}/commits`);
const res_raw = await this.api.getData(url, environment.nodeJsXhrHeader);
if (res_raw.ok) {
const gh: any = await res_raw.json();
this.cfg.githubSet(gh[0]);
const botGuild = this.bot ? this.bot.guilds.cache.get(environment.discord.guild_id) : null;
if (botGuild) {
const botMember = botGuild.members.cache.get(this.bot.user.id);
if (botMember) {
botMember.setNickname(`${environment.siteName} - ${this.cfg.githubGet()?.sha?.slice(0, 7)}`);
}
try {
const url = new URL(`https://api.github.com/repos/${environment.author}/${environment.siteName}/commits`);
const res_raw = await this.api.getData(url, environment.nodeJsXhrHeader);
if (res_raw.ok) {
const gh: any = await res_raw.json();
await this.cfgGithubSet(gh[0]);
const botGuild = this.bot ? this.bot.guilds.cache.get(environment.discord.guild_id) : null;
if (botGuild) {
const botMember = botGuild.members.cache.get(this.bot.user.id);
if (botMember) {
botMember.setNickname(`${environment.siteName} - ${this.cfg.githubGet()?.sha?.slice(0, 7)}`);
}
} else {
throw new Error('Github API Error!');
}
} catch (error) {
this.gs.log('[DISCORD_SERVICE-CHANGE_BOT_NICKNAME] 🎉', error, 'error');
} else {
throw new Error('Github API Error!');
}
} catch (error) {
this.gs.log('[DISCORD_SERVICE-CHANGE_BOT_NICKNAME] 🎉', error, 'error');
}
}

Expand Down

0 comments on commit 092088b

Please sign in to comment.