1 |
const { Client, Intents } = require("discord.js"); |
2 |
const { config } = require("dotenv"); |
3 |
const CommandManager = require("./CommandManager"); |
4 |
const path = require("path"); |
5 |
const fs = require("fs"); |
6 |
const Config = require("./Config"); |
7 |
const Database = require("./Database"); |
8 |
const Logger = require("./Logger"); |
9 |
const SpamFilter = require("./SpamFilter"); |
10 |
const server = require("./server"); |
11 |
const AntiRaid = require("./AntiRaid"); |
12 |
|
13 |
class App { |
14 |
constructor(rootdir) { |
15 |
global.app = App.app = this; |
16 |
this.rootdir = rootdir; |
17 |
this.loadConfig(); |
18 |
this.env = process.env; |
19 |
|
20 |
this.client = new Client({ |
21 |
partials: ["CHANNEL"], |
22 |
intents: [ |
23 |
Intents.FLAGS.GUILDS, |
24 |
Intents.FLAGS.GUILD_MESSAGES, |
25 |
Intents.FLAGS.DIRECT_MESSAGES, |
26 |
Intents.FLAGS.DIRECT_MESSAGE_TYPING, |
27 |
Intents.FLAGS.GUILD_PRESENCES, |
28 |
Intents.FLAGS.GUILD_MEMBERS, |
29 |
] |
30 |
}); |
31 |
|
32 |
this.config = new Config(); |
33 |
this.db = new Database(path.resolve(__dirname, '..', 'database.db')); |
34 |
this.commandManager = new CommandManager(path.resolve(__dirname, rootdir, "commands")); |
35 |
this.logger = new Logger(); |
36 |
this.spamFilter = new SpamFilter(); |
37 |
this.antiRaid = new AntiRaid(); |
38 |
this.boot(); |
39 |
} |
40 |
|
41 |
boot() { |
42 |
this.on('ready', () => { |
43 |
console.log("Logged in as " + this.client.user.tag); |
44 |
server(); |
45 |
}); |
46 |
|
47 |
this.on('messageCreate', async (message) => { |
48 |
if (message.author.bot) { |
49 |
return; |
50 |
} |
51 |
|
52 |
await (this.msg = message); |
53 |
|
54 |
// await this.spamFilter.start(message); |
55 |
|
56 |
await this.commandManager.setMessage(message); |
57 |
|
58 |
const valid = await this.commandManager.valid(); |
59 |
const has = await this.commandManager.has(); |
60 |
const snippet = await this.commandManager.snippet(); |
61 |
|
62 |
if (valid && has) { |
63 |
await this.exec(); |
64 |
} |
65 |
else if (valid && snippet !== undefined) { |
66 |
await message.channel.send({ |
67 |
content: snippet.content |
68 |
}); |
69 |
} |
70 |
else if (valid && !has) { |
71 |
await this.commandManager.notFound(); |
72 |
} |
73 |
}); |
74 |
|
75 |
this.on("messageUpdate", async (oldMessage, newMessage) => { |
76 |
if (oldMessage.author.bot) |
77 |
return; |
78 |
|
79 |
await this.logger.logEdit(oldMessage, newMessage); |
80 |
}); |
81 |
|
82 |
this.on("messageDelete", async (message) => { |
83 |
if (message.author.bot) |
84 |
return; |
85 |
|
86 |
await this.logger.logDelete(message); |
87 |
}); |
88 |
|
89 |
this.on("guildCreate", guild => { |
90 |
console.log("Joined a new guild: " + guild.name); |
91 |
|
92 |
this.config.props[guild.id] = { |
93 |
prefix: "-", |
94 |
debug: false, |
95 |
}; |
96 |
|
97 |
this.config.write(); |
98 |
}) |
99 |
|
100 |
this.on("guildDelete", guild => { |
101 |
console.log("Left a guild: " + guild.name); |
102 |
delete this.config.props[guild.id]; |
103 |
this.config.write(); |
104 |
}) |
105 |
|
106 |
this.on('guildMemberAdd', async (member) => { |
107 |
console.log('Joined'); |
108 |
await this.antiRaid.start(member); |
109 |
}); |
110 |
} |
111 |
|
112 |
loadConfig() { |
113 |
if (fs.existsSync(path.join(__dirname, this.rootdir, '.env'))) { |
114 |
console.log("Loading .env file"); |
115 |
config(); |
116 |
} |
117 |
} |
118 |
|
119 |
on(event, handler) { |
120 |
this.client.on(event, handler); |
121 |
} |
122 |
|
123 |
exec() { |
124 |
this.commandManager.exec(); |
125 |
} |
126 |
|
127 |
run() { |
128 |
this.client.login(process.env.TOKEN); |
129 |
} |
130 |
|
131 |
tempFileCreate(name) { |
132 |
const fullname = path.join(__dirname, '..', 'tmp', name); |
133 |
const file = fs.createWriteStream(fullname); |
134 |
|
135 |
return { |
136 |
name: fullname, |
137 |
file |
138 |
}; |
139 |
} |
140 |
} |
141 |
|
142 |
module.exports = App; |