forked from marjobtw/horsebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
310 lines (263 loc) · 10.1 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// Colors:
// Base: 00a2ff
// Warning: ff8400
// Error: ff7070
/**
* Module Imports
*/
Array.prototype.contains = function (needle) {
for (i in this) {
if (this[i] == needle) return true;
}
return false;
}
const { Client, Collection, MessageActionRow, MessageButton, MessageEmbed, Partials, GatewayIntentBits} = require("discord.js");
const { readdirSync } = require("fs");
const { join } = require("path");
require('dotenv').config()
const wait = require('util').promisify(setTimeout);
const fs = require("fs");
const fetch = require("node-fetch");
const myIntents = [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildEmojisAndStickers, GatewayIntentBits.GuildInvites, GatewayIntentBits.GuildPresences, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMessageReactions, GatewayIntentBits.DirectMessages, GatewayIntentBits.DirectMessageReactions, GatewayIntentBits.MessageContent]
const client = new Client({ disableMentions: "everyone", partials: ['MESSAGE', 'CHANNEL', 'REACTION'], intents: myIntents});
var PREFIX = "!"
client.restarting = false;
const log = text => console.log(text)
//IDs of the account that can use internal commands
client.devs = ["191647893835350017", "225700324680269824"]
//Activity
client.statusText = "Stallion Squad"
client.statusType = "COMPETING"
//Check these directories at the bot start
client.searchDirectories = ["commands", "commands/selfRoles"]
client.login(process.env.BOT_TOKEN).then(r => log(`Login initialized`));
client.commands = new Collection();
client.prefix = PREFIX;
client.queue = new Map();
const cooldowns = new Collection();
// Reaction things (so that it can fetch channels and msgs at the start of the bot
let reactionChannel = "778618553275908129";
let reactionMsgs = ['787080813635567657', '859205200279371806'];
const escapeRegex = (str) => str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
/**
* Client Events
*/
client.on("ready", async () => {
let error = false;
console.log(`${client.user.username} activated`);
//Fetch Item Definitions from API
await fetch("http://api.peekio.no/GetItemDefs.php?format=json", {method: "Get"})
.then(res => res.json())
.catch(e => {console.log(e); error = true})
.then(!error ? json => client.steamItems = JSON.parse(json.response.itemdef_json) : null);
if(client.steamItems != null) {
console.log(`Loaded ${client.steamItems.length} steam items`)
}
client.user.setActivity(`${client.statusText}`, { type: client.statusType, url: "https://www.twitch.tv/directory/game/Stallion%20Squad" });
});
client.on("warn", (info) => console.log(info));
client.on("error", console.error);
/**
* Import all commands
*/
//const commandFiles = readdirSync(join(__dirname, "commands")).filter((file) => file.endsWith(".js"));
let commandFiles = [];
client.searchDirectories.forEach(i => {
let files = readdirSync(join(__dirname, i)).filter((file) => file.endsWith(".js"));
files.forEach(b => commandFiles.push(join(i, b)));
})
let modules = commandFiles.flat();
log(`Found ${modules.length} modules`)
for (const file of modules) {
const command = require(join(__dirname, `${file}`));
client.commands.set(command.name, command);
let moduleName = file.split("\\")
log(`Loaded module "${moduleName[moduleName.length-1].split(".")[0]}"`)
}
client.on("messageCreate", async (message) => {
if (!client.application?.owner) await client.application?.fetch();
//overwrites the application slash commands with data
if (message.content.toLowerCase() === '!8uy8983257983278t9ydhwehfieuwhufiwhu' && message.author.id === client.application?.owner.id) {
const command = await client.application?.commands.set(data);
message.react("783427378050629672").catch(e => console.log(e));
console.log(command);
}
//return if the message is not in a guild or made by bot
if (message.author.bot) return;
if (!message.guild) return;
if(message.content.startsWith("ratio") || message.content.endsWith("ratio")) {message.react("👍"); return;}
const prefixRegex = new RegExp(`^(<@!?${client.user.id}>|${escapeRegex(PREFIX)})\\s*`);
if (!prefixRegex.test(message.content)) return;
const [, matchedPrefix] = message.content.match(prefixRegex);
const args = message.content.slice(matchedPrefix.length).trim().split(/ +/);
const commandName = args.shift().toLowerCase();
const command =
client.commands.get(commandName) ||
client.commands.find((cmd) => cmd.aliases && cmd.aliases.includes(commandName));
if (!command) return;
if (!cooldowns.has(command.name)) {
cooldowns.set(command.name, new Collection());
}
const now = Date.now();
const timestamps = cooldowns.get(command.name);
const cooldownAmount = (command.cooldown || 1) * 1000;
if (timestamps.has(message.author.id)) {
const expirationTime = timestamps.get(message.author.id) + cooldownAmount;
if (now < expirationTime) {
const timeLeft = (expirationTime - now) / 1000;
return message.reply(
`please wait ${timeLeft.toFixed(1)} more second(s) before reusing the \`${command.name}\` command.`
);
}
}
timestamps.set(message.author.id, now);
setTimeout(() => timestamps.delete(message.author.id), cooldownAmount);
if(command.enabled === false) {
message.reply("This command is currently disabled.");
return;
}
try {
command.execute(message, args);
} catch (error) {
console.error(error);
message.reply("There was an error executing that command.").catch(console.error);
}
});
//Reaction add event
client.on("messageReactionAdd", async (reaction, user) => {
if(reactionMsgs.contains(reaction.message.id)) {
if(user.id === client.user.id) {return}
//Give sneakpeak role
if(reaction.message.id === "859205200279371806") {
client.commands.get("selfRole-sneakpeak").execute(reaction, user)
}
}
})
//Reaction remove event
client.on("messageReactionRemove", async(reaction, user) =>{
})
//Join message
client.on('guildMemberAdd', async member => {
const command =
client.commands.get("velkommen-member") ||
client.commands.find((cmd) => cmd.aliases && cmd.aliases.includes("velkommen-member"));
await member.guild.members.cache.get(member.id).roles.add(member.guild.roles.cache.get("668891845207785473"));
await command.execute(member);
})
// Slash commands
client.on('interactionCreate', async interaction => {
if (!interaction.isChatInputCommand()) return;
const interactCmd = interaction.commandName;
const command =
client.commands.get(interactCmd) ||
client.commands.find((cmd) => cmd.aliases && cmd.aliases.includes(interactCmd));
await command.interaction(interaction);
});
//Modal commands
client.on('interactionCreate', async interaction => {
if (!interaction.isButton()) return;
const interactCmd = interaction.customId;
const command =
client.commands.get(interactCmd) ||
client.commands.find((cmd) => cmd.aliases && cmd.aliases.includes(interactCmd));
await command.interaction(interaction);
});
//Modal input commands
client.on('interactionCreate', async interaction => {
if (!interaction.isModalSubmit()) return;
const interactCmd = interaction.customId;
const command =
client.commands.get(interactCmd) ||
client.commands.find((cmd) => cmd.aliases && cmd.aliases.includes(interactCmd));
await command.interaction(interaction);
});
const data = [
{
name: 'ping',
description: 'Checks Horsebot\'s response time to Discord',
},
{
name: 'wiki',
description: 'Displays wiki statistics',
},
{
name: 'horse',
description: 'Sends random image from thishorsedoesnotexist.com',
},
{
name: 'item',
description: 'Displays information about the Steam item',
options: [{
name: 'item',
type: 'STRING',
description: 'Item name or ItemDefID',
required: true,
},{
name: 'advanced_mode',
type: 'BOOLEAN',
description: 'Show advanced data',
required: false,
}],
},
{
name: 'inventory',
description: 'Displays user\'s Steam inventory using SteamId64',
options: [{
name: 'steamid64',
type: 'STRING',
description: 'Specify SteamId64 you want to get inventory data from',
required: true,
}],
},
{
name: 'inspect',
description: 'Inspect user\'s Steam inventory using SteamId64',
options: [{
name: 'steamid64',
type: 'STRING',
description: 'Specify SteamId64 you want to get inventory data from',
required: true,
}],
},
{
name: 'userinfo',
description: 'Displays information about the user',
options: [{
name: 'user',
type: 'USER',
description: 'User that you want info about (leave empty if you want info about yourself)',
required: false,
}],
},
{
name: 'suggest',
description: 'Add a suggestion to the suggestions channel',
options: [
{
name: 'platform',
type: 'STRING',
description: 'What platform do you want to add the suggestion to',
required: true,
choices: [
{
name: 'Stallion Squad',
value: 'stallion_squad',
},
{
name: 'Peekio Connect',
value: 'connect',
},
{
name: 'Discord',
value: 'discord',
},
],
},
{
name: 'suggestion',
type: 'STRING',
description: 'Write your suggestion here',
required: true,
}],
},
];