首页 > 解决方案 > 响应 discord.js 中的消息

问题描述

我想向某人发送以 s# 开头的消息的频道发送消息“E”。我有两个问题:

  1. 我有一个功能:
function read(msg)
{
  if(msg[0] === "s" && msg[1] === "#") console.log("E");
}

而且我不知道如何使用某人作为“msg”发送的消息来调用此函数。

  1. 当我弄清楚如何做到这一点时,我想添加一个channel.send("E");,但我不知道如何获取消息发送到的频道的 ID。

标签: javascriptdiscorddiscord.js

解决方案


client.on('message', message => {
    if(message.content.startsWith('s#')) console.log('s#');//check if the message starts with s#
});
message.channel.send('this is a reply to the message sent in this channel');

请参阅文档:https ://discord.js.org/#/docs/main/stable/class/Client?scrollTo=e-message

编辑(带有读取功能的整个代码):

const Discord = require('discord.js');
const client = new Discord.Client();

client.on('message', message => {
    read(message);
});

function read(message){
   if(message.content.startsWith('s#')) console.log('s#');
}

client.login('your-token-here');

推荐阅读