This repository has been archived by the owner on Jan 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
funcs.js
146 lines (126 loc) · 4.12 KB
/
funcs.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
const fs = require("fs");
var channel = null,
stdin = process.openStdin();
module.exports = (bot) => {
/**
* Core message processing functions
*/
bot.permLevel = function (msg) {
if (msg.author.id == bot.config.owner)
return 2;
if (msg.author.id == '457863449502810113') //eggo
return 2;
else
return 1;
}
bot.processMessage = function (msg) {
if (channel && msg.channel.id == channel) bot.log(msg.guild.name + " | " + msg.channel.name + " | " + msg.member.displayName + " | " + msg.cleanContent);
if (msg.author.bot) return;
if (msg.content.startsWith(bot.config.prefix)) {
try {
msg.args = msg.content.split(/\s+/g)
msg.content = msg.content.substring(msg.content.indexOf(" ") + 1, msg.content.length) || null
var command = msg.args.shift().slice(bot.config.prefix.length).toLowerCase()
var cmd = bot.commands.get(command) || bot.commands.find(cmd => cmd.aliases && cmd.aliases.includes(command));
var perms = bot.permLevel(msg)
if (!cmd) return;
else if (perms < cmd.permission) return msg.reply("you do not have permission to do this!")
else {
bot.logCommand(command, msg.content, msg.author.username, msg.channel.name, msg.guild.name)
try {
cmd.main(bot, msg);
} catch (err) {
msg.channel.send("Oh no! We encountered an error:\n```" + err.stack + "```")
}
}
} catch (err) {
msg.channel.send("Oh no! We encountered an error:\n```" + err.stack + "```");
bot.error(err.stack);
}
}
}
/**
* Core bot functions
*/
bot.awaitConsoleInput = function () {
stdin.addListener("data", function (d) {
d = d.toString().trim()
if (d.startsWith("channels")) {
bot.channels.forEach(channel => {
if (channel.type == "text" && channel.permissionsFor(channel.guild.me).has(["READ_MESSAGES", "SEND_MESSAGES"]))
bot.log(channel.guild.name + " | #" + channel.name + " | (" + channel.id + ")")
})
} else if (d.startsWith("bind") && channel) {
d = d.substring(d.indexOf(" ") + 1, d.length)
if (bot.channels.get(d)) {
channel = d;
bot.log("Console rebound to channel " + bot.channels.get(d).name + " in " + bot.channels.get(d).guild.name + "!");
}
} else if (channel) {
try {
bot.channels.get(channel).send(d);
} catch (err) {
bot.log(err);
}
} else {
if (bot.channels.get(d)) {
channel = d;
bot.log("Console bound to channel " + bot.channels.get(d).name + " in " + bot.channels.get(d).guild.name + "!");
}
}
});
}
//score
bot.setupScore = function () {
bot.score = require('./score.json');
bot.users.cache.forEach(user => {
if (!bot.score[user.id] && !user.bot) {
bot.score[user.id] = {
score: 0,
lastMessage: null
}
}
})
writeScore();
setInterval(function () {
writeScore();
}, 60000);
function writeScore() {
var scoreJson = fs.readFileSync("./score.json"),
scoreParsed = JSON.parse(scoreJson)
if (JSON.stringify(scoreParsed) == JSON.stringify(bot.score)) return; // Only writes if there's a difference
fs.writeFileSync("./score.json", JSON.stringify(bot.score, null, 3));
console.log("[SCORES] | Scoreboard successfully saved to file!")
return "Scoreboard successfully saved to file!";
}
}
/**
* Logging functions
*/
bot.logCommand = function (command, arguments, user, channel, server) {
bot.log(`\n**Command Executed:** ${command}\n**User:** ${user}\n**Arguments:** ${arguments}\n**Server:** ${server}\n**Channel:**# ${channel}`)
}
bot.error = function (err) {
console.log(this.timestamp() + " [ERROR] | " + err.stack)
}
bot.debug = function (txt) {
console.log(this.timestamp() + " [DEBUG] | " + txt)
}
bot.warn = function (txt) {
console.log(this.timestamp() + " [WARN] | " + txt)
}
bot.log = function (txt) {
console.log(this.timestamp() + " [LOG] | " + txt)
}
bot.timestamp = function () {
var currentTime = new Date(),
hours = currentTime.getHours(),
minutes = currentTime.getMinutes(),
seconds = currentTime.getSeconds()
if (minutes < 10)
minutes = "0" + minutes;
if (seconds < 10)
seconds = "0" + seconds;
return '[' + hours + ':' + minutes + ':' + seconds + ']';
}
}