This repository has been archived by the owner on Apr 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
158 lines (131 loc) · 4.41 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
150
151
152
153
154
155
156
157
158
/*
* ====================NOTE====================
* This code was created by LostAndDead,
* please don't claim this as your own work
* https://github.com/LostAndDead
* ============================================
*/
console.clear()
const Discord = require("discord.js");
const fs = require("fs");
const yaml = require('js-yaml');
const init_commands = require("./init_commands")
const utils = require("./utils")
const bot = new Discord.Client({ disableEveryone: true});
utils.log('---------------------- Restarted ---------------------')
let Config = null;
let calls = []
try {
let fileContents = fs.readFileSync('./config.yml', 'utf8');
Config = yaml.load(fileContents);
}
catch (e) {
console.log(e);
}
//Create calls for slash commands
fs.readdir("./commands/", (err, file) => {
if (err) console.log(err);
let jsfile = file.filter(f => f.split(".").pop() === "js");
if (jsfile.length <= 0) {
console.log("Cant Find Commands");
return;
}
jsfile.forEach((f, i) => {
let props = require(`./commands/${f}`);
let data = props.info
calls.push(data)
});
});
//creates data files if they dont exist
const data = new Uint8Array(Buffer.from('{"Leagues":{},"Ignore":[]}'));
fs.access("data.json", fs.F_OK, (err) => {
if (err) {
if(err.code == "ENOENT"){
fs.writeFile("data.json", data, (err) => {
if (err) throw err;
console.log("Created data.json as it didnt exist")
return
})
}else{
console.error(err)
return
}
}else{
console.log("data.json already exists")
}
})
// D.JS Client listeners
bot.on("error", (e) => console.error(e));
bot.on("warn", (e) => console.warn(e));
//bot.on("debug", (e) => console.info(e));
bot.on('reconnecting', () => console.log('Reconnecting WS...'));
bot.on('disconnect', () => {
console.log('Disconnected, trying to restart...');
process.exit();
});
// NodeJS process listeners
process.on('unhandledRejection', console.error);
process.on('warning', console.warn);
bot.on("ready", async() => {
await init_commands.sendCalls(bot, calls)
console.log("\nOnline!")
console.log("Keep this window open for the bot to run\n")
console.log(`Invite me to a server with the following link.\nhttps://discord.com/api/oauth2/authorize?client_id=${bot.user.id}&permissions=2147609600&scope=bot%20applications.commands\n`);
console.log("Press CTRL+C to exit\n")
bot.ws.on("INTERACTION_CREATE", async interaction => {
const command = interaction.data.name.toLowerCase();
const args = interaction.data.options;
let data = await utils.loadData()
if(data.Ignore.includes(interaction.member.user.id)){
return
}
//Load all the commands
const ping = require("./commands/ping")
const echo = require("./commands/echo")
const league = require("./commands/league")
const result = require("./commands/result")
const table = require("./commands/table")
const adjust = require("./commands/adjust")
const help = require("./commands/help")
const ignore = require("./commands/ignore")
const unignore = require("./commands/unignore")
switch(command){
case "ping":
ping.run(bot, interaction, args)
break;
case "echo":
echo.run(bot, interaction, args)
break;
case "league":
league.run(bot, interaction, args)
break;
case "result":
result.run(bot, interaction, args)
break;
case "table":
table.run(bot, interaction, args)
break;
case "adjust":
adjust.run(bot, interaction, args)
break;
case "help":
help.run(bot, interaction, args)
break;
case "ignore":
ignore.run(bot, interaction, args)
break;
case "unignore":
unignore.run(bot, interaction, args)
break;
}
})
});
bot.on("guildMemberAdd", member => {
if(Config.Setup.AutoRoleID == "0"){return;}
try{
member.roles.add(Config.Setup.AutoRoleID)
}catch (e){
return;
}
})
bot.login(Config.Setup.Token);