javascript 我的discord机器人在线,但没有得到任何互动

u3r8eeie  于 2023-06-20  发布在  Java
关注(0)|答案(1)|浏览(98)

我的Discord机器人是在线的,没有错误显示,但没有与“/”的任何交互。我已经确认了令牌和ID是正确的,因为当我在终端中启动“node.”时,我的机器人是在线的。但是,我不知道为什么没有互动。下面是机器人的代码:

  1. require('dotenv').config();
  2. const { Client, Collection } = require('discord.js');
  3. const { GatewayIntentBits } = require('discord-api-types/v9');
  4. const fs = require('fs');
  5. const client = new Client({
  6. intents: [
  7. GatewayIntentBits.Guilds,
  8. GatewayIntentBits.GuildMessages,
  9. GatewayIntentBits.GuildVoiceStates,
  10. ],
  11. });
  12. client.commands = new Collection();
  13. const commandFiles = fs
  14. .readdirSync('./commands')
  15. .filter(file => file.endsWith('.js'));
  16. // Add the new command here
  17. for (const file of commandFiles) {
  18. const command = require(`./commands/${file}`);
  19. if (command.data) {
  20. client.commands.set(command.data.name, command);
  21. }
  22. }
  23. client.once('ready', () => {
  24. console.log('Bot is ready!');
  25. });
  26. client.on('interactionCreate', async interaction => {
  27. if (!interaction.isCommand()) return;
  28. const command = client.commands.get(interaction.commandName);
  29. if (!command) return;
  30. try {
  31. console.log(`Executing command: ${interaction.commandName}`); // Debug statement
  32. await command.execute({ client, interaction });
  33. console.log(`Command executed successfully: ${interaction.commandName}`); // Debug statement
  34. } catch (error) {
  35. console.error(error);
  36. await interaction.reply({ content: 'An error occurred while executing the command.', ephemeral: true });
  37. }
  38. });
  39. client.login(process.env.TOKEN);

我也有package.json

  1. {
  2. "name": "musicbot",
  3. "version": "1.0.0",
  4. "description": "",
  5. "main": "index.js",
  6. "scripts": {
  7. "test": "echo \"Error: no test specified\" && exit 1"
  8. },
  9. "author": "",
  10. "license": "ISC",
  11. "dependencies": {
  12. "@discordjs/builders": "^1.6.3",
  13. "@discordjs/opus": "^0.9.0",
  14. "@discordjs/rest": "^1.7.1",
  15. "@discordjs/voice": "^0.16.0",
  16. "discord-api-types": "^0.37.45",
  17. "discord-player": "^6.5.0",
  18. "discord.js": "^14.11.0",
  19. "dotenv": "^16.1.4",
  20. "prism-media": "^1.3.5"
  21. }
  22. }

您还可以看到下面的play.js代码的命令:

  1. const { SlashCommandBuilder } = require("@discordjs/builders");
  2. const { MessageEmbed } = require("discord.js");
  3. const { QueryType } = require("discord-player");
  4. module.exports = {
  5. data: new SlashCommandBuilder()
  6. .setName("play")
  7. .setDescription("Plays a song.")
  8. .addSubcommand((subcommand) =>
  9. subcommand
  10. .setName("search")
  11. .setDescription("Searches for a song")
  12. .addStringOption((option) =>
  13. option
  14. .setName("searchterms")
  15. .setDescription("Search keywords")
  16. .setRequired(true)
  17. )
  18. )
  19. .addSubcommand((subcommand) =>
  20. subcommand
  21. .setName("playlist")
  22. .setDescription("Plays a playlist from YouTube")
  23. .addStringOption((option) =>
  24. option
  25. .setName("url")
  26. .setDescription("Playlist URL")
  27. .setRequired(true)
  28. )
  29. )
  30. .addSubcommand((subcommand) =>
  31. subcommand
  32. .setName("song")
  33. .setDescription("Plays a song from YouTube")
  34. .addStringOption((option) =>
  35. option
  36. .setName("url")
  37. .setDescription("Song URL")
  38. .setRequired(true)
  39. )
  40. ),
  41. async execute({ client, interaction }) {
  42. if (!interaction.member.voice.channel) {
  43. await interaction.reply("You need to be in a voice channel.");
  44. return;
  45. }
  46. const queue = client.player.createQueue(interaction.guild);
  47. try {
  48. if (!queue.connection) await queue.connect(interaction.member.voice.channel);
  49. } catch (error) {
  50. queue.destroy();
  51. await interaction.reply("Failed to join the voice channel.");
  52. return;
  53. }
  54. await interaction.deferReply();
  55. const subcommand = interaction.options.getSubcommand();
  56. if (subcommand === "song") {
  57. const url = interaction.options.getString("url");
  58. const track = await client.player
  59. .search(url, {
  60. requestedBy: interaction.user,
  61. searchEngine: QueryType.YOUTUBE_VIDEO,
  62. })
  63. .then((x) => x.tracks[0]);
  64. if (!track) {
  65. await interaction.editReply("No results found.");
  66. return;
  67. }
  68. queue.addTrack(track);
  69. await interaction.editReply(`Added **${track.title}** to the queue.`);
  70. } else if (subcommand === "playlist") {
  71. const url = interaction.options.getString("url");
  72. const tracks = await client.player
  73. .search(url, {
  74. requestedBy: interaction.user,
  75. searchEngine: QueryType.YOUTUBE_PLAYLIST,
  76. })
  77. .then((x) => x.tracks);
  78. if (!tracks.length) {
  79. await interaction.editReply("No tracks found in the playlist.");
  80. return;
  81. }
  82. queue.addTracks(tracks);
  83. await interaction.editReply(`Added ${tracks.length} tracks to the queue.`);
  84. } else if (subcommand === "search") {
  85. const searchTerms = interaction.options.getString("searchterms");
  86. const tracks = await client.player
  87. .search(searchTerms, {
  88. requestedBy: interaction.user,
  89. searchEngine: QueryType.AUTO,
  90. })
  91. .then((x) => x.tracks);
  92. if (!tracks.length) {
  93. await interaction.editReply("No results found.");
  94. return;
  95. }
  96. queue.addTrack(tracks[0]);
  97. await interaction.editReply(`Added **${tracks[0].title}** to the queue.`);
  98. }
  99. if (!queue.playing) await queue.play();
  100. },
  101. };
nwlqm0z1

nwlqm0z11#

  • 首先,您的bot必须具有在服务器中创建斜杠命令的权限:

转到discord developers portal-> OAuth2 -> URL Generator -然后检查是否检查了applications.commands范围,并使用新链接邀请服务器上的bot

  • 然后,您需要注册slashcommands -> https://discordjs.guide/creating-your-bot/command-deployment.html#command-registration

你的机器人应该把他的命令(全球或1个公会)不和谐显示给用户:

  1. [...]
  2. await rest.put(
  3. Routes.applicationCommands(clientId),
  4. { body: commands },
  5. );
  6. [...]

相关问题