-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
287 lines (233 loc) · 10.3 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
const { Client, RichEmbed } = require("discord.js");
const fs = require("fs");
const follow = require("./services/follow");
const subscribe = require("./services/subscribe");
const config = require("./config.json");
const tokens = require("./tokens.json");
const txtToArray = async (path) => {
try {
const array = await fs.promises
.readFile(path)
.then(buffer => buffer
.toString()
.trim()
.split(/\r?\n|\r/g)
.filter(el => el.length > 0)
);
return Promise.resolve(array);
} catch (error) {
return Promise.reject(error);
}
}
const client = new Client();
client.on("ready", () => console.log(`Working on ${client.user.tag}`));
client.on("message", async (message) => {
if (!message.guild) return;
if (!message.content.startsWith(config.prefix)) return;
const [command, ...args] = message.content.slice(config.prefix.length).trim().split(/ +/g);
if (!tokens[message.author.id]) {
tokens[message.author.id] = {
tokens: 0
};
}
if (command === "add") {
if (message.author.id !== config.userid) return message.reply("you can't use this command!");
const [key, tokensToAdd] = args;
if (!key) return message.reply("you must provide a key!");
if (!tokensToAdd) return message.reply("you must provide the tokens for this key!");
if (isNaN(tokensToAdd)) return message.reply("the tokens to add must be an integer!");
try {
const exists = await fs.promises.readFile(`./keys/${key}.json`).catch(() => false);
if (exists) return message.reply(`\`${key}\` already exists!`);
await fs.promises.writeFile(`./keys/${key}.json`, tokensToAdd);
} catch (error) {
throw error;
}
message.channel.send(
new RichEmbed()
.setAuthor("Key created!", message.author.avatarURL)
.setColor("GREEN")
.addField("Key", key, true)
.addField("Tokens", tokensToAdd, true)
)
} else if (command === "redeem") {
message.delete().catch(() => void 0);
const [key] = args;
if (!key) return message.reply("you must provide a key!");
try {
const tokensFromKey = await fs.promises
.readFile(`./keys/${key}.json`)
.then(buffer => Number(buffer.toString().trim()))
.catch(() => null);
if (!tokensFromKey) return message.reply(`\`${key}\` is an invalid key!`);
if (!tokens[message.author.id]) tokens[message.author.id] = { tokens: 5000 };
tokens[message.author.id].tokens += tokensFromKey;
message.channel.send(
new RichEmbed()
.setAuthor("Success!", message.author.avatarURL)
.setColor("GREEN")
.setDescription(`✅ Key redeemed!\n${tokensFromKey} tokens added to your account!`)
);
await Promise.all([
fs.promises.writeFile("./tokens.json", JSON.stringify(tokens, null, 2)),
fs.promises.unlink(`./keys/${key}.json`)
]);
} catch (error) {
throw error;
}
} else if (command === "tokens") {
message.reply(`you currently have ${tokens[message.author.id].tokens} token${tokens[message.author.id].tokens === 1 ? "" : "s"}`);
} else if (command === "sub") {
message.delete().catch(() => void 0);
const [channel, tokensToSpend] = args;
if (!channel) return message.reply("you must provide the channel to target!");
if (!tokensToSpend) return message.reply("you must provide the amount of tokens you want to use!");
if (isNaN(tokensToSpend)) return message.reply("`tokens` has to be an integer!");
const amount = parseInt(tokensToSpend);
if (amount > tokens[message.author.id].tokens) return message.reply("you do not have enough tokens!");
let subsGiven = 0;
try {
let subTokens = await txtToArray("./tokens/subs.txt");
if (!subTokens.length) return message.channel.send(`Sorry ${message.author.toString()}, we have ran out of stock!`);
if (subTokens.length < amount) return message.channel.send(`Sorry ${message.author.toString()}, we do not have \`${amount}\` subscribers in stock!`);
message.reply("order has been created!");
client.channels
.find(channel => channel.name === "orders")
.send(
new RichEmbed()
.setAuthor("Order Created!", message.author.avatarURL)
.setColor("GREEN")
.addField("Type", "Sub", false)
.addField("Channel", channel, true)
.addField("Amount", amount, true)
.setFooter("Ordered by: " + message.author.tag)
.setTimestamp()
)
.catch(() => void 0);
while (subsGiven < amount) {
if (!subTokens.length) {
message.channel.send(`Sorry ${message.author.toString()}, we have ran out of stock! We could only provide you with **${subsGiven}** subscribers!`);
break;
}
try {
const response = await subscribe(subTokens[0], channel);
if (!response.text.includes(`"errors":[{`) && !response.text.includes(`"error":[{`) && !response.text.includes(`"error":{`)) subsGiven++;
} catch (error) {
} finally {
subTokens = subTokens.slice(1);
}
}
tokens[message.author.id].tokens -= subsGiven;
if (subsGiven >= amount) message.reply(`we have successfully provided you with **${subsGiven}** subscribers!`);
await Promise.all([
fs.promises.writeFile("./tokens.json", JSON.stringify(tokens, null, 2)),
fs.promises.writeFile("./tokens/subs.txt", subTokens.join("\n"))
]);
} catch (error) {
throw error;
}
} else if (command === "follow") {
message.delete().catch(() => void 0);
const channel = args[0];
const tokensToSpend = parseFloat(args[1]);
if (!channel) return message.reply("you must provide the channel to target!");
if (!tokensToSpend) return message.reply("you must provide the amount of tokens you want to use!");
if (isNaN(tokensToSpend)) return message.reply("`tokens` has to be an integer!");
const followersToGive = tokensToSpend * 1;
if (tokensToSpend > tokens[message.author.id].tokens) return message.reply("you do not have enough tokens!");
const usedTokens = [];
let followersGiven = 100;
try {
const followerTokens = await txtToArray("./tokens/follows.txt");
const usedFollowerTokens = await txtToArray("./used_tokens/follows.txt");
let availableTokens = followerTokens.filter(token => !usedFollowerTokens.includes(token));
if (!availableTokens.length) return message.channel.send(`Sorry ${message.author.toString()}, we have ran out of followers!`);
if (availableTokens.length < followersToGive) return message.channel.send(`Sorry ${message.author.toString()}, we do not have ${amount} followers in stock!`);
message.reply("order has been created!");
client.channels
.find(channel => channel.name === "orders")
.send(
new RichEmbed()
.setAuthor("Order Created!", message.author.avatarURL)
.setColor("GREEN")
.addField("Type", "Follow", true)
.addField("Channel", channel, true)
.addField("Amount", followersToGive, true)
.setFooter("Ordered by: " + message.author.tag)
.setTimestamp()
)
.catch(() => void 0);
while (followersGiven < followersToGive) {
if (!availableTokens.length) {
message.channel.send(`Sorry ${message.author.toString()}, we have ran out of stock! We could only provide you with **${followersGiven}** followers!`);
break;
}
try {
const response = await follow(availableTokens[0], channel);
if (
!response.text.includes(`"errors":[{`) &&
!response.text.includes(`"error":[{`) &&
!response.text.includes(`"error":{`)
) {
followersGiven++;
}
} catch (error) {
} finally {
usedTokens.push(availableTokens[0]);
availableTokens = availableTokens.slice(1);
}
}
tokens[message.author.id].tokens -= parseFloat((followersGiven / 100).toFixed(2));
usedFollowerTokens.push(...usedTokens)
if (followersGiven >= followersToGive) message.reply(`we have successfully provided you with **${followersGiven}** followers!`);
await Promise.all([
fs.promises.writeFile("./tokens.json", JSON.stringify(tokens, null, 2)),
fs.promises.writeFile("./tokens/follows.txt", availableTokens.join("\n")),
fs.promises.writeFile("./used_tokens/follows.txt", usedFollowerTokens.join("\n"))
]);
} catch (error) {
throw error;
}
} else if (command === "stock") {
try {
const subTokens = await txtToArray("./tokens/subs.txt");
const followerTokens = await txtToArray("./tokens/follows.txt");
const usedFollowerTokens = await txtToArray("./used_tokens/follows.txt");
const availableFollowTokens = followerTokens.filter(token => !usedFollowerTokens.includes(token));
message.channel.send(
new RichEmbed()
.setAuthor("Stock", message.author.avatarURL)
.setColor("ORANGE")
.setDescription(`\`${subTokens.length}\` subscriber(s) available\n\`${availableFollowTokens.length}\` follower(s) available`)
);
} catch (error) {
throw error;
}
} else if (command === "help") {
const commands = [
"follow (channel) (amount)",
"sub (channel) (amount)",
"redeem (license",
"stock",
"tokens"
];
message.channel.send(
new RichEmbed()
.setAuthor("Help", message.author.avatarURL)
.setColor("ORANGE")
.setFooter("Powered by 3メのりひ丂りノムノイ丂ムレ")
.setDescription(commands.map(cmd => `${config.prefix}${cmd}`).join("\n"))
);
}
});
const activities_list = [
"Watching",
"You're Tokens..**Drool**"
]; // creates an arraylist containing phrases you want your bot to switch through.
client.on('ready', () => {
setInterval(() => {
const index = Math.floor(Math.random() * (activities_list.length - 1) + 1); // generates a random number between 1 and the length of the activities array list (in this case 5).
client.user.setActivity(activities_list[index]); // sets bot's activities to one of the phrases in the arraylist.
}, 10000); // Runs this every 10 seconds.
});
client.login(config.token);