Skip to content

Commit

Permalink
feat(vanity): Cache urls to reduce requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Crespi committed Mar 30, 2020
1 parent 654e993 commit 55d0560
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 deletions.
3 changes: 3 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { InviteCodeSettingsCache } from './invites/cache/InviteCodeSettingsCache
import { InvitesCache } from './invites/cache/InvitesCache';
import { LeaderboardCache } from './invites/cache/LeaderboardCache';
import { RanksCache } from './invites/cache/RanksCache';
import { VanityUrlCache } from './invites/cache/VanityUrlCache';
import { CaptchaService } from './invites/services/Captcha';
import { InvitesService } from './invites/services/Invites';
import { TrackingService } from './invites/services/Tracking';
Expand Down Expand Up @@ -68,6 +69,7 @@ export interface ClientCacheObject {

inviteCodes: InviteCodeSettingsCache;
invites: InvitesCache;
vanity: VanityUrlCache;
leaderboard: LeaderboardCache;
ranks: RanksCache;
members: MemberSettingsCache;
Expand Down Expand Up @@ -201,6 +203,7 @@ export class IMClient extends Client {
this.cache = {
inviteCodes: new InviteCodeSettingsCache(this),
invites: new InvitesCache(this),
vanity: new VanityUrlCache(this),
leaderboard: new LeaderboardCache(this),
ranks: new RanksCache(this),
members: new MemberSettingsCache(this),
Expand Down
17 changes: 17 additions & 0 deletions src/invites/cache/VanityUrlCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Cache } from '../../framework/cache/Cache';

export class VanityUrlCache extends Cache<string> {
public async init() {
// NO-OP
}

protected async _get(guildId: string): Promise<string> {
return (
this.client.guilds.get(guildId)?.vanityURL ||
this.client
.getGuildVanity(guildId)
.then((r) => r.code as any)
.catch(() => null)
);
}
}
9 changes: 1 addition & 8 deletions src/invites/commands/invites/inviteCodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,7 @@ export default class extends Command {
isWidget: !code.inviter
}));

const vanityInv =
guild.vanityURL ||
(await guild
.getVanity()
.then((r) => {
return r.code;
})
.catch(() => undefined));
const vanityInv = await this.client.cache.vanity.get(guild.id);
if (vanityInv) {
newDbCodes.push({
code: vanityInv,
Expand Down
20 changes: 3 additions & 17 deletions src/invites/services/Tracking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,17 +319,10 @@ export class TrackingService extends IMService {

let isVanity = false;
if (inviteCodesUsed.length === 0) {
const vanityInv =
guild.vanityURL ||
(await guild
.getVanity()
.then((r) => {
return r.code;
})
.catch(() => undefined));
const vanityInv = await this.client.cache.vanity.get(guild.id);
if (vanityInv) {
isVanity = true;
inviteCodesUsed.push(vanityInv as string);
inviteCodesUsed.push(vanityInv);
invs.push({
code: vanityInv,
channel: null,
Expand Down Expand Up @@ -763,14 +756,7 @@ export class TrackingService extends IMService {
// Collect concurrent promises
const promises: any[] = [];

const vanityInv =
guild.vanityURL ||
(await guild
.getVanity()
.then((r) => {
return r.code;
})
.catch(() => undefined));
const vanityInv = await this.client.cache.vanity.get(guild.id);
if (vanityInv) {
newInviteCodes.push({
code: vanityInv,
Expand Down

0 comments on commit 55d0560

Please sign in to comment.