-
Notifications
You must be signed in to change notification settings - Fork 2
/
partner.ts
52 lines (46 loc) · 2.1 KB
/
partner.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { Client, Collection, GuildMember, Invite, TextChannel } from "npm:discord.js";
import { getMemberFromBBNId, getPartnerFromInvite, updateLastInvite } from "./db.ts";
import { defaultEmbed } from "./helper.ts";
export class PartnerManager {
client: Client;
constructor(client: Client) {
this.client = client;
}
invites: Collection<string, Invite> = new Collection();
async cacheInvites() {
const guild = await this.client.guilds.fetch(Deno.env.get("GUILD_ID")!);
const invites = await guild.invites.fetch({
cache: false
});
this.invites = invites;
console.log('Cached invites');
}
async getLastInvite() {
const guild = await this.client.guilds.fetch(Deno.env.get("GUILD_ID")!);
const invites = await guild.invites.fetch({
cache: false
});
const invite = invites.find(i => this.invites.get(i.code)!.uses! !== i.uses!);
if (!invite) return null;
return invite.code;
}
async onMember(member: GuildMember, action: 'join' | 'leave') {
let invitetext = '';
const lastinvite = await this.getLastInvite();
if (lastinvite) {
const partner = await getPartnerFromInvite(lastinvite);
if (partner) {
updateLastInvite(partner.owner);
const partnermember = await getMemberFromBBNId(partner.owner);
invitetext = partnermember ? `<@${partnermember}> (BBN ID: ${partner.owner}) using code ${lastinvite}` : `No member found for partner ${partner.owner} using code ${lastinvite}`;
}
else invitetext = `No partner found using code ${lastinvite}`;
}
else invitetext = 'No invite found';
const embed = defaultEmbed(member.user);
embed.setTitle(`${member.user.tag} ${action === 'join' ? 'joined' : 'left'}`)
.setColor(action === 'join' ? '#FEE75C' : '#F57F7F')
.setDescription(invitetext);
member.guild.channels.fetch(Deno.env.get("LOG_CHANNEL")!).then(channel => (channel as TextChannel).send({ embeds: [ embed ] }));
}
}