-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathindex.js
199 lines (164 loc) · 6.77 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const fs = require("fs");
const config = require("./utils/config.js");
const db = require('./utils/database')
const fetch = require('node-fetch')
const {
REST,
Client,
GatewayIntentBits,
ActivityType,
Collection,
EmbedBuilder,
Routes,
Partials,
Colors
} = require("discord.js");
const client = new Client({
intents: [
GatewayIntentBits.Guilds
],
partials: [Partials.Channel]
});
const commands = [];
const clientCommands = new Collection();
function readCommandsFromDirectory(directory) {
const commandFiles = fs.readdirSync(directory, { withFileTypes: true });
for (const file of commandFiles) {
if (file.isDirectory()) {
readCommandsFromDirectory(`${directory}/${file.name}`);
} else if (file.isFile() && file.name.endsWith(".js")) {
const command = require(`${directory}/${file.name}`);
commands.push(command.data.toJSON());
clientCommands.set(command.data.name, command);
}
}
}
readCommandsFromDirectory("./commands");
client.on("error", console.error);
process.on('unhandledRejection', error => {
console.error('Encountered an unhandled promise rejection', error);
});
client.once('ready', async () => {
console.clear();
console.log("Bot is now online.");
console.log("Currently signed in as:", client.user.tag)
const CLIENT_ID = client.user.id;
const rest = new REST().setToken(config.token);
(async () => {
try {
if (config.type === "production") {
await rest.put(Routes.applicationCommands(CLIENT_ID), {
body: commands
});
console.log("Commands have been incorporated for Global Usage.")
} else {
await rest.put(Routes.applicationGuildCommands(CLIENT_ID, config.DevelopmentServerId), {
body: commands
})
console.log(`Command access is now restricted to guild members.`)
}
} catch (err) {
console.error({
rawError: err.rawError,
Errors: err["rawError"]["errors"]
});
}
})();
await setPresence(client, `KeyAuth.cc`, { type: ActivityType.Competing, status: 'online' });
});
client.on('interactionCreate', async interaction => {
if (interaction.isButton()) {
if (interaction.customId.startsWith("selectapp_")) {
const id = interaction.customId.split("_")[1];
const idFromGuild = interaction.guild ? interaction.guild.id : interaction.user.id;
const applications = await db.get(`applications_${idFromGuild}`) || [];
if (applications.length === 0) {
return interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription("No applications have been added yet.")
.setColor(Colors.Red)
.setTimestamp()
],
ephemeral: true
});
}
const selectedApp = applications.find(app => app.id === id);
if (!selectedApp) {
return interaction.editReply({
embeds: [
new EmbedBuilder()
.setDescription("The selected application does not exist.")
.setColor(Colors.Red)
.setTimestamp()
],
ephemeral: true
});
}
db.get(`token_${idFromGuild}`);
db.set(`token_${idFromGuild}`, selectedApp.sellerkey);
return interaction.reply({
embeds: [
new EmbedBuilder()
.setDescription(`The application \`${selectedApp.application}\` has been selected.`)
.setColor(Colors.Green)
.setTimestamp()
],
ephemeral: true
});
}
return false;
}
if (!interaction.type === 2) return;
const command = clientCommands.get(interaction.commandName);
if (!command) return;
let idfrom = interaction.guild ? interaction.guild.id : interaction.user.id;
let ephemeral = !interaction.guild ? false : true;
await interaction.deferReply({ ephemeral: ephemeral });
if (interaction.member != null)
if (!interaction.member.roles.cache.find(x => x.name == "perms")) return interaction.editReply({ embeds: [new EmbedBuilder().setDescription(`You need a role with the name \`perms\` to execute commands. Please ask an administrator to create a role with this name if not already done and assign it to you.`).setColor(Colors.Red).setTimestamp()], ephemeral: true })
await setPresence(client, `KeyAuth.cc`, { type: ActivityType.Competing, status: 'online' });
let content = `**${interaction.user.username}${interaction.user.discriminator ? ("#" + interaction.user.discriminator) : ''} (ID: ${interaction.user.id})** executed the command \`/${interaction.commandName}\`\n`;
for (const option of interaction.options._hoistedOptions) {
content += `\n${option.name} : ${option.value}`;
}
let wh_url = await db.get(`wh_url_${idfrom}`)
if (wh_url != null) {
var params = {
content: content
}
fetch(wh_url, {
method: "POST",
headers: {
'Content-type': 'application/json'
},
body: JSON.stringify(params)
})
}
try {
await command.execute(interaction);
} catch (err) {
if (err) console.error(err);
await interaction.editReply({
embeds: [new EmbedBuilder()
.setAuthor({ name: "Interaction was unsuccessful." })
.setColor(Colors.Red)
.setTimestamp()
.setFooter({ text: "KeyAuth Discord Bot", iconURL: client.user.displayAvatarURL() })],
ephemeral: true
})
}
});
async function setPresence(client, text, options = { type: ActivityType.Competing, status: 'online' }) {
client.user.setPresence({
activities: [{ name: text, type: options.type }],
status: options.status,
});
}
client.login(config.token).catch(err => {
console.clear();
if (err.code === "TokenInvalid") {
console.error("The provided token is invalid. Please review your config.json file or check the Environment Variables if you're not using a JSON config, and attempt again.");
process.exit(1);
}
})