Skip to content

Commit

Permalink
fix: Check whether guild is available (#383)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikuroXina authored Aug 6, 2022
1 parent d672c08 commit 61a1466
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
15 changes: 9 additions & 6 deletions src/adaptor/discord/member-stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,25 @@ export class DiscordMemberStats

async allMemberCount(): Promise<number> {
const guild = await this.client.guilds.fetch(this.guildId);
if (!guild) {
throw new Error('guild not found');
if (!guild.available) {
throw new Error('guild unavailable');
}
return guild.memberCount;
}

async botMemberCount(): Promise<number> {
const guild = await this.client.guilds.fetch(this.guildId);
if (!guild) {
throw new Error('guild not found');
if (!guild.available) {
throw new Error('guild unavailable');
}
const guildMemberList = await guild.members.list({ limit: 1000 });
return guildMemberList.filter((member) => member.user.bot).size;
}

async fetchMembersWithRole(): Promise<MemberWithRole[]> {
const guild = await this.client.guilds.fetch(this.guildId);
if (!guild) {
throw new Error('guild not found');
if (!guild.available) {
throw new Error('guild unavailable');
}
const guildMemberList = await guild.members.list({ limit: 1000 });
return guildMemberList.map(({ displayName, roles }) => ({
Expand All @@ -49,6 +49,9 @@ export class DiscordMemberStats

async fetchStats(userId: string): Promise<UserStats | null> {
const guild = await this.client.guilds.fetch(this.guildId);
if (!guild.available) {
throw new Error('guild unavailable');
}
const member = await guild.members.fetch(userId);
if (!member) {
return null;
Expand Down
9 changes: 9 additions & 0 deletions src/adaptor/discord/role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export class DiscordRoleManager implements RoleManager, RoleStatsRepository {

async addRole(targetMember: Snowflake, newRoleId: Snowflake): Promise<void> {
const guild = await this.client.guilds.fetch(this.guildId);
if (!guild.available) {
throw new Error('guild unavailable');
}
const member = await guild.members.fetch(targetMember);
await member.roles.add(newRoleId);
}
Expand All @@ -24,12 +27,18 @@ export class DiscordRoleManager implements RoleManager, RoleStatsRepository {
removingRoleId: Snowflake
): Promise<void> {
const guild = await this.client.guilds.fetch(this.guildId);
if (!guild.available) {
throw new Error('guild unavailable');
}
const member = await guild.members.fetch(targetMember);
await member.roles.remove(removingRoleId);
}

async fetchStats(roleId: string): Promise<RoleStats | null> {
const guild = await this.client.guilds.fetch(this.guildId);
if (!guild.available) {
throw new Error('guild unavailable');
}
const role = await guild.roles.fetch(roleId);
if (!role) {
return null;
Expand Down
7 changes: 4 additions & 3 deletions src/adaptor/discord/voice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ export class DiscordVoiceConnectionFactory<K extends string | number | symbol>
guildId: Snowflake,
roomId: Snowflake
): Promise<VoiceConnection<K>> {
const guild =
this.client.guilds.cache.get(guildId) ||
(await this.client.guilds.fetch(guildId));
const guild = await this.client.guilds.fetch(guildId);
if (!guild.available) {
throw new Error('guild unavailable');
}
const channel =
guild.channels.cache.get(roomId) || (await guild.channels.fetch(roomId));
if (!channel) {
Expand Down

0 comments on commit 61a1466

Please sign in to comment.