-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
170 lines (142 loc) · 5.27 KB
/
bot.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
const { Collection, GatewayIntentBits, ActivityType } = require("discord.js")
const discordClient = require("discord.js")
const { readFileSync, readdirSync } = require("fs")
const { connect } = require("mongoose")
// import and require .env reference
require('dotenv').config();
const {publicToken, devToken, databaseTokenPublic, databaseTokenDev, epicGamesAuth} = process.env;
// Calling config and utils file
const config = JSON.parse(readFileSync(`./config.json`, 'utf8'))
const tools = require(`${config.provider == true ? `/home/electrocute4u/bot` : `.`}/utils/functions`)
let token;
if (config.dev == false) token = publicToken
if (config.dev == true) token = devToken
const bot = new discordClient.Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMessagePolls,
GatewayIntentBits.GuildModeration
],
allowedMentions: { parse: ['users', 'roles'], repliedUser: true }
})
// Utilize the Hybrid Sharding instead of
//const Cluster = require('discord-hybrid-sharding');
// Initiate client (with Cluster Manager handling shards)
// const bot = new Client({
// intents: [GatewayIntentBits.Guilds, GatewayIntentBits.DirectMessages], // The needed intents for the bot to function
// shards: Cluster.data.SHARD_LIST, // An array of shards that will get spawned (handled by Cluster Manager)
// shardCount: Cluster.data.TOTAL_SHARDS, // Total number of shards (handled by Cluster Manager)
// });
// initialize the Client, so we can access the .broadcastEval()
// bot.cluster = new Cluster.Client(bot);
// Command collection
bot.commands = new Collection();
bot.commandArray = [];
// Buttons collection
bot.buttons = new Collection();
// Cooldown collection
bot.cooldown = new Collection();
// Pick presence
bot.pickPresence = async () => {
const options = [
{
type: ActivityType.Playing,
text: "🎮 Dauntless",
status: "online"
},
{
type: ActivityType.Competing,
text: "🪙 Gauntlet",
status: "online"
},
{
type: ActivityType.Competing,
text: "⏳ Trials",
status: "online"
},
{
type: ActivityType.Custom,
text: "🌌 Gazing upon the stars in Twilight Sanctuary",
status: "online"
},
{
type: ActivityType.Custom,
text: "🏙️ Enjoying the view in Ramsgate",
status: "online"
},
{
type: ActivityType.Custom,
text: "🗻 Climbing the Escalation islands",
status: "online"
}
];
const option = Math.floor(Math.random() * options.length)
bot.user.setPresence({
activities: [
{
name: options[option].text,
type: options[option].type,
}],
status: options[option].status
})
}
// Require functions for handlers (commands, components and events)
const functionFolders = readdirSync(`${config.provider == true ? `/home/electrocute4u/bot` : `.`}/functions`)
for (const folder of functionFolders) {
const functionFiles = readdirSync(`${config.provider == true ? `/home/electrocute4u/bot` : `.`}/functions/${folder}`).filter((file) => file.endsWith(".js"));
for (const file of functionFiles)
require(`${config.provider == true ? `/home/electrocute4u/bot` : `.`}/functions/${folder}/${file}`)(bot);
}
bot.handleEvents();
bot.handleCommands();
// Bot login
bot.login(token);
if(config.dev == false) {
let epicClient
async function signIntoEpic() {
try {
// Login to Epic Games API
const { readFile, writeFile } = require('fs').promises;
const { Client } = require('fnbr');
let auth;
try {
auth = { deviceAuth: JSON.parse(await readFile(`./deviceAuth.json`)) };
} catch (e) {
auth = { authorizationCode: async () => epicGamesAuth };
}
epicClient = new Client({ auth });
epicClient.on('deviceauth:created', (da) => writeFile(`./deviceAuth.json`, JSON.stringify(da, null, 2)));
await epicClient.login();
console.log(`Logged into Epic Games API as ${epicClient.user.displayName}`);
} catch (e) {
tools.CustomLog("Something happened with Epic Games API", "Error")
// handle error
// console.error(e)
}
}
// Login to Epic client
signIntoEpic().then(() => {
bot.epicClient = epicClient
}).catch(() => null)
}
// Handle and log rejection errors without crashing process
process.on('uncaughtException', (err, origin) => {
console.log(err.stack)
tools.CustomLog(`Caught exception: ${err}\n` + `Exception origin: ${origin}`);
});
// Handle and log rejection errors without crashing process
process.on('uncaughtExceptionMonitor', (err, origin) => {
console.log(`Uncaught Exception Monitor:`, err, origin)
});
// Handle and log rejection errors without crashing process
process.on('unhandledRejection', async (reason, promise) => {
console.log(`Unhandled rejection at:`, promise, `reason:`, reason)
});
// Connect to the Database Cluster
// (async () => {
// await connect(config.dev == false ? databaseTokenPublic : databaseTokenDev).catch(console.error)
// })();