我想向某人发送以s#开头的消息的通道发送消息“ E”。我有2个问题:
java -jar <program_name>.jar --<some_parameter> <some_filename>.csv
我不知道如何用某人以“ msg”形式发送的消息来调用此函数。
function read(msg)
{
if(msg[0] === "s" && msg[1] === "#") console.log("E");
}
,但是我不知道如何获取消息发送到的通道的ID。答案 0 :(得分:0)
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');
查看文档:{{3}}
编辑(带有读取功能的整个代码):
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');
答案 1 :(得分:0)
如果要使用读取功能,有两种方法
如@SickBoy所说,
function read(msg){
if(msg.content.startsWith('s#'){
return true;
}
else{
return false;
}
}
否则,
function read(msg){
const data = msg.content.split('')
if(data[0] === 's' && data[1] === '#'){
return true;
}
else{
return false;
}
}
要调用此函数,只需添加一个消息事件。
client.on('message' , message=> {
const res = read(message)
if(res){
//The message starts with s#
}
else{
//It doesn't start with s#
}
})
答案 2 :(得分:0)
有一个名为message
的事件,只要有人在服务器中发送消息,该事件就会发出。此事件返回一个message
对象,该对象包含有关该消息的所有信息,例如内容,作者,频道,成员等。因此,作为回复发送一些内容,您可以执行以下操作:
client.on('message', message => {
if (message.content.startsWith('s#')) {
message.channel.send('E');
}
}
此外,我看到您正在ready
函数msg[0]
中执行此操作。这不是您访问message
对象的内容的方式。它不是数组。您可以使用let text = message.content
获取消息的内容。此文本为字符串类型,并存储消息文本。