-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
82 lines (57 loc) · 2.59 KB
/
index.js
File metadata and controls
82 lines (57 loc) · 2.59 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
const { Client, GatewayIntentBits, EmbedBuilder, StringSelectMenuBuilder, StringSelectMenuOptionBuilder, ActionRowBuilder } = require('discord.js');
const config = require("./config.json")
const client = new Client({
intents: [
GatewayIntentBits.Guilds
]
});
client.once('ready', async () => {
console.log(`Logged in as ${client.user.tag}`);
const channel = await client.channels.fetch(config.channel);
if (!channel) {
console.error('指定されたチャンネルが見つかりません。');
return;
}
const embed = new EmbedBuilder()
.setTitle('ロール選択')
.setDescription('以下のドロップダウンから選択してロールを選択してください。')
.setColor('#0099ff');
const selectMenu = new StringSelectMenuBuilder()
.setCustomId('role_select')
.setPlaceholder('ロールを選択してください')
.addOptions(
Object.entries(config.mapping).map(([key, roleId]) => (
new StringSelectMenuOptionBuilder()
.setLabel(key)
.setValue(roleId)
.setDescription(`${key} ロールを付与/剥奪します`)
))
);
const row = new ActionRowBuilder().addComponents(selectMenu);
await channel.send({ embeds: [embed], components: [row] });
});
client.on('interactionCreate', async interaction => {
if (!interaction.isStringSelectMenu()) return;
if (interaction.customId === 'role_select') {
const selectedRoleId = interaction.values[0];
const member = interaction.member;
const role = interaction.guild.roles.cache.get(selectedRoleId);
if (!role) {
await interaction.reply({ content: '指定されたロールが見つかりません。', ephemeral: true });
return;
}
try {
if (member.roles.cache.has(selectedRoleId)) {
await member.roles.remove(role);
await interaction.reply({ content: `${role.name} ロールを剥奪しました!`, ephemeral: true });
} else {
await member.roles.add(role);
await interaction.reply({ content: `${role.name} ロールを付与しました!`, ephemeral: true });
}
} catch (error) {
console.error(error);
await interaction.reply({ content: 'ロールの操作に失敗しました。ボットの権限を確認してください。', ephemeral: true });
}
}
});
client.login(config.token);