Skip to content

Commit

Permalink
fix(premium): Check premium in intervals
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Crespi committed Dec 3, 2019
1 parent 4806ef3 commit 7faeb03
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ export class IMClient extends Client {
'that support me on Patreon with the pro tier.\n\n' +
'To purchase the pro tier visit https://www.patreon.com/invitemanager\n\n' +
'If you purchased premium run `!premium check` and then `!premium activate` in the server\n\n' +
'I will be leaving your server now, thanks for having me!'
'I will be leaving your server soon, thanks for having me!'
)
.catch(() => undefined);
const onTimeout = async () => {
Expand Down Expand Up @@ -337,7 +337,7 @@ export class IMClient extends Client {
'It looks like this guild was banned from using the InviteManager bot.\n' +
'If you believe this was a mistake please contact staff on our support server.\n\n' +
`${this.config.bot.links.support}\n\n` +
'I will be leaving your server now, thanks for having me!'
'I will be leaving your server soon, thanks for having me!'
)
.catch(() => undefined);
await guild.leave();
Expand Down
54 changes: 54 additions & 0 deletions src/framework/services/PremiumService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import axios from 'axios';
import moment from 'moment';

import { IMClient } from '../../client';
import { BotType } from '../../types';

export class PremiumService {
private client: IMClient = null;
Expand All @@ -10,6 +11,59 @@ export class PremiumService {
this.client = client;
}

public async init() {
if (this.client.type !== BotType.pro) {
return;
}

setInterval(() => this.checkGuilds(), 1 * 60 * 60 * 1000);
}

private async checkGuilds() {
if (this.client.type !== BotType.pro) {
return;
}

console.log('Checking all guilds for premium...');
for (const guild of this.client.guilds.values()) {
let premium = await this.client.cache.premium._get(guild.id);

if (!premium) {
// Let's try and see if this guild had pro before, and if maybe
// the member renewed it, but it didn't update.
const oldPremium = await this.client.db.getPremiumSubscriptionGuildForGuild(guild.id, false);
if (oldPremium) {
await this.checkPatreon(oldPremium.memberId);
premium = await this.client.cache.premium._get(guild.id);
}

if (!premium) {
const dmChannel = await this.client.getDMChannel(guild.ownerID);
await dmChannel
.createMessage(
'Hi!' +
`Thanks for inviting me to your server \`${guild.name}\`!\n\n` +
'I am the pro version of InviteManager, and only available to people ' +
'that support me on Patreon with the pro tier.\n\n' +
'To purchase the pro tier visit https://www.patreon.com/invitemanager\n\n' +
'If you purchased premium run `!premium check` and then `!premium activate` in the server\n\n' +
'I will be leaving your server soon, thanks for having me!'
)
.catch(() => undefined);
const onTimeout = async () => {
// Check one last time before leaving
if (await this.client.cache.premium._get(guild.id)) {
return;
}

await guild.leave();
};
setTimeout(onTimeout, 3 * 60 * 1000);
}
}
}
}

public async checkPatreon(userId: string) {
const res = await axios
.get(`https://api.invitemanager.gg/patreon/check/?userId=${userId}`, {
Expand Down

0 comments on commit 7faeb03

Please sign in to comment.