forked from treasurealts/Discord-V13-Generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
221 lines (196 loc) · 8.98 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
const {Client, Intents, MessageEmbed} = require("discord.js");
const dotenv = require("dotenv");
const util = require("util");
const fs = require("fs");
const keep_alive = require('./keep_alive.js');
//init dotenv
dotenv.config();
let discord_token = process.env.DISCORD_TOKEN; //Discord Bot Token (env: DISCORD_TOKEN)
let discord_guild = process.env.DISCORD_GUILD_ID; //Discord Guild ID (ID of you discord server) (env: DISCORD_GUILD)
let account_dir = __dirname+"/accounts/";
//Data for all commands
const command_data = {
'generate': {
name: 'generate',
description: 'Generate a random account',
options: [{
name: 'service',
type: 'STRING',
description: 'Name of account you want to generate',
required: true
}]
},
'stock': {
name: 'stock',
description: 'Check account stock'
},
'add': {
name: 'add',
description: 'Add a random account',
options: [
{
name: 'service',
type: 'STRING',
description: 'Name of account service (Minecraft, Netflix)',
required: true
},
{
name: 'account',
type: 'STRING',
description: 'Account (email:password)',
required: true
}
]
},
'help': {
name: 'help',
description: 'View all commands for generator',
}
}
//Set bot intents
const bot = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES
]
});
const create_embed = (content) => {
const embed = new MessageEmbed()
.setColor('#009ad6')
.setTitle('Account Generator')
.setDescription("**"+content+"**");
return embed;
}
const capitalize = (s) => {
if (typeof s !== 'string') return ''
return s.charAt(0).toUpperCase() + s.slice(1)
}
//check for on ready event
bot.on('ready', async () => {
console.log("The bot is online");
//Create commands
const generate_command = await bot.guilds.cache.get(discord_guild)?.commands.create(command_data['generate']);
const stock_command = await bot.guilds.cache.get(discord_guild)?.commands.create(command_data['stock']);
const add_command = await bot.guilds.cache.get(discord_guild)?.commands.create(command_data['add']);
const help_command = await bot.guilds.cache.get(discord_guild)?.commands.create(command_data['help']);
});
bot.on('interactionCreate', async interaction => {
if(interaction.isCommand){
let command_name = (interaction.commandName).toLowerCase();
if(command_name === "generate"){
try{
let generate_service = (interaction.options.getString("service")).toLowerCase();
let generate_file = account_dir+generate_service+".json";
if(fs.existsSync(generate_file)){
fs.readFile(generate_file, "utf8", async function(err, data){
if(err){
throw err;
}
if(data && data.length){
account_array = JSON.parse(data);
account_array_len = account_array.length;
account_rand = Math.floor(Math.random() * account_array_len);
account_name = capitalize(generate_service);
account_val = account_array[account_rand];
if(account_val){
const reply_embed = create_embed("✅ Check your messages for the account");
const dm_embed = create_embed("Your "+account_name+" account:\n\n"+account_val);
let user_full = interaction.user.username+"#"+interaction.user.discriminator;
interaction.user.send({embeds: [dm_embed]}).catch(() => {console.log("Can't DM user: %d", user_full)});
await interaction.reply({embeds: [reply_embed], ephemeral: true});
}
else{
const embed = create_embed("⚠️ No stock available");
await interaction.reply({embeds: [embed], ephemeral: true});
}
}
else{
const embed = create_embed("⚠️ No stock available");
await interaction.reply({embeds: [embed], ephemeral: true});
}
});
}
else{
const embed = create_embed("⚠️ This account service does not exist");
await interaction.reply({embeds: [embed], ephemeral: true});
}
}
catch(err){
console.log(err);
}
}
else if(command_name === "stock"){
try{
fs.readdir(account_dir, 'utf-8', async (err, data) => {
let stock_msg = "";
for (const file of data){
const accounts = await util.promisify(fs.readFile)(account_dir+file, 'utf8');
const clean_name = file.substring(0, file.length - 5);
accounts_len = JSON.parse(accounts).length;
stock_msg += capitalize(clean_name)+": "+accounts_len+"\n";
}
const embed = create_embed(stock_msg);
interaction.reply({embeds: [embed], ephemeral: true})
});
}
catch(err){
const embed = create_embed("⚠️ Sorry, can't fetch stock right now");
interaction.reply({embeds: [embed], ephemeral: true})
console.log(err);
}
}
else if(command_name === "add"){
if(interaction.member.permissions.has("ADMINISTRATOR")){
try{
let generate_service = (interaction.options.getString("service")).toLowerCase();
let generate_account = (interaction.options.getString("account"));
let generate_file = account_dir+generate_service+".json";
if(fs.existsSync(generate_file)){
fs.readFile(generate_file, "utf8", async function(err, data){
if(err){
throw err;
}
if(data && data.length){
let account_array = JSON.parse(data);
account_array.push(generate_account);
account_array_json = JSON.stringify(account_array);
fs.writeFile(generate_file, account_array_json, () => {
const embed = create_embed("✅ This account has been added");
interaction.reply({embeds: [embed], ephemeral: true});
});
}
else{
let account_array_json = JSON.stringify([generate_account]);
fs.writeFile(generate_file, account_array_json, () => {
const embed = create_embed("✅ New service has been created and account added");
interaction.reply({embeds: [embed], ephemeral: true});
});
}
});
}
else{
let account_array_json = JSON.stringify([generate_account]);
fs.writeFile(generate_file, account_array_json, () => {
const embed = create_embed("✅ New service has been created and account added");
interaction.reply({embeds: [embed], ephemeral: true});
});
}
}
catch(err){
console.log(err);
}
}
else{
const embed = create_embed("⚠️ Only admin's can use this command");
interaction.reply({embeds: [embed], ephemeral: true});
}
}
else if(command_name === "help"){
let help_txt = "\n/generate <service>\n/stock\n/add <service> <account>\n/help";
const embed = create_embed(help_txt);
interaction.reply({embeds: [embed], ephemeral: true});
}
}
});
//Log bot in with token
bot.login(discord_token);