1 |
|
/** |
2 |
|
* This file is part of SudoBot. |
3 |
|
* |
4 |
|
* Copyright (C) 2021-2022 OSN Inc. |
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 { formatDistanceToNowStrict } from "date-fns"; |
import { formatDistanceToNowStrict } from "date-fns"; |
21 |
import { CommandInteraction, GuildMember, Message, User, Util } from "discord.js"; |
import { CommandInteraction, GuildMember, Message, Util } from "discord.js"; |
22 |
import DiscordClient from "../client/Client"; |
import DiscordClient from "../client/Client"; |
23 |
import MessageEmbed from "../client/MessageEmbed"; |
import MessageEmbed from "../client/MessageEmbed"; |
24 |
import AFKCommand from "../commands/utils/AFKCommand"; |
import AFK, { IAFK } from "../models/AFK"; |
|
import AFK from "../models/AFK"; |
|
25 |
import Service from "../utils/structures/Service"; |
import Service from "../utils/structures/Service"; |
26 |
|
|
27 |
export interface MentionSchema { |
export interface MentionSchema { |
30 |
} |
} |
31 |
|
|
32 |
export default class AFKEngine extends Service { |
export default class AFKEngine extends Service { |
33 |
list: AFK[] = []; |
list: IAFK[] = []; |
34 |
|
|
35 |
constructor(client: DiscordClient) { |
constructor(client: DiscordClient) { |
36 |
super(client); |
super(client); |
37 |
AFK.findAll().then(models => this.list = models).catch(console.error); |
AFK.find().then(models => this.list = models).catch(console.error); |
38 |
} |
} |
39 |
|
|
40 |
findUsers(ids: string[], guild: string) { |
findUsers(ids: string[]) { |
41 |
return this.list.filter(afk => ids.includes(afk.get("user") as string) && afk.get("guild_id") as string === guild); |
return this.list.filter(afk => ids.includes(afk.user)); |
42 |
} |
} |
43 |
|
|
44 |
async removeUser(id: string, guild: string) { |
async removeUser(id: string) { |
45 |
let index = 0; |
let index = 0; |
46 |
|
|
47 |
for await (const afk of this.list) { |
for await (const afk of this.list) { |
48 |
if (afk.get('user') === id && afk.get("guild_id") === guild) { |
if (afk.user === id) { |
49 |
await afk.destroy(); |
await afk.delete(); |
50 |
this.list.splice(index, 1); |
this.list.splice(index, 1); |
51 |
} |
} |
52 |
|
|
55 |
} |
} |
56 |
|
|
57 |
async toggle(message: Message | CommandInteraction, enable: boolean = false, status?: string) { |
async toggle(message: Message | CommandInteraction, enable: boolean = false, status?: string) { |
58 |
const afk = this.findUsers([message.member!.user.id], message.guild!.id); |
const afk = this.findUsers([message.member!.user.id]); |
59 |
|
|
60 |
if (afk.length > 0) { |
if (afk.length > 0) { |
61 |
const mentions = afk[0].get("mentions")! as Array<MentionSchema>; |
const mentions = afk[0].get("mentions")! as Array<MentionSchema>; |
84 |
count++; |
count++; |
85 |
} |
} |
86 |
|
|
87 |
await this.client.afkEngine.removeUser(message.member!.user.id, message.guild!.id); |
await this.client.afkEngine.removeUser(message.member!.user.id); |
88 |
|
|
89 |
await message.reply({ |
await message.reply({ |
90 |
embeds: [ |
embeds: [ |
99 |
user: message.member!.user.id, |
user: message.member!.user.id, |
100 |
guild_id: message.guild!.id, |
guild_id: message.guild!.id, |
101 |
mentions: [], |
mentions: [], |
102 |
reason: status ?? undefined |
reason: status ?? undefined, |
103 |
|
createdAt: new Date() |
104 |
})); |
})); |
105 |
|
|
106 |
await message.reply({ |
await message.reply({ |
117 |
if (msg.author.bot) |
if (msg.author.bot) |
118 |
return; |
return; |
119 |
|
|
120 |
const selfAFK = this.findUsers([msg.author.id], msg.guild!.id); |
const selfAFK = this.findUsers([msg.author.id]); |
121 |
|
|
122 |
if (selfAFK.length > 0) { |
if (selfAFK.length > 0) { |
123 |
this.toggle(msg, false); |
this.toggle(msg, false); |
126 |
const mention = msg.mentions.members?.first(); |
const mention = msg.mentions.members?.first(); |
127 |
|
|
128 |
if (mention) { |
if (mention) { |
129 |
const afkRecords: AFK[] = this.findUsers([...msg.mentions.members!.keys()].slice(0, 3), msg.guild!.id); |
const afkRecords: Array<IAFK> = this.findUsers([...msg.mentions.members!.keys()].slice(0, 3)).filter(afk => afk.user !== msg.author.id); |
130 |
|
|
131 |
if (!afkRecords || afkRecords.length < 1) { |
if (!afkRecords || afkRecords.length < 1) { |
132 |
return; |
return; |
133 |
} |
} |
134 |
|
|
135 |
for (const record of afkRecords) { |
for (const record of afkRecords) { |
136 |
const mentions = record.get("mentions") as MentionSchema[]; |
const mentions = record.mentions as MentionSchema[]; |
137 |
|
|
138 |
mentions.push({ |
mentions.push({ |
139 |
date: Date.now(), |
date: Date.now(), |