This repository has been archived by the owner on Dec 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
132 lines (108 loc) · 4.21 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
// packages
const { Client, Intents, MessageEmbed, MessageActionRow, MessageButton } = require("discord.js");
const { REST } = require("@discordjs/rest");
const { Routes } = require("discord-api-types/v9");
const fs = require("node:fs");
require("dotenv").config();
const { addServer, removeServer, getLinks, getLimit, getUser, setUser } = require("./db.js");
// variables
//const intents = Object.values(Intents.FLAGS);
const intents = [Intents.FLAGS.GUILDS,Intents.FLAGS.GUILD_MESSAGES];
const client = new Client({ intents });
const commandFiles = fs.readdirSync("./commands").filter(file => file.endsWith(".js"));
// events
client.on("guildCreate", (guild) => {
updateSlashCommands(guild.id);
addServer(guild.id);
});
client.on("guildDelete", (guild) => {
removeServer(guild.id);
});
client.on("interactionCreate", async (interaction) => {
if (interaction.isCommand()) {
if (interaction.member.user.bot) return;
if(!interaction.member.permissions.has("ADMINISTRATOR")) return await interaction.reply({ content: "This command requires administrator permissions.", ephemeral: true });
const command = interaction.commandName.toLowerCase();
try {
await require(`./commands/${command}.js`).exec(client, interaction);
} catch {
return;
}
} else if (interaction.isButton()) {
if (interaction.member.user.bot) return;
const type = interaction.customId;
if (type.startsWith("__")) return;
// get information from database
let limit = await getLimit(interaction.guild.id);
if (!limit.status) return await interaction.reply({ content: limit.message, ephemeral: true });
limit = limit.data;
let user = await getUser(interaction.guild.id, interaction.member.user.id);
if (!user.status) return await interaction.reply({ content: user.message, ephemeral: true });
user = user.data;
if (user.count >= limit) return await interaction.reply({ content: `You have reached your limit of ${limit} links.`, ephemeral: true });
let links = await getLinks(interaction.guild.id, type);
if (!links.status) return await interaction.reply({ content: links.message, ephemeral: true });
links = links.data;
// randomly shuffle links
links = links.sort(() => Math.random() - 0.5);
// get random link
let link;
links.forEach((_link) => {
user.links[type] = user.links[type] || [];
if (user.links[type].includes(_link)) return;
if (link) return;
user.links[type].push(_link);
link = _link;
user.count++;
});
if (!link) return await interaction.reply({ content: "No links available.", ephemeral: true });
const member = client.users.cache.get(interaction.member.user.id);
// send message
const embed = new MessageEmbed()
.setTitle(`Proxy Bot - ${interaction.guild.name}`)
.addField("URL", link)
.addField("Type", type)
.addField("Remaining", `${limit - user.count}`)
.setTimestamp()
.setFooter({ text: `Proxy Bot - ${interaction.guild.name}`, iconURL: client.user.displayAvatarURL() });
try {
await member.send({ embeds: [embed] });
await interaction.reply({ content: "Check your DMs.", ephemeral: true });
} catch {
await interaction.reply({ content: "Failed to send message. Are your DMs off?", ephemeral: true });
}
// update database
await setUser(interaction.guild.id, interaction.member.user.id, user);
}
});
// update slash commands
client.on("ready", () => {
const guilds = client.guilds.cache.map(guild => guild.id);
console.log("Updating slash commands");
guilds.forEach((guildId) => {
updateSlashCommands(guildId);
});
console.log("Updated slash commands");
console.log(`Logged in as ${client.user.tag}!`);
});
// init
function updateSlashCommands (guildId) {
const commands = [];
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
commands.push(command.data.toJSON());
}
const rest = new REST({ version: "9" }).setToken(process.env.TOKEN);
(async () => {
try {
await rest.put(
Routes.applicationGuildCommands(client.user.id, guildId),
{ body: commands },
);
} catch (error) {
console.error(error);
}
})();
}
// login
client.login(process.env.TOKEN);