search function

Can the discord bot use the discord search function? For example, from the user:, after:, in the channel: Can you write an example code? I will be grateful! My code below works, though not exactly as it should, but if, for example, there are about 10 thousand messages in the channel, then it will take a very long time(
const START_DATE = new Date('2024-12-11');

async function fetchAllMessages(channelId, userId) {
const channel = await interaction.client.channels.fetch(channelId);
let allMessages = [];
let lastMessageId = null;

while (true) {
const options = { limit: 100 };
if (lastMessageId) {
options.before = lastMessageId;
}

const fetchedMessages = await channel.messages.fetch(options);
const messagesArray = Array.from(fetchedMessages.values());
const userMessages = messagesArray.filter(msg => msg.createdAt >= START_DATE && msg.author.id === userId);
allMessages = allMessages.concat(userMessages);

if (fetchedMessages.size < 100 || userMessages.length === 0) {
break;
}

lastMessageId = messagesArray[messagesArray.length - 1].id;
}

return allMessages;
}
const START_DATE = new Date('2024-12-11');

async function fetchAllMessages(channelId, userId) {
const channel = await interaction.client.channels.fetch(channelId);
let allMessages = [];
let lastMessageId = null;

while (true) {
const options = { limit: 100 };
if (lastMessageId) {
options.before = lastMessageId;
}

const fetchedMessages = await channel.messages.fetch(options);
const messagesArray = Array.from(fetchedMessages.values());
const userMessages = messagesArray.filter(msg => msg.createdAt >= START_DATE && msg.author.id === userId);
allMessages = allMessages.concat(userMessages);

if (fetchedMessages.size < 100 || userMessages.length === 0) {
break;
}

lastMessageId = messagesArray[messagesArray.length - 1].id;
}

return allMessages;
}
3 Replies
d.js toolkit
d.js toolkit2w ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
treble/luna
treble/luna2w ago
No bots do not have access to that your code also is massive api spam so dont do that
pal3st1na
pal3st1naOP2w ago
Oh, well, thank you!
Want results from more Discord servers?
Add your server