forked from 3ddelano/pc-statbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
119 lines (100 loc) · 3.49 KB
/
server.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
const fs = require("fs");
const moment = require("moment");
const config = JSON.parse(fs.readFileSync("./config.json"));
const Discord = require('discord.js');
const client = new Discord.Client();
let MESSAGE;
// load enabled components
const component_names = fs.readdirSync('./components/');
const components = new Map();
component_names.forEach(file_name => {
if (!file_name.endsWith('.js')) return;
const file = require(`./components/${file_name}`);
const component_name = file_name.split('.js').join('');
if (config.components[component_name])
components.set(component_name, file);
});
async function update() {
if (!MESSAGE) return console.log('Unable to fetch channel or message.');
let payload = '';
if (config.title != ""){
payload += `\n__**${config.title}**__\n`;
}
const promises = [];
components.forEach(component => promises.push(component.update()));
const values = await Promise.all(promises);
payload += values.join('\n');
switch (config.displayTimestamp) {
case false:
break;
case "12h": case "12": case "": case true:
payload += `\n:timer: **Last Updated:** ${moment().format("hh:mm:ss A DD-MM-YYYY")} `;
break;
case "24h": case "24":
payload += `\n:timer: **Last Updated:** ${moment().format("HH:mm:ss DD-MM-YYYY")} `;
break;
default:
console.error('Invalid displayTimestamp config value.');
break;
}
MESSAGE.edit(payload);
setTimeout(update, config.interval * 1000);
}
client.on('ready', async () => {
if (config.activityDisplay == true){
client.user.setPresence({ activity: { name: 'Watching s.help' }, status: 'active' })
}
console.log(`Logged in as ${client.user.tag} !`);
if (config.messageID && config.channelID) {
let channel = await client.channels.fetch(config.channelID);
MESSAGE = await channel.messages.fetch(config.messageID);
console.log(`Updating every ${config.interval}s...`);
update();
} else {
console.log("Waiting for s.start");
}
});
client.on('message', async (message) => {
if (message.author.bot) return;
if (message.content === 's.start') {
if (config.messageID) return message.reply("stats has already started.");
let msg = await message.channel.send("Updatig stats...");
config.messageID = msg.id;
config.channelID = msg.channel.id;
MESSAGE = msg;
fs.writeFileSync('./config.json', JSON.stringify(config, null, 2));
update();
}
if (message.content === 's.ping') {
const msg = await message.channel.send('Ping?')
msg.edit(`Pong! Latency is ${Math.round(msg.createdTimestamp - message.createdTimestamp)} ms. API Latency is ${Math.round(client.ws.ping)} ms`)
}
if (message.content === 's.help') {
const help = [
'Welcome to PC STAT BOT',
'',
'Here are the list of commands: ',
'`s.ping` - shows my latency',
'`s.start` - start updating the stats',
'`s.help` - shows this message',
'',
'About me',
'I am made by Delano Lourenco - https://delano-lourenco.web.app',
'For suggestions or bugs join my support server - https://discord.gg/FZY9TqW'
].join('\n');
return message.channel.send(help);
}
});
try {
console.log("Starting PC STAT BOT");
if (config.clientID) console.log(`Invite Link: https://discord.com/oauth2/authorize?client_id=${config.clientID}&scope=bot&permissions=2048`)
client.login(config.token);
} catch (err) {
console.log('Error logging in.', err);
client.login(config.token);
}
client.on("error", (err) => {
console.log('Dicord Client Error:', err);
client.login(config.token);
})
process.on('unhandledRejection', err => { console.log('Caught Error:', err) })