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 { ChannelType, GuildMember, Message, MessageType } from "discord.js"; |
21 |
|
|
import Client from "../../core/Client"; |
22 |
|
|
import EventListener from "../../core/EventListener"; |
23 |
|
|
import { Events } from "../../types/ClientEvents"; |
24 |
|
|
import { log, logError } from "../../utils/Logger"; |
25 |
|
|
|
26 |
|
|
export default class MessageCreateEvent extends EventListener<Events.MessageCreate> { |
27 |
|
|
public readonly name = Events.MessageCreate; |
28 |
|
|
public readonly types = [MessageType.Default, MessageType.Reply]; |
29 |
|
|
public readonly listeners = [ |
30 |
|
|
(message: Message) => this.client.messageFilter.onMessageCreate(message), |
31 |
|
|
(message: Message) => this.client.messageRuleService.onMessageCreate(message), |
32 |
|
|
(message: Message) => this.client.fileFilter.onMessageCreate(message), |
33 |
|
|
(message: Message) => this.client.aiAutoMod.onMessageCreate(message) |
34 |
|
|
]; |
35 |
|
|
|
36 |
|
|
constructor(protected client: Client) { |
37 |
|
|
super(client); |
38 |
|
|
} |
39 |
|
|
|
40 |
|
|
async execute(message: Message) { |
41 |
|
|
/** |
42 |
|
|
* For performance reasons, ignore bots completely. |
43 |
|
|
*/ |
44 |
|
|
if (message.author.bot) return; |
45 |
|
|
|
46 |
|
|
if (!this.types.includes(message.type)) return; |
47 |
|
|
if (message.channel.type === ChannelType.DM) return; |
48 |
|
|
|
49 |
|
|
let member = message.member as GuildMember; |
50 |
|
|
|
51 |
|
|
if (!("has" in (member.permissions as GuildMember["permissions"]))) { |
52 |
|
|
try { |
53 |
|
|
member = await message.guild!.members.fetch(member.user.id); |
54 |
|
|
|
55 |
|
|
if (!member) { |
56 |
|
|
throw new Error("Invalid member"); |
57 |
|
|
} |
58 |
|
|
|
59 |
|
|
(message.member as GuildMember) = member; |
60 |
|
|
} catch (e) { |
61 |
|
|
logError(e); |
62 |
|
|
} |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
this.client.emit(Events.NormalMessageCreate, message); |
66 |
|
|
|
67 |
|
|
for (const listener of this.listeners) { |
68 |
|
|
if (await listener(message)) { |
69 |
|
|
return; |
70 |
|
|
} |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
await this.client.antispam.onMessageCreate(message).catch(logError); |
74 |
|
|
this.client.statsService.onMessageCreate(message); |
75 |
|
|
this.client.triggerService.onMessageCreate(message); |
76 |
|
|
|
77 |
|
|
const value = await this.client.commandManager.runCommandFromMessage(message).catch(logError); |
78 |
|
|
|
79 |
|
|
if (value === false) { |
80 |
|
|
log("Command or snippet not found: all strategies failed"); |
81 |
|
|
} |
82 |
|
|
} |
83 |
|
|
} |