【问题标题】:Discord.js don't execute client.on("message")Discord.js 不执行 client.on("message")
【发布时间】:2021-11-12 20:49:38
【问题描述】:

我正在尝试使用 discord.jsNodeJS 构建 Discord 机器人,但我遇到了问题。当我运行代码时,dicord.js 不执行 client.on("message"),他只是跳过这部分并转到代码的末尾,就像您在下图:

index.js

    const { Client, Intents } = require('discord.js');
const { prefix, token } = require("./config.json");
require('dotenv').config();

const client = new Client({ intents: [Intents.FLAGS.GUILDS] });

const queue = new Map();

client.once("ready", c => {
  console.log(`???? [${c.user.tag}] Running...`);
  client.user.setActivity(`${prefix}play`, { type: "PLAYING" })
});

client.once("reconnecting", c => {
  console.log(`???? [${c.user.tag}] Reconnecting...`);
});

client.once("disconnect", c => {
  console.log(`[${c.user.tag}] Disconnect!`);
});


client.on("message", async message => {
  console.log("I'm here!")
  if (message.author.bot) return;
  if (!message.content.startsWith(prefix)) return;

  const serverQueue = queue.get(message.guild.id);

  if (message.content.startsWith(`${prefix}play`)) {
    execute(message, serverQueue);
    return;
  } else if (message.content.startsWith(`${prefix}skip`)) {
    skip(message, serverQueue);
    return;
  } else if (message.content.startsWith(`${prefix}stop`)) {
    stop(message, serverQueue);
    return;
  } else {
    message.channel.send("You need to enter a valid command!");
  }
});

async function execute(message, serverQueue) {
  const args = message.content.split(" ");

  const voiceChannel = message.member.voice.channel;
  if (!voiceChannel)
    return message.channel.send(
      "You need to be in a voice channel to play music!"
    );
  const permissions = voiceChannel.permissionsFor(message.client.user);
  if (!permissions.has("CONNECT") || !permissions.has("SPEAK")) {
    return message.channel.send(
      "I need the permissions to join and speak in your voice channel!"
    );
  }

  const songInfo = await ytdl.getInfo(args[1]);
  const song = {
        title: songInfo.videoDetails.title,
        url: songInfo.videoDetails.video_url,
   };

  if (!serverQueue) {
    const queueContruct = {
      textChannel: message.channel,
      voiceChannel: voiceChannel,
      connection: null,
      songs: [],
      volume: 5,
      playing: true
    };

    queue.set(message.guild.id, queueContruct);

    queueContruct.songs.push(song);

    try {
      var connection = await voiceChannel.join();
      queueContruct.connection = connection;
      play(message.guild, queueContruct.songs[0]);
    } catch (err) {
      console.log(err);
      queue.delete(message.guild.id);
      return message.channel.send(err);
    }
  } else {
    serverQueue.songs.push(song);
    return message.channel.send(`${song.title} has been added to the queue!`);
  }
}

function skip(message, serverQueue) {
  if (!message.member.voice.channel)
    return message.channel.send(
      "You have to be in a voice channel to stop the music!"
    );
  if (!serverQueue)
    return message.channel.send("There is no song that I could skip!");
  serverQueue.connection.dispatcher.end();
}

function stop(message, serverQueue) {
  if (!message.member.voice.channel)
    return message.channel.send(
      "You have to be in a voice channel to stop the music!"
    );
    
  if (!serverQueue)
    return message.channel.send("There is no song that I could stop!");
    
  serverQueue.songs = [];
  serverQueue.connection.dispatcher.end();
}

function play(guild, song) {
  const serverQueue = queue.get(guild.id);
  if (!song) {
    serverQueue.voiceChannel.leave();
    queue.delete(guild.id);
    return;
  }

  const dispatcher = serverQueue.connection
    .play(ytdl(song.url))
    .on("finish", () => {
      serverQueue.songs.shift();
      play(guild, serverQueue.songs[0]);
    })
    .on("error", error => console.error(error));
  dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
  serverQueue.textChannel.send(`Start playing: **${song.title}**`);
}

client.login(token);

你能帮忙解决这个问题吗?

【问题讨论】:

  • client.on 是一个事件监听器。它只会在您收到“消息”事件时执行。您是否收到一条消息,但它仍然不会被执行?
  • 值得注意的是,the message event is deprecated 在最新版本的 Discord.js 中。

标签: javascript node.js discord discord.js bots


【解决方案1】:

虽然message 事件确实已被弃用,但您在使用它时仍会收到进程警告。

主要问题是您没有为公会消息添加意图,您的机器人无法看到消息。将Intents.FLAGS.GUILD_MESSAGES 添加到您的客户意图中。

const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });

【讨论】:

  • 谢谢,这个解决方案解决了我的问题!
【解决方案2】:

对于我在他们的documentation 上看到的内容,并且根据@esqew 的评论,他们弃用了消息事件。 你应该试试client.on("messageCreate")

【讨论】:

    猜你喜欢
    • 2021-06-10
    • 2021-12-03
    • 2021-01-21
    • 1970-01-01
    • 2022-10-31
    • 1970-01-01
    • 1970-01-01
    • 2021-10-18
    • 1970-01-01
    相关资源
    最近更新 更多