-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
156 lines (146 loc) · 6.22 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
const Discord = require('discord.js'),
{ readFileSync, writeFileSync } = require('fs'),
database = JSON.parse(readFileSync('./data.db')),
{ token, owners, color } = require('./config.json');
client = new Discord.Client({
intents: 3276799
});
client.login(token);
function writeDatabase() {
writeFileSync("./data.db", JSON.stringify(database));
}
client.on('ready', () => {
console.log(`[!] — Logged in as ${client.user.tag} (${client.user.id})`);
client.application.commands.set([
{
name: 'suggest',
description: 'Submit a new suggestion.',
options: [
{
name: 'image',
description: 'Add an image to your suggestion.',
type: 11
}
]
},
{
name: 'setchannel',
description: 'Set the channel where suggestions will be received.',
options: [
{
name: 'channel',
description: 'The channel where suggestions will be sent.',
type: 7,
required: true
}
]
}
])
});
client.on('interactionCreate', async interaction => {
if (!interaction.isChatInputCommand()) return;
if (!database.guilds) database.guilds = {}
const guildData = database.guilds[interaction.guildId];
if (interaction.commandName === 'suggest') {
if (!guildData) {
const embed = new Discord.EmbedBuilder()
.setTitle('`❌` ▸ Channel Not Configured')
.setDescription('*It appears that the suggestion channel hasn\'t been configured.*')
.setFooter({ text: interaction.user.username, iconURL: interaction.user.displayAvatarURL() })
.setColor(color)
.setTimestamp();
return interaction.reply({ embeds: [embed], ephemeral: true });
}
let modal = {
title: 'Suggestion',
custom_id: 'suggest-modal',
components: [
{
type: 1,
components: [
{
type: 4,
style: 1,
label: 'Title',
custom_id: 'title',
placeholder: 'Title of the suggestion...',
required: true,
max_length: 30
}
]
},
{
type: 1,
components: [
{
type: 4,
style: 2,
label: 'Description',
custom_id: 'description',
placeholder: 'Description of the suggestion...',
min_length: 10,
required: true
}
]
}
]
}
await interaction.showModal(modal);
const response = await interaction.awaitModalSubmit({ time: 120000 });
if (!response) return;
const title = response.fields.getTextInputValue('title'),
description = response.fields.getTextInputValue('description');
const embed = new Discord.EmbedBuilder()
.setTitle(`\`🪄\` ▸ ${title}`)
.setDescription(description)
.setAuthor({ name: interaction.user.username, iconURL: interaction.user.displayAvatarURL() })
.setFooter({ text: interaction.guild.name, iconURL: interaction.guild.iconURL() })
.setColor(color)
.setTimestamp();
const image = interaction.options.getAttachment('image');
if (image) {
embed.setImage(image.url);
}
await response.deferReply({ fetchReply: true, ephemeral: true });
await interaction.guild.channels.cache.get(guildData).send({ embeds: [embed] }).then((message) => {
message.react('✅');
message.react('❌');
const embed = new Discord.EmbedBuilder()
.setTitle('`🪄` ▸ Suggestion Been Sent')
.setDescription('*Your suggestion has been sent.*')
.setFooter({ text: interaction.user.username, iconURL: interaction.user.displayAvatarURL() })
.setColor(color)
.setTimestamp();
return response.editReply({ embeds: [embed], ephemeral: true });
}).catch(() => {
const embed = new Discord.EmbedBuilder()
.setTitle('`❌` ▸ An error occurred')
.setDescription('*An error occurred while sending your suggestion.*')
.setFooter({ text: interaction.user.username, iconURL: interaction.user.displayAvatarURL() })
.setColor(color)
.setTimestamp();
return response.editReply({ embeds: [embed], ephemeral: true });
})
} else if (interaction.commandName === 'setchannel') {
if (!owners.includes(interaction.user.id)) {
const embed = new Discord.EmbedBuilder()
.setTitle('`❌` ▸ Unauthorized User')
.setDescription('*You are not authorized to use this command.*')
.setFooter({ text: interaction.user.username, iconURL: interaction.user.displayAvatarURL() })
.setColor(color)
.setTimestamp();
return interaction.reply({ embeds: [embed], ephemeral: true });
}
const channel = interaction.options.getChannel('channel');
if (![5, 4, 0].includes(channel.type)) return;
database.guilds[interaction.guildId] = channel.id;
writeDatabase();
const embed = new Discord.EmbedBuilder()
.setTitle('`🪄` ▸ Channel Been Defined')
.setDescription('*The suggestion channel has been defined.*')
.setFooter({ text: interaction.user.username, iconURL: interaction.user.displayAvatarURL() })
.setColor(color)
.setTimestamp();
return interaction.reply({ embeds: [embed], ephemeral: true });
}
});