【问题标题】:Having trouble sending a message to a channel with Discord.js [duplicate]使用 Discord.js 向频道发送消息时遇到问题 [重复]
【发布时间】:2021-10-18 01:55:46
【问题描述】:

我正在尝试制作一个机器人,一旦用户发送特定消息,它就会向频道发送消息。我已经设法让它在机器人登录后发送一条消息,但 client.on() 函数不会做任何事情。如果我做错了什么,请告诉我,提前谢谢!

const { Client, Intents } = require("discord.js");

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

client.login("<bot token>");

client.once("ready", () => {
console.log("Ready!");

channel.send("hello world"); //This works

const guild = client.guilds.cache.get("<server id>");
const channel = guild.channels.cache.get("<channel id>");

//This is the issue. Nothing happens when I send "!ping" in the server
client.on("message", message => {
    if (message.content === "!ping") {
        channel.send("pong");
    }
});
});

【问题讨论】:

标签: javascript discord discord.js bots


【解决方案1】:

您需要启用GUILD_MESSAGES 意图:

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

这将使您能够接收在公会中发送的消息的MESSAGE_CREATE 事件。

可以在on the Discord developer docs找到完整的意图列表。

此外,如果您使用 Discord.js v13,the message event has been deprecated 因为它已重命名为 messageCreate

【讨论】:

    【解决方案2】:

    您没有使用GUILD_MESSAGES 意图。试试这个:

    
    const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
    
    client.login("<bot token>");
    
    client.once("ready", () => {
    console.log("Ready!");
    
    channel.send("hello world"); //This works
    
    const guild = client.guilds.cache.get("<server id>");
    const channel = guild.channels.cache.get("<channel id>");
    
    //This is the issue. Nothing happens when I send "!ping" in the server
    client.on("message", message => {
        if (message.content === "!ping") {
            channel.send("pong");
        }
    });
    });
    

    【讨论】:

      猜你喜欢
      • 2021-02-17
      • 1970-01-01
      • 2021-08-21
      • 2020-04-04
      • 2021-02-06
      • 1970-01-01
      • 1970-01-01
      • 2021-08-09
      相关资源
      最近更新 更多