Modalを使ってみよう
入力された文字列からメンバーを検索し、結果を出力するコード
Modalはスラッシュコマンド、コンテキストメニュー、ボタン、SelectMenuの.showModal()から表示可能。 code:js
const {
Client,
GatewayIntentBits,
ModalBuilder,
ActionRowBuilder,
TextInputBuilder,
SlashCommandBuilder,
REST,
Routes,
TextInputStyle,
EmbedBuilder
} = require("discord.js");
const client = new Client({
intents: [
//...
]
});
const token = process.env.token;
client.on("ready", async () => {
const rest = new REST({ version: '10' }).setToken(token);
await rest.put(
Routes.applicationCommands(client.user.id),
{
body: [
new SlashCommandBuilder()
.setName("user")
.setDescription("入力された文字列を名前に含むユーザーを検索します。")
]
}
);
});
client.on("interactionCreate", async (interaction) => {
if (interaction.isChatInputCommand()){
if (interaction.commandName == "user"){
if (!interaction.guild) return interaction.reply({
content: "サーバー内で実行してください。",
ephemeral: true
});
const modal = new ModalBuilder()
.setTitle("タイトル")
.setCustomId("user_submit");
const TextInput = new TextInputBuilder()
.setLabel("ユーザー名")
.setCustomId("name")
.setStyle(TextInputStyle.Short)
.setMaxLength(100)
.setMinLength(2)
.setRequired(true);
const ActionRow = new ActionRowBuilder().setComponents(TextInput);
modal.setComponents(ActionRow);
return interaction.showModal(modal);
};
} else if (interaction.isModalSubmit()){
if (interaction.customId == "user_submit"){
const name = interaction.fields.getTextInputValue("name");
const members = await interaction.guild.members.fetch();
const result = members.filter(member => member.displayName.includes(name));
if (result.size == 0) return interaction.reply({
content: ${name}で検索しましたが、見つかりませんでした。,
ephemeral: true
});
return interaction.reply({
embeds: [
new EmbedBuilder()
.setTitle(${name}の検索結果)
.setDescription(result.map(member => ${member.displayName}).join("\n"))
]
});
};
};
});
client.login(token);
code:js
const {
Client,
Intents,
Modal,
MessageActionRow,
TextInputComponent,
TextInputStyle,
MessageEmbed
} = require("discord.js");
const { SlashCommandBuilder } = require("@discordjs/builders");
const { REST } = require("@discordjs/rest");
const { Routes } = require("discord-api-types");
const client = new Client({
intents: [
//...
]
});
const token = process.env.token;
client.on("ready", async () => {
const rest = new REST({ version: '10' }).setToken(token);
await rest.put(
Routes.applicationCommands(client.user.id),
{
body: [
new SlashCommandBuilder()
.setName("user")
.setDescription("入力された文字列を名前に含むユーザーを検索します。")
]
}
);
});
client.on("interactionCreate", async (interaction) => {
if (interaction.isCommand()){
if (interaction.commandName === "user"){
if (!interaction.guild) return interaction.reply({
content: "サーバー内で実行してください。",
ephemeral: true
});
const modal = new Modal()
.setTitle("タイトル")
.setCustomId("user_submit");
const TextInput = new TextInputComponent()
.setLabel("ユーザー名")
.setCustomId("name")
.setStyle(TextInputStyle.SHORT)
.setMaxLength(100)
.setMinLength(2)
.setRequired(true);
const ActionRow = new MessageActionRow().setComponents(TextInput); return interaction.showModal(modal);
}
} else if (interaction.isModalSubmit()){
if (interaction.customId === "user_submit"){
const name = interaction.fields.getTextInputValue("name");
const members = await interaction.guild.members.fetch();
const result = members.filter(member => member.displayName.includes(name));
if (result.size == 0) return interaction.reply({
content: ${name}で検索しましたが、見つかりませんでした。,
ephemeral: true
});
return interaction.reply({
embeds: [
new MessageEmbed()
.setTitle(${name}の検索結果)
.setDescription(result.map(member => ${member.displayName}).join("\n"))
]
});
};
};
});
client.login(token);
関連