-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteraction.js
112 lines (101 loc) · 5.12 KB
/
interaction.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
const { Events, EmbedBuilder, ModalBuilder, TextInputBuilder, TextInputStyle, ActionRowBuilder } = require('discord.js');
const { suggestionChannelId } = require('../config.json'); // 提案を送信するチャンネルIDを設定
module.exports = {
name: Events.InteractionCreate,
async execute(interaction) {
const client = interaction.client;
if (interaction.isCommand()) {
const command = client.commands.get(interaction.commandName);
if (!command) {
await interaction.reply({ content: '不明なコマンドです。', ephemeral: true });
return;
}
try {
await command.execute(interaction);
} catch (error) {
console.error('コマンド実行中にエラーが発生しました:', error);
await interaction.reply({ content: 'エラーが発生しました。', ephemeral: true });
}
}
// 提案ボタンがクリックされたときにモーダルを表示
else if (interaction.isButton() && interaction.customId === 'openSuggestionModal') {
const modal = new ModalBuilder()
.setCustomId('suggestionModal')
.setTitle('サーバー及びBotへの提案');
const suggestionInput = new TextInputBuilder()
.setCustomId('suggestionInput')
.setLabel('提案内容を入力してください。')
.setStyle(TextInputStyle.Paragraph)
.setRequired(true);
const actionRow = new ActionRowBuilder().addComponents(suggestionInput);
modal.addComponents(actionRow);
try {
await interaction.showModal(modal);
} catch (error) {
console.error('モーダルの表示中にエラーが発生しました:', error);
await interaction.reply({ content: 'モーダルを表示中にエラーが発生しました。', ephemeral: true });
}
}
// モーダルの入力が送信されたとき
else if (interaction.isModalSubmit() && interaction.customId === 'suggestionModal') {
const suggestion = interaction.fields.getTextInputValue('suggestionInput');
// 提案内容をEmbedで作成
const suggestionEmbed = new EmbedBuilder()
.setColor(0x0000ff)
.setTitle('新しい提案が届きました。')
.setDescription(suggestion)
.addFields(
{ name: '提案者', value: `<@${interaction.user.id}>`, inline: true },
{ name: '提案日時', value: new Date().toLocaleString(), inline: true }
)
.setTimestamp();
try {
// 指定チャンネルに提案を送信
const channel = await client.channels.fetch(suggestionChannelId);
if (channel) {
await channel.send({ embeds: [suggestionEmbed] });
await interaction.reply({ content: '提案が送信されました。', ephemeral: true });
} else {
await interaction.reply({ content: '提案を送信するチャンネルが見つかりませんでした。', ephemeral: true });
}
} catch (error) {
console.error('提案を送信中にエラーが発生しました:', error);
await interaction.reply({ content: '提案を送信する際にエラーが発生しました。', ephemeral: true });
}
}
// 認証ボタンがクリックされたとき
else if (interaction.isButton() && interaction.customId.startsWith('verify_button_')) {
const roleId = interaction.customId.split('_')[2];
const role = interaction.guild.roles.cache.get(roleId);
if (!role) {
await interaction.reply({
content: '指定されたロールが見つかりません。',
ephemeral: true,
});
return;
}
// ユーザーがすでにロールを持っているか確認
if (interaction.member.roles.cache.has(role.id)) {
await interaction.reply({
content: 'すでにこのロールが付与されています!',
ephemeral: true,
});
return;
}
try {
// ロールをユーザーに付与
await interaction.member.roles.add(role);
await interaction.reply({
content: '認証が完了し、ロールが付与されました!',
ephemeral: true,
});
} catch (error) {
console.error('ロールの付与中にエラーが発生しました:', error);
await interaction.reply({
content: 'ロールを付与する際にエラーが発生しました。',
ephemeral: true,
});
}
}
}
};