-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
136 lines (122 loc) · 3.79 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
const fs = require('fs');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
// Require the necessary discord.js classes
const { Client, GatewayIntentBits, Intents, Collection } = require('discord.js');
const { InfluxDB, WriteApi, Point } = require('@influxdata/influxdb-client');
const { hostname } = require("os");
// Create a new client instance
const client = new Client({
shards: 'auto',
intents: [Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES] // something to do with the response time issue??
});
// Loading commands from the commands folder
const commands = [];
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
// Loading the token from .env file
const dotenv = require('dotenv');
const envFILE = dotenv.config();
const TOKEN = process.env['TOKEN'];
const INFLUXDBTOKEN = envFILE.parsed['INFLUXDBTOKEN'];
const INFLUXDBURL = envFILE.parsed['INFLUXDBURL'];
// Create a new InfluxDB client instance with authentication details
const influx = new InfluxDB({
url: INFLUXDBURL,
token: INFLUXDBTOKEN,
});
var commandanalytics = {
"food": {
count: 0
},
"category": {
count: 0
}
}
function submitanalytics() {
const writeApi = influx.getWriteApi("circularsprojects","food-bot","ns")
writeApi.useDefaultTags({location: hostname()})
for (const command in commandanalytics) {
const point1 = new Point('command')
.tag('command', command)
.intField('count', commandanalytics[command].count) // Amount of commands sent in past minute, hour, etc
.timestamp(new Date())
writeApi.writePoint(point1)
commandanalytics[command].count = 0;
}
const serverpoint = new Point('servers')
.intField('count', client.guilds.cache.size)
.timestamp(new Date())
writeApi.writePoint(serverpoint)
try {
writeApi.close()
} catch (e) {
console.error(e)
}
}
setInterval(submitanalytics, 600000);
// Edit your TEST_GUILD_ID here in the env file for development
const TEST_GUILD_ID = envFILE.parsed['TEST_GUILD_ID'];
// Creating a collection for commands in client
client.commands = new Collection();
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
commands.push(command.data.toJSON());
client.commands.set(command.data.name, command);
}
// When the client is ready, run this code (only once)
client.once('ready', () => {
console.log('Ready!');
// Registering the commands in the client
const CLIENT_ID = client.user.id;
const rest = new REST({
version: '9'
}).setToken(TOKEN);
(async () => {
try {
if (!TEST_GUILD_ID) {
await rest.put(
Routes.applicationCommands(CLIENT_ID), {
body: commands
},
);
console.log('Successfully registered application commands globally');
} else {
await rest.put(
Routes.applicationGuildCommands(CLIENT_ID, TEST_GUILD_ID), {
body: commands
},
);
console.log('Successfully registered application commands for development guild');
}
} catch (error) {
if (error) console.error(error);
}
})();
});
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
await interaction.deferReply();
await command.execute(interaction);
if (commandanalytics[interaction.commandName]) {
commandanalytics[interaction.commandName].count += 1;
}
} catch (error) {
if (error) console.error(error);
try {
await interaction.reply({ content: "There was an error while executing this command!", ephemeral: true })
} catch {
// shits fucked (again)
try {
await interaction.channel.send("the bot brokey (again)")
} catch {
// too fucked
}
}
}
});
// Login to Discord with your client's token
client.login(TOKEN);