我正在尝试制作一个显示“/help”等命令的机器人,但 /help 根本不起作用,并且没有显示。
我多次尝试给予机器人不同的权限并重新邀请机器人。我改变了一些东西,但不知道问题可能是什么。
我不知道该怎么做,因为没有任何效果,我不知道这是否是我的机器人。我的节点版本是正确的
// Import required modules
const { Client, GatewayIntentBits, EmbedBuilder, PermissionsBitField, Permissions } = require('discord.js');
const { ButtonBuilder, ButtonStyle, ComponentType, ActionRowBuilder } = require('discord.js');
const { SlashCommandBuilder } = require('@discordjs/builders');
const { MessageActionRow, MessageButton } = require('discord.js');
// Create a new Discord client
const client = new Client({
intents: [
GatewayIntentBits.Guilds, // Enable intents for guilds
GatewayIntentBits.GuildMessages, // Enable intents for guild messages
GatewayIntentBits.MessageContent, // Enable intents for message content
GatewayIntentBits.GuildMessageReactions, // Enable intents for guild message reactions
],
});
// Set the bot token
const token = "Bot Token"
// When the client is ready, log the bot's username to the console
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}`);
});
// Define the help command
const helpCommand = new SlashCommandBuilder()
.setName('help')
.setDescription('Get help with various issues.');
// Handle interactionCreate event
client.on('interactionCreate', async (interaction) => {
if (!interaction.isCommand() || interaction.commandName !== 'help') return;
// Check if the command was used in a guild channel
if (interaction.channel) {
// Check if the command was used in the expected channel
if (interaction.channelId !== 'Channel ID') {
await interaction.reply({ content: 'This command is not available in this channel.', ephemeral: true });
return;
}
}
// Create the buttons as raw JSON components
const buttonsRow1 = [
{
type: 2, // Button type
style: 3, // Button style (SUCCESS)
label: 'Ban Info',
custom_id: 'ban-button'
},
{
type: 2,
style: 3, // Button style (SUCCESS)
label: 'Tools Status',
custom_id: 'first-button'
},
{
type: 2,
style: 1, // Button style (PRIMARY)
label: 'Program Not Running',
custom_id: 'second-button'
},
{
type: 2,
style: 1, // Button style (PRIMARY)
label: 'Failed Error',
custom_id: 'third-button'
}
];
const buttonsRow2 = [
{
type: 2,
style: 1, // Button style (PRIMARY)
label: 'Cracked',
custom_id: 'fourth-button'
},
{
type: 2,
style: 1, // Button style (PRIMARY)
label: 'DLC Cars',
custom_id: 'fifth-button'
},
{
type: 2,
style: 1, // Button style (PRIMARY)
label: 'Credits',
custom_id: 'sixth-button'
},
{
type: 2,
style: 1, // Button style (PRIMARY)
label: 'Add XP',
custom_id: 'seventh-button'
}
];
const buttonsRow3 = [
{
type: 2,
style: 1, // Button style (PRIMARY)
label: 'Add Wheel-spins',
custom_id: 'eighth-button'
},
{
type: 2,
style: 1, // Button style (PRIMARY)
label: 'Disable AC Error',
custom_id: 'ninth-button'
},
{
type: 2,
style: 1, // Button style (PRIMARY)
label: 'AIO V2 Virus?',
custom_id: 'tenth-button'
},
{
type: 2,
style: 2, // Button style (SECONDARY)
label: 'Exit',
custom_id: 'exit-button'
}
];
// Send a reply with buttons
await interaction.reply({
content: 'Help Command Recognized\nIf you\'re having an issue or want a solution, click on one of the buttons to get an answer for your question.',
components: [
{
type: 1, // Action row type
components: buttonsRow1
},
{
type: 1,
components: buttonsRow2
},
{
type: 1,
components: buttonsRow3
}
],
ephemeral: true
});
// Handle button clicks
const collector = interaction.channel?.createMessageComponentCollector({ time: 60000 });
collector?.on('collect', async (buttonInteraction) => {
if (buttonInteraction.customId === 'ban-button') {
await buttonInteraction.update({ content: 'Ban Info button clicked', ephemeral: true });
} else if (buttonInteraction.customId === 'first-button') {
await buttonInteraction.update({ content: 'Tools Status button clicked', ephemeral: true });
} else if (buttonInteraction.customId === 'second-button') {
await buttonInteraction.update({ content: 'Program Not Running button clicked', ephemeral: true });
} else if (buttonInteraction.customId === 'third-button') {
await buttonInteraction.update({ content: 'Failed Error button clicked', ephemeral: true });
} else if (buttonInteraction.customId === 'fourth-button') {
await buttonInteraction.update({ content: 'Cracked button clicked', ephemeral: true });
} else if (buttonInteraction.customId === 'fifth-button') {
await buttonInteraction.update({ content: 'DLC Cars button clicked', ephemeral: true });
} else if (buttonInteraction.customId === 'sixth-button') {
await buttonInteraction.update({ content: 'Credits button clicked', ephemeral: true });
} else if (buttonInteraction.customId === 'seventh-button') {
await buttonInteraction.update({ content: 'Add XP button clicked', ephemeral: true });
} else if (buttonInteraction.customId === 'eighth-button') {
await buttonInteraction.update({ content: 'Add Wheel-spins button clicked', ephemeral: true });
} else if (buttonInteraction.customId === 'ninth-button') {
await buttonInteraction.update({ content: 'Disable AC Error button clicked', ephemeral: true });
} else if (buttonInteraction.customId === 'tenth-button') {
await buttonInteraction.update({ content: 'AIO V2 Virus? button clicked', ephemeral: true });
} else if (buttonInteraction.customId === 'exit-button') {
await buttonInteraction.update({ content: 'Exiting...', ephemeral: true });
collector.stop(); // Stop the collector
client.application.commands.create(helpCommand);
}
});
// Handle the end of the collector
collector?.on('end', () => {
interaction.editReply({ content: 'You ran out of time, try again.', components: [] })
.then(() => {
interaction.deleteReply();
})
.catch(console.error);
});
});
client.login(token)
您必须注册斜杠命令才能使用它们。
DiscordJS 指南有关于如何执行此操作的丰富资源。这里的完整答案有点太复杂了。