/[sudobot]/branches/6.x/src/services/InviteTrackerService.ts
ViewVC logotype

Annotation of /branches/6.x/src/services/InviteTrackerService.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (hide annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 6028 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 rakinar2 577 /**
2     * This file is part of SudoBot.
3     *
4     * Copyright (C) 2021-2023 OSN Developers.
5     *
6     * SudoBot is free software; you can redistribute it and/or modify it
7     * under the terms of the GNU Affero General Public License as published by
8     * the Free Software Foundation, either version 3 of the License, or
9     * (at your option) any later version.
10     *
11     * SudoBot is distributed in the hope that it will be useful, but
12     * WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU Affero General Public License for more details.
15     *
16     * You should have received a copy of the GNU Affero General Public License
17     * along with SudoBot. If not, see <https://www.gnu.org/licenses/>.
18     */
19    
20     import { Collection, Guild, GuildMember, GuildPremiumTier, Invite, Snowflake, Vanity } from "discord.js";
21     import Service from "../core/Service";
22     import { GatewayEventListener } from "../decorators/GatewayEventListener";
23     import { HasEventListeners } from "../types/HasEventListeners";
24     import { log, logError } from "../utils/logger";
25     import { wait } from "../utils/utils";
26    
27     export const name = "inviteTracker";
28    
29     export default class InviteTrackerService extends Service implements HasEventListeners {
30     public readonly invites = new Collection<`${Snowflake}_${string}`, number>();
31    
32     @GatewayEventListener("ready")
33     async onReady() {
34     log("Syncing the invites...", this.client.guilds.cache.size);
35    
36     for (const [, guild] of this.client.guilds.cache) {
37     if (!this.client.configManager.config[guild.id]?.invite_tracking?.enabled) {
38     continue;
39     }
40    
41     try {
42     const invites = await guild.invites.fetch();
43    
44     for (const [, invite] of invites) {
45     log("Adding invite", invite.code);
46     this.invites.set(`${guild.id}_${invite.code}`, invite.uses ?? 0);
47     }
48    
49     if (guild.premiumTier === GuildPremiumTier.Tier3) {
50     log("Fetching vanity info for guild ", guild.id);
51    
52     try {
53     const vanity = guild.vanityURLUses !== null ? guild.vanityURLUses : (await guild.fetchVanityData()).uses;
54     this.invites.set(`${guild.id}_VANITY`, vanity);
55     } catch (e) {
56     logError(e);
57     }
58     }
59    
60     await wait(1000);
61     } catch (e) {
62     logError(e);
63     }
64     }
65    
66     log("Successfully synced the invites");
67     }
68    
69     @GatewayEventListener("inviteCreate")
70     async onInviteCreate(invite: Invite) {
71     log("Added new invite", invite.code);
72     if (!invite.guild?.id || !this.client.configManager.config[invite.guild?.id ?? ""]?.invite_tracking?.enabled) {
73     log("Invalid guild");
74     return;
75     }
76    
77     this.invites.set(`${invite.guild?.id}_${invite.code}`, invite.uses ?? 0);
78     }
79    
80     @GatewayEventListener("inviteDelete")
81     async onInviteDelete(invite: Invite) {
82     log("Deleted invite", invite.code);
83     if (!invite.guild?.id || !this.client.configManager.config[invite.guild?.id ?? ""]?.invite_tracking?.enabled) {
84     log("Invalid guild");
85     return;
86     }
87    
88     this.invites.delete(`${invite.guild?.id}_${invite.code}`);
89     }
90    
91     @GatewayEventListener("guildUpdate")
92     async onGuildUpdate(oldGuild: Guild, newGuild: Guild) {
93     if (!this.client.configManager.config[newGuild.id]?.invite_tracking?.enabled) {
94     return;
95     }
96    
97     if (newGuild.vanityURLCode === oldGuild.vanityURLCode && newGuild.vanityURLUses === oldGuild.vanityURLUses) {
98     return;
99     }
100    
101     if (oldGuild.vanityURLCode && !newGuild.vanityURLCode) {
102     this.invites.delete(`${newGuild.id}_VANITY`);
103     } else if (!oldGuild.vanityURLCode && newGuild.vanityURLCode) {
104     this.invites.set(`${newGuild.id}_VANITY`, newGuild.vanityURLUses ?? 0);
105     }
106     }
107    
108     async findNewMemberInviteLink(member: GuildMember): Promise<
109     | {
110     isVanity: true;
111     vanity: Vanity;
112     invite: undefined;
113     }
114     | {
115     isVanity: false;
116     vanity: undefined;
117     invite: Invite;
118     }
119     | undefined
120     > {
121     if (!this.client.configManager.config[member.guild.id]?.invite_tracking?.enabled) {
122     return;
123     }
124    
125     await wait(3500);
126     const invites = await member.guild.invites.fetch();
127    
128     for (const [, invite] of invites) {
129     const oldInviteCount = this.invites.get(`${member.guild.id}_${invite.code}`);
130    
131     if (oldInviteCount === undefined) {
132     continue;
133     }
134    
135     log("Compare", invite.code, oldInviteCount, invite.uses);
136    
137     if ((invite.uses ?? 0) > oldInviteCount) {
138     this.invites.set(`${member.guild.id}_${invite.code}`, invite.uses ?? 0);
139     return {
140     isVanity: false,
141     invite,
142     vanity: undefined
143     };
144     }
145     }
146    
147     if (member.guild.premiumTier === GuildPremiumTier.Tier3) {
148     try {
149     const vanity = await member.guild.fetchVanityData();
150     const oldVanityUses = this.invites.get(`${member.guild.id}_VANITY`);
151    
152     if (oldVanityUses === undefined) {
153     log("No vanity");
154     return;
155     }
156    
157     log("Vanity", oldVanityUses, vanity.uses);
158    
159     if (vanity.uses > oldVanityUses) {
160     this.invites.set(`${member.guild.id}_VANITY`, vanity.uses);
161     return {
162     isVanity: true,
163     vanity,
164     invite: undefined
165     };
166     }
167     } catch (e) {
168     logError(e);
169     }
170     }
171     }
172     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26