1 |
import { GuildMember } from "discord.js"; |
2 |
import DiscordClient from "../client/Client"; |
3 |
import MessageEmbed from "../client/MessageEmbed"; |
4 |
import Service from "../utils/structures/Service"; |
5 |
|
6 |
export default class Automute extends Service { |
7 |
MuteRecord: typeof import("../models/MuteRecord").default; |
8 |
|
9 |
constructor(client: DiscordClient) { |
10 |
super(client); |
11 |
this.MuteRecord = require("../models/MuteRecord").default; |
12 |
} |
13 |
|
14 |
public async mute(member: GuildMember) { |
15 |
await member.roles.add(this.client.config.props[member.guild.id].mute_role); |
16 |
} |
17 |
|
18 |
public async onMemberJoin(member: GuildMember) { |
19 |
const { MuteRecord } = this; |
20 |
const muteRecord = await MuteRecord.findOne({ |
21 |
user_id: member.user.id, |
22 |
guild_id: member.guild.id |
23 |
}); |
24 |
|
25 |
if (!muteRecord) { |
26 |
return; |
27 |
} |
28 |
|
29 |
await this.mute(member); |
30 |
|
31 |
this.client.logger.send(member.guild, { |
32 |
embeds: [ |
33 |
new MessageEmbed({ |
34 |
author: { |
35 |
name: member.user.tag, |
36 |
iconURL: member.user.displayAvatarURL(), |
37 |
}, |
38 |
description: 'This user had left the server when they were muted. They\'ve been muted again.', |
39 |
color: 'GOLD', |
40 |
footer: { text: 'Auto Mute' } |
41 |
}) |
42 |
.setTimestamp() |
43 |
] |
44 |
}); |
45 |
|
46 |
await muteRecord.delete(); |
47 |
} |
48 |
|
49 |
public async onMemberLeave(member: GuildMember) { |
50 |
const { MuteRecord } = this; |
51 |
|
52 |
if (!member.roles.cache.has(this.client.config.props[member.guild.id].mute_role)) { |
53 |
return; |
54 |
} |
55 |
|
56 |
const muteRecord = await MuteRecord.findOne({ |
57 |
user_id: member.user.id, |
58 |
guild_id: member.guild.id |
59 |
}); |
60 |
|
61 |
if (!muteRecord) { |
62 |
await MuteRecord.create({ |
63 |
user_id: member.user.id, |
64 |
guild_id: member.guild.id, |
65 |
createdAt: new Date() |
66 |
}); |
67 |
} |
68 |
} |
69 |
} |