![](/img/trans.png)
[英]How can I make my discord bot edit its own last message after data is ready
[英]How can I make my bot find the last message that matches a criteria, e.x ("joe") and send who sent it?
我正在为我的机器人使用 node.js,并且我像问题所述一样,需要在频道中找到与代码中指定的条件匹配的最后一条消息。 例如,命令会说,?joe,它会找到最后一条包含“joe”的消息,并返回发送它的成员。 在我要使用它的服务器中,消息非常频繁,所以我认为它不会遇到任何消息阅读限制。
我在 main.js 中编写了代码,在单独的 js 文件中编写了命令,就像这样,
const Discord = require('discord.js');
const client = new Discord.Client();
const prefix = '?'
const fs = require('fs');
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandFiles){
const command = require(`./commands/${file}`);
client.commands.set(command.name, command)
}
client.once('ready', () => {
console.log('AbidBot is online!!');
});
client.on('message', message =>{
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command === 'ping'){
client.commands.get('ping').execute(message, args);
}
});
client.login();
ping 命令如下所示:
module.exports = {
name: 'ping',
description: "this is a ping command!",
execute(message, args){
message.channel.send('pong!')
}
}
谢谢你的帮助!
编辑 #1谢谢,但它似乎对我 @Lioness100 不起作用。 你能看看吗?
module.exports = {
name: 'joe',
description: "find joe",
execute(message, args){
message.channel.messages.fetch().then((messages) => {
// find the message that has 'joe' in it
const author = messages.find({ content } => content.includes(' joe ')).author.tag;
message.channel.send(author);
}
});
}
您可以使用MessageManager.fetch()
和Collection.find()
// fetch the last 100 messages in the channel
message.channel.messages.fetch({ limit: 100 }).then((messages) => {
// find the message that has 'joe' in it
const msg = messages.find(({ content }) => content.includes(' joe '));
return msg
? message.channel.send(msg)
: message.channel.send('None of the last 100 messages include the word `joe`!');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.