forked from wagumi/5000
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
125 lines (117 loc) · 3.28 KB
/
server.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
const fs = require("fs");
const locker = require("node-file-lock");
const { Client, Intents } = require("discord.js");
const http = require("http");
http.createServer(function(req, res) {
res.write("OK");
res.end();
}).listen(8080);
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES],
partials: ["MESSAGE", "CHANNEL"],
});
client.once("ready", async () => {
const data = require("./commands/slashCommand.json");
const commands = await client.guilds.cache
.get(process.env.SERVER_ID)
.commands.set(data);
const commandPermissions = require("./commands/commandPermission.json");
commands.map(async (command) => {
console.log(command.id, command.name);
await client.guilds.cache
.get(process.env.SERVER_ID)
.commands.permissions.add({
command: command.id,
permissions: commandPermissions,
});
});
console.log("Ready!");
});
client.on("interactionCreate", async (interaction) => {
try {
if (!interaction.isCommand()) {
return;
}
const { commandName } = interaction;
//get_poap
if (commandName === "get_poap") {
try {
const userId = interaction.user.id;
const url = getMintUrl(userId);
await interaction.reply({
content: url,
ephemeral: true,
});
} catch (e) {
console.log(e);
await interaction.reply({
content: `ERROR:${JSON.stringify(e)}`,
ephemeral: true,
});
}
//reset_mint
} else if (commandName === "reset_mint") {
try {
const userId = interaction.user.id;
deleteMintUrl(userId);
await interaction.reply({
content: "MINT has been reset.",
ephemeral: true,
});
} catch (e) {
console.log(e);
}
//reset_user
} else if (commandName === "reset_user") {
try {
const userId = interaction.options.getUser("user").id;
deleteMintUrl(userId);
await interaction.reply({
content: "MINT has been reset.",
ephemeral: true,
});
} catch (e) {
console.log(e);
}
//ping
} else if (commandName === "ping") {
await interaction.reply({
content: "pong",
ephemeral: true,
});
}
} catch (e) {
console.log(e);
}
});
const deleteMintUrl = (userId) => {
const lock = new locker("locked.bin");
const users = JSON.parse(fs.readFileSync("./minted.json"));
delete users[userId];
fs.writeFileSync("./minted.json", JSON.stringify(users, null, 2));
lock.unlock();
console.log(`${userId} reset`);
};
const getMintUrl= (userId) => {
let url = "";
const lock = new locker("locked.bin");
const users = JSON.parse(fs.readFileSync("./minted.json"));
if (users[userId]) {
url = users[userId];
console.log(`${userId} ${url} Got Already`);
} else {
const str = fs.readFileSync("./list.txt", "utf-8");
const urls = str.split("\n");
url = urls.shift();
users[userId] = url;
fs.writeFileSync("./list.txt", urls.join("\n"));
fs.writeFileSync("./minted.json", JSON.stringify(users, null, 2));
console.log(`${userId} ${url}`);
}
lock.unlock();
return url;
};
(async () => {
console.log("login...");
await client.login(process.env.DISCORD_BOT_TOKEN);
})();