forked from GDColon/Polaris-Open
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
149 lines (117 loc) · 5.81 KB
/
index.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const Discord = require("discord.js")
const fs = require("fs")
const config = require("./config.json")
const Tools = require("./classes/Tools.js")
const Model = require("./classes/DatabaseModel.js")
// automatic files: these handle discord status and version number, manage them with the dev commands
const autoPath = "./json/auto/"
if (!fs.existsSync(autoPath)) fs.mkdirSync(autoPath)
if (!fs.existsSync(autoPath + "status.json")) fs.copyFileSync("./json/default_status.json", autoPath + "status.json")
if (!fs.existsSync(autoPath + "version.json")) fs.writeFileSync(autoPath + "version.json", JSON.stringify({ version: "1.0.0", updated: Date.now() }, null, 2))
const rawStatus = require("./json/auto/status.json")
const version = require("./json/auto/version.json")
const startTime = Date.now()
// create client
const client = new Discord.Client({
allowedMentions: { parse: ["users"] },
makeCache: Discord.Options.cacheWithLimits({ MessageManager: 0 }),
intents: ['Guilds', 'GuildMessages', 'DirectMessages', 'GuildVoiceStates', 'GuildMembers'].map(i => Discord.GatewayIntentBits[i]),
partials: ['Channel'].map(p => Discord.Partials[p]),
failIfNotExists: false
})
if (!client.shard) {
console.error("No sharding info found!\nMake sure you start the bot from polaris.js, not index.js")
return process.exit()
}
client.shard.id = client.shard.ids[0]
client.globalTools = new Tools(client);
// connect to db
client.db = new Model("servers", require("./database_schema.js").schema)
// command files
const dir = "./commands/"
client.commands = new Discord.Collection()
fs.readdirSync(dir).forEach(type => {
fs.readdirSync(dir + type).filter(x => x.endsWith(".js")).forEach(file => {
let command = require(dir + type + "/" + file)
if (!command.metadata) command.metadata = { name: file.split(".js")[0] }
command.metadata.type = type
client.commands.set(command.metadata.name, command)
})
})
client.statusData = rawStatus
client.updateStatus = function() {
let status = client.statusData
client.user.setPresence({ activities: status.type ? [{ name: status.name, state: status.state || undefined, type: Discord.ActivityType[status.type], url: status.url }] : [], status: status.status })
}
// when online
client.on("ready", () => {
if (client.shard.id == client.shard.count - 1) console.log(`Bot online! (${+process.uptime().toFixed(2)} secs)`)
client.startupTime = Date.now() - startTime
client.version = version
client.application.commands.fetch() // cache slash commands
.then(cmds => {
if (cmds.size < 1) { // no commands!! deploy to test server
console.info("!!! No global commands found, deploying dev commands to test server (Use /deploy global=true to deploy global commands)")
client.commands.get("deploy").run(client, null, client.globalTools)
}
})
client.updateStatus()
setInterval(client.updateStatus, 15 * 60000);
// run the web server
if (client.shard.id == 0 && config.enableWebServer) require("./web_app.js")(client)
})
// on message
client.on("messageCreate", async message => {
if (message.system || message.author.bot) return
else if (!message.guild || !message.member) return // dm stuff
else client.commands.get("message").run(client, message, client.globalTools)
})
// on interaction
client.on("interactionCreate", async int => {
if (!int.guild) return int.reply("You can't use commands in DMs!")
// for setting changes
if (int.isStringSelectMenu()) {
if (int.customId.startsWith("configmenu_")) {
if (int.customId.split("_")[1] != int.user.id) return int.deferUpdate()
let configData = int.values[0].split("_").slice(1)
let configCmd = (configData[0] == "dir" ? "button:settings_list" : "button:settings_view")
client.commands.get(configCmd).run(client, int, new Tools(client, int), configData)
}
return;
}
// also for setting changes
else if (int.isModalSubmit()) {
if (int.customId.startsWith("configmodal")) {
let modalData = int.customId.split("~")
if (modalData[2] != int.user.id) return int.deferUpdate()
client.commands.get("button:settings_edit").run(client, int, new Tools(client, int), modalData[1])
}
return;
}
// general commands and buttons
let foundCommand = client.commands.get(int.isButton() ? `button:${int.customId.split("~")[0]}` : int.commandName)
if (!foundCommand) return
else if (foundCommand.metadata.slashEquivalent) foundCommand = client.commands.get(foundCommand.metadata.slashEquivalent)
let tools = new Tools(client, int)
// dev perm check
if (foundCommand.metadata.dev && !tools.isDev()) return tools.warn("Only developers can use this!")
else if (config.lockBotToDevOnly && !tools.isDev()) return tools.warn("Only developers can use this bot!")
try { await foundCommand.run(client, int, tools) }
catch(e) { console.error(e); int.reply({ content: "**Error!** " + e.message, ephemeral: true }) }
})
// on join
client.on("guildMemberAdd", async (member) => {
console.log(`New User "${member.user.username}" has joined "${member.guild.name}"` );
// get user data
let tools = new Tools(client)
let db = await tools.fetchSettings(member.id, member.guild.id)
let userData = db.users[member.id] || { xp: 0, cooldown: 0 }
let level = tools.getLevel(userData.xp, db.settings)
let roleCheck = tools.checkLevelRoles(member.guild.roles.cache, member.roles.cache, level, db.settings.rewards)
tools.syncLevelRoles(member, roleCheck)
});
client.on('error', e => console.warn(e))
client.on('warn', e => console.warn(e))
process.on('uncaughtException', e => console.warn(e))
process.on('unhandledRejection', (e, p) => console.warn(e))
client.login(process.env.DISCORD_TOKEN)