gpt4 book ai didi

javascript - 如何让我的机器人获取已经从 channel 中的用户发送的随机消息,并在触发自动响应时发送该随机消息

转载 作者:行者123 更新时间:2023-11-29 20:27:22 27 4
gpt4 key购买 nike

正如标题所说,我尝试使用 fetchMessages 来检查某个 channel 发送的消息,但是当我试图弄乱它时,它只会在控制台中给我无休止的信息 block 。

我尝试阅读它,但其中似乎没有实际消息,只是提供有关服务器的信息,例如 channel ID、成员(member) ID、他们的权限等。

起初我以为使用这种方法会给我随机消息,所以我可以让机器人将消息直接发送到一个 channel ,但它给了我一个错误,所以我只是使用 console.log 来获取更多信息。但事实并非如此,或者我只是用错了......

这是我正在尝试的代码:

    bot.on('message', msg =>{

if(msg.author.bot) return;
lowerCase = msg.content.toLowerCase();

if(lowerCase.includes ("fetch")){
msg.channel.fetchMessages({limit: 1}).then(console.log);
}
})

显然它也无法跟踪旧消息,因为当我尝试使用用户 ID 过滤消息时,它显示他们发送了 0 条消息,即使他们之前一直在发送消息?我正在努力实现它仍然可以读取非常旧的消息,并且在我使用自动回复消息时仍然可以发送这些消息。

最佳答案

TextChannel.fetchMessages() 一次最多只能获取 100 条消息

Short Answer:

Although it doesn't show in the docs, if you fetch more than 100, you'll get an API error:

DiscordAPIError: Invalid Form Body
limit: int value should be less than or equal to 100.

Console logging the Message object will indeed give you a whole lot of information regarding the Guild and everything else in it. You should probably want to only console log the message's content and author id/usertag

So if you want to search beyond the past 100 messages, you'll have to continuously fetch 100 messages by using the ID of the oldest message in ChannelLogsQueryOptions's before parameter in each consecutive TextChannel.fetchMessages() call, and then concatinating each Collection together using Collection.concat().


长答案:

例如,如果你想获取过去的 200 条消息,你可以这样写:

// using .then()
await channel.fetchMessages({limit: 100})
.then(first100 => {
let next100 = await channel.fetchMessages({limit: 100, before: first100.lastKey()})
.then(next100 => {
let all200 = first100.concat(next100);
});
});
// in async function
let first100 = await channel.fetchMessages({limit: 100});
let next100 = await channel.fetchMessages({limit: 100, before: first100.last().id});
let all200 = first100.concat(next100);

看看this answer对于将过去的消息存储在数组中的异步函数。

我制作了自己的版本,它会给你一个 Collection并将其作为适当的 Promise 返回相反:

const fetchManyMessages = (channel, limit = 200) => {
return new Promise((resolve, reject) => {
channel.fetchMessages({limit: limit < 100 ? limit : 100})
.then(collection => {
const nextBatch = () => {
console.log(collection.size);
let remaining = limit - collection.size;

channel.fetchMessages({limit: remaining<100 ? remaining : 100, before: collection.lastKey()})
.then(next => {
let concatenated = collection.concat(next);

// resolve when limit is met or when no new msgs were added (reached beginning of channel)
if (collection.size >= limit || collection.size == concatenated.size) return resolve(concatenated);

collection = concatenated;
nextBatch();
})
.catch(error => reject(error));
}

nextBatch();
})
.catch(error => reject(error));
});
}

示例用法:

fetchManyMessages(message.channel, 1500)
.then(msgs => console.log(msgs.size))
.catch(err => console.log(err));

在这里我还制作了一个 prototype版本,我认为它在某些语法上很棒:

Discord.TextChannel.prototype.fetchManyMessages = function (limit = 200) {
return new Promise((resolve, reject) => {
this.fetchMessages({limit: limit < 100 ? limit : 100})
.then(collection => {
const nextBatch = () => {
console.log(collection.size);
let remaining = limit - collection.size;

this.fetchMessages({limit: remaining<100 ? remaining : 100, before: collection.lastKey()})
.then(next => {
let concatenated = collection.concat(next);

// resolve when limit is met or when no new msgs were added (reached beginning of channel)
if (collection.size >= limit || collection.size == concatenated.size) return resolve(concatenated);

collection = concatenated;
nextBatch();
})
.catch(error => reject(error));
}

nextBatch();
})
.catch(error => reject(error));
});
}

示例用法:

message.channel.fetchManyMessages(1500)
.then(msgs => console.log(msgs.size))
.catch(err => console.log(err));

关于javascript - 如何让我的机器人获取已经从 channel 中的用户发送的随机消息,并在触发自动响应时发送该随机消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58973108/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com