-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
185 lines (158 loc) · 6.42 KB
/
bot.js
File metadata and controls
185 lines (158 loc) · 6.42 KB
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
const { Client, GatewayIntentBits, EmbedBuilder, ActionRowBuilder, StringSelectMenuBuilder, REST, Routes } = require('discord.js');
const fs = require('fs');
const path = require('path');
// Đọc config
function loadConfig() {
const configPath = path.join(__dirname, 'config.json');
const data = fs.readFileSync(configPath, 'utf8');
return JSON.parse(data);
}
// Khởi tạo bot
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages
]
});
// Đăng ký slash command
async function registerCommands() {
const commands = [
{
name: 'menu',
description: 'Hiển thị menu chọn'
}
];
const rest = new REST({ version: '10' }).setToken(process.env.DISCORD_TOKEN);
try {
console.log('Đang đăng ký slash commands...');
await rest.put(
Routes.applicationGuildCommands(client.user.id, process.env.GUILD_ID),
{ body: commands }
);
console.log('Đăng ký slash commands thành công!');
} catch (error) {
console.error('Lỗi khi đăng ký commands:', error);
}
}
// Khi bot sẵn sàng
client.once('ready', async () => {
console.log(`Bot đã đăng nhập: ${client.user.tag}`);
await registerCommands();
});
// Xử lý slash command
client.on('interactionCreate', async (interaction) => {
if (interaction.isChatInputCommand()) {
if (interaction.commandName === 'menu') {
const config = loadConfig();
// Tạo embed chính với đầy đủ tính năng
const mainEmbedData = config.mainEmbed;
const embed = new EmbedBuilder()
.setTitle(mainEmbedData.title)
.setDescription(mainEmbedData.description)
.setColor(mainEmbedData.color);
if (mainEmbedData.thumbnail) {
embed.setThumbnail(mainEmbedData.thumbnail);
}
if (mainEmbedData.image) {
embed.setImage(mainEmbedData.image);
}
if (mainEmbedData.author && mainEmbedData.author.name) {
const authorData = { name: mainEmbedData.author.name };
if (mainEmbedData.author.icon_url) authorData.iconURL = mainEmbedData.author.icon_url;
if (mainEmbedData.author.url) authorData.url = mainEmbedData.author.url;
embed.setAuthor(authorData);
}
if (mainEmbedData.footer && mainEmbedData.footer.text) {
const footerData = { text: mainEmbedData.footer.text };
if (mainEmbedData.footer.icon_url) footerData.iconURL = mainEmbedData.footer.icon_url;
embed.setFooter(footerData);
}
if (mainEmbedData.fields && mainEmbedData.fields.length > 0) {
mainEmbedData.fields.forEach(field => {
if (field.name && field.value) {
embed.addFields({
name: field.name,
value: field.value,
inline: field.inline || false
});
}
});
}
// Tạo select menu
const selectMenu = new StringSelectMenuBuilder()
.setCustomId('menu_select')
.setPlaceholder('Chọn một mục...');
config.menuItems.forEach(item => {
const option = {
label: item.label,
value: item.value,
description: item.description
};
if (item.emoji) {
option.emoji = item.emoji;
}
selectMenu.addOptions(option);
});
const row = new ActionRowBuilder().addComponents(selectMenu);
await interaction.reply({
embeds: [embed],
components: [row]
});
}
}
// Xử lý khi user chọn từ menu
if (interaction.isStringSelectMenu()) {
if (interaction.customId === 'menu_select') {
const selectedValue = interaction.values[0];
const config = loadConfig();
const selectedItem = config.menuItems.find(item => item.value === selectedValue);
if (selectedItem && selectedItem.content) {
const content = selectedItem.content;
// Tạo embed chi tiết
const detailEmbed = new EmbedBuilder()
.setTitle(content.title)
.setDescription(content.description)
.setColor(content.color);
// Thêm thumbnail
if (content.thumbnail) {
detailEmbed.setThumbnail(content.thumbnail);
}
// Thêm image
if (content.image) {
detailEmbed.setImage(content.image);
}
// Thêm author
if (content.author && content.author.name) {
const authorData = { name: content.author.name };
if (content.author.icon_url) authorData.iconURL = content.author.icon_url;
if (content.author.url) authorData.url = content.author.url;
detailEmbed.setAuthor(authorData);
}
// Thêm footer
if (content.footer && content.footer.text) {
const footerData = { text: content.footer.text };
if (content.footer.icon_url) footerData.iconURL = content.footer.icon_url;
detailEmbed.setFooter(footerData);
}
// Thêm fields
if (content.fields && content.fields.length > 0) {
content.fields.forEach(field => {
if (field.name && field.value) {
detailEmbed.addFields({
name: field.name,
value: field.value,
inline: field.inline || false
});
}
});
}
await interaction.reply({
embeds: [detailEmbed],
ephemeral: true
});
}
}
}
});
// Export client để web server có thể truy cập
module.exports = client;