我的Discord机器人是在线的,没有错误显示,但没有与“/”的任何交互。我已经确认了令牌和ID是正确的,因为当我在终端中启动“node.”时,我的机器人是在线的。但是,我不知道为什么没有互动。下面是机器人的代码:
require('dotenv').config();
const { Client, Collection } = require('discord.js');
const { GatewayIntentBits } = require('discord-api-types/v9');
const fs = require('fs');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildVoiceStates,
],
});
client.commands = new Collection();
const commandFiles = fs
.readdirSync('./commands')
.filter(file => file.endsWith('.js'));
// Add the new command here
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
if (command.data) {
client.commands.set(command.data.name, command);
}
}
client.once('ready', () => {
console.log('Bot is ready!');
});
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
console.log(`Executing command: ${interaction.commandName}`); // Debug statement
await command.execute({ client, interaction });
console.log(`Command executed successfully: ${interaction.commandName}`); // Debug statement
} catch (error) {
console.error(error);
await interaction.reply({ content: 'An error occurred while executing the command.', ephemeral: true });
}
});
client.login(process.env.TOKEN);
我也有package.json
:
{
"name": "musicbot",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"@discordjs/builders": "^1.6.3",
"@discordjs/opus": "^0.9.0",
"@discordjs/rest": "^1.7.1",
"@discordjs/voice": "^0.16.0",
"discord-api-types": "^0.37.45",
"discord-player": "^6.5.0",
"discord.js": "^14.11.0",
"dotenv": "^16.1.4",
"prism-media": "^1.3.5"
}
}
您还可以看到下面的play.js
代码的命令:
const { SlashCommandBuilder } = require("@discordjs/builders");
const { MessageEmbed } = require("discord.js");
const { QueryType } = require("discord-player");
module.exports = {
data: new SlashCommandBuilder()
.setName("play")
.setDescription("Plays a song.")
.addSubcommand((subcommand) =>
subcommand
.setName("search")
.setDescription("Searches for a song")
.addStringOption((option) =>
option
.setName("searchterms")
.setDescription("Search keywords")
.setRequired(true)
)
)
.addSubcommand((subcommand) =>
subcommand
.setName("playlist")
.setDescription("Plays a playlist from YouTube")
.addStringOption((option) =>
option
.setName("url")
.setDescription("Playlist URL")
.setRequired(true)
)
)
.addSubcommand((subcommand) =>
subcommand
.setName("song")
.setDescription("Plays a song from YouTube")
.addStringOption((option) =>
option
.setName("url")
.setDescription("Song URL")
.setRequired(true)
)
),
async execute({ client, interaction }) {
if (!interaction.member.voice.channel) {
await interaction.reply("You need to be in a voice channel.");
return;
}
const queue = client.player.createQueue(interaction.guild);
try {
if (!queue.connection) await queue.connect(interaction.member.voice.channel);
} catch (error) {
queue.destroy();
await interaction.reply("Failed to join the voice channel.");
return;
}
await interaction.deferReply();
const subcommand = interaction.options.getSubcommand();
if (subcommand === "song") {
const url = interaction.options.getString("url");
const track = await client.player
.search(url, {
requestedBy: interaction.user,
searchEngine: QueryType.YOUTUBE_VIDEO,
})
.then((x) => x.tracks[0]);
if (!track) {
await interaction.editReply("No results found.");
return;
}
queue.addTrack(track);
await interaction.editReply(`Added **${track.title}** to the queue.`);
} else if (subcommand === "playlist") {
const url = interaction.options.getString("url");
const tracks = await client.player
.search(url, {
requestedBy: interaction.user,
searchEngine: QueryType.YOUTUBE_PLAYLIST,
})
.then((x) => x.tracks);
if (!tracks.length) {
await interaction.editReply("No tracks found in the playlist.");
return;
}
queue.addTracks(tracks);
await interaction.editReply(`Added ${tracks.length} tracks to the queue.`);
} else if (subcommand === "search") {
const searchTerms = interaction.options.getString("searchterms");
const tracks = await client.player
.search(searchTerms, {
requestedBy: interaction.user,
searchEngine: QueryType.AUTO,
})
.then((x) => x.tracks);
if (!tracks.length) {
await interaction.editReply("No results found.");
return;
}
queue.addTrack(tracks[0]);
await interaction.editReply(`Added **${tracks[0].title}** to the queue.`);
}
if (!queue.playing) await queue.play();
},
};
1条答案
按热度按时间nwlqm0z11#
转到discord developers portal-> OAuth2 -> URL Generator -然后检查是否检查了
applications.commands
范围,并使用新链接邀请服务器上的bot你的机器人应该把他的命令(全球或1个公会)不和谐显示给用户: