使用 Discord.js 向频道发送消息时遇到问题

Having trouble sending a message to a channel with Discord.js(使用 Discord.js 向频道发送消息时遇到问题)
本文介绍了使用 Discord.js 向频道发送消息时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

I'm trying to make a bot that sends a message to a channel once a user sends a specific message. I've managed to make it send a message once the bot logs in, but the client.on() function won't do anything. Please let me know if I'm doing something wrong, thank you in advance!

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");
    }
});
});

推荐答案

您需要启用 GUILD_MESSAGES Intent:

You need to enable the GUILD_MESSAGES intent:

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

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

This will enable you to receive the MESSAGE_CREATE event for messages sent in guilds.

可以在 Discord 开发者文档中找到完整的意图列表.

A full list of intents can be found on the Discord developer docs.

此外,如果您使用 Discord.js v13,message 事件已被弃用,因为它已重命名为 messageCreate.

Additionally, if you are using Discord.js v13, the message event has been deprecated as it has been renamed to messageCreate.

这篇关于使用 Discord.js 向频道发送消息时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Using discord.js to detect image and respond(使用 discord.js 检测图像并响应)
Check if user ID exists in Discord server(检查 Discord 服务器中是否存在用户 ID)
Guild Member Add does not work (discordjs)(公会成员添加不起作用(discordjs))
Creating my first bot using REPLIT but always error Discord.JS(使用 REPLIT 创建我的第一个机器人,但总是错误 Discord.JS)
How do I code event/command handlers for my Discord.js bot?(如何为我的 Discord.js 机器人编写事件/命令处理程序?)
How to find a User ID from a Username in Discord.js?(如何从 Discord.js 中的用户名中查找用户 ID?)