BestSpark687090
BestSpark687090
DIAdiscord.js - Imagine an app
Created by BestSpark687090 on 8/18/2024 in #djs-questions
Attempting to react to messages older than bot does not work
I'm trying to make my bot react with an emoji to all messages that are in a server. However, the reactions do go back by one day, but don't go farther than that. Here's the code:
const { Client, Events, GatewayIntentBits, Partials, TextChannel } = require("discord.js")
const env = require("dotenv")
env.config()

// Create a new client instance
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent],
partials: [ Partials.Channel, Partials.Message ] });
client.once(Events.ClientReady, async (readyClient) => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
let server = await client.guilds.fetch("1155619424020213801");
let channels = await server.channels.fetch(); // Get ALL channels
for (let channel of channels){
if (channel[1].type == 4){continue}
let messages = await fetchAllMessages(channel[1],server)
//const filteredMessages = messages.filter(m => m.reactions.cache.me == false);
let i = 0;
messages.forEach(function(e){
e.react("🍉");
i++;
})
console.log("Finished with channel " + channel[1].name+" and count "+i)
}
console.log("Finished reacting.")
});

// Log in to Discord with your client's token
client.login(process.env.token);

async function fetchAllMessages(id,server) {
const channel = id
if (channel.size == 38){
console.log("Defaulted?")
return []
}
let messages = [];
let message = channel.messages.fetch({ limit: 1 })
.then(messagePage => (messagePage.size === 1 ? messagePage.at(0) : null));
while (message) {
await channel.messages
.fetch({ limit: 100, before: message.id })
.then(messagePage => {
messagePage.forEach(msg => messages.push(msg));
message = 0 < messagePage.size ? messagePage.at(messagePage.size - 1) : null;
});
}

return messages // Print all messages
}
const { Client, Events, GatewayIntentBits, Partials, TextChannel } = require("discord.js")
const env = require("dotenv")
env.config()

// Create a new client instance
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent],
partials: [ Partials.Channel, Partials.Message ] });
client.once(Events.ClientReady, async (readyClient) => {
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
let server = await client.guilds.fetch("1155619424020213801");
let channels = await server.channels.fetch(); // Get ALL channels
for (let channel of channels){
if (channel[1].type == 4){continue}
let messages = await fetchAllMessages(channel[1],server)
//const filteredMessages = messages.filter(m => m.reactions.cache.me == false);
let i = 0;
messages.forEach(function(e){
e.react("🍉");
i++;
})
console.log("Finished with channel " + channel[1].name+" and count "+i)
}
console.log("Finished reacting.")
});

// Log in to Discord with your client's token
client.login(process.env.token);

async function fetchAllMessages(id,server) {
const channel = id
if (channel.size == 38){
console.log("Defaulted?")
return []
}
let messages = [];
let message = channel.messages.fetch({ limit: 1 })
.then(messagePage => (messagePage.size === 1 ? messagePage.at(0) : null));
while (message) {
await channel.messages
.fetch({ limit: 100, before: message.id })
.then(messagePage => {
messagePage.forEach(msg => messages.push(msg));
message = 0 < messagePage.size ? messagePage.at(messagePage.size - 1) : null;
});
}

return messages // Print all messages
}
5 replies
DIAdiscord.js - Imagine an app
Created by BestSpark687090 on 12/22/2022 in #djs-questions
How to get most recent messages from channel
I'm trying to have my bot get the most recent message of the channel, but it's returning undefined. How would i do that?
7 replies
DIAdiscord.js - Imagine an app
Created by BestSpark687090 on 12/22/2022 in #djs-questions
ExpectValidationError expected
I'm getting a weird error message from the sapphire node module. Here's the message:
C:\Users\esmit\source\repos\Visual-Studio-Code-Projects\gamebot\node_modules\@sapphire\shapeshift\dist\index.js:41
throw this.error;
^

ExpectedValidationError: Expected
at InstanceValidator.handle (C:\Users\esmit\source\repos\Visual-Studio-Code-Projects\gamebot\node_modules\@sapphire\shapeshift\dist\index.js:714:75)
at InstanceValidator.parse (C:\Users\esmit\source\repos\Visual-Studio-Code-Projects\gamebot\node_modules\@sapphire\shapeshift\dist\index.js:201:88)
at assertReturnOfBuilder (C:\Users\esmit\source\repos\Visual-Studio-Code-Projects\gamebot\node_modules\@discordjs\builders\dist\index.js:942:53)
at MixedClass._sharedAddOptionMethod (C:\Users\esmit\source\repos\Visual-Studio-Code-Projects\gamebot\node_modules\@discordjs\builders\dist\index.js:1347:5)
at MixedClass.addStringOption (C:\Users\esmit\source\repos\Visual-Studio-Code-Projects\gamebot\node_modules\@discordjs\builders\dist\index.js:1335:17)
at Object.<anonymous> (C:\Users\esmit\source\repos\Visual-Studio-Code-Projects\gamebot\command.js:7:10)
at Module._compile (node:internal/modules/cjs/loader:1155:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1209:10)
at Module.load (node:internal/modules/cjs/loader:1033:32)
at Function.Module._load (node:internal/modules/cjs/loader:868:12) {
validator: 's.instance(V)',
given: undefined,
expected: [Function: SlashCommandStringOption]
}
C:\Users\esmit\source\repos\Visual-Studio-Code-Projects\gamebot\node_modules\@sapphire\shapeshift\dist\index.js:41
throw this.error;
^

ExpectedValidationError: Expected
at InstanceValidator.handle (C:\Users\esmit\source\repos\Visual-Studio-Code-Projects\gamebot\node_modules\@sapphire\shapeshift\dist\index.js:714:75)
at InstanceValidator.parse (C:\Users\esmit\source\repos\Visual-Studio-Code-Projects\gamebot\node_modules\@sapphire\shapeshift\dist\index.js:201:88)
at assertReturnOfBuilder (C:\Users\esmit\source\repos\Visual-Studio-Code-Projects\gamebot\node_modules\@discordjs\builders\dist\index.js:942:53)
at MixedClass._sharedAddOptionMethod (C:\Users\esmit\source\repos\Visual-Studio-Code-Projects\gamebot\node_modules\@discordjs\builders\dist\index.js:1347:5)
at MixedClass.addStringOption (C:\Users\esmit\source\repos\Visual-Studio-Code-Projects\gamebot\node_modules\@discordjs\builders\dist\index.js:1335:17)
at Object.<anonymous> (C:\Users\esmit\source\repos\Visual-Studio-Code-Projects\gamebot\command.js:7:10)
at Module._compile (node:internal/modules/cjs/loader:1155:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1209:10)
at Module.load (node:internal/modules/cjs/loader:1033:32)
at Function.Module._load (node:internal/modules/cjs/loader:868:12) {
validator: 's.instance(V)',
given: undefined,
expected: [Function: SlashCommandStringOption]
}
13 replies
DIAdiscord.js - Imagine an app
Created by BestSpark687090 on 12/16/2022 in #djs-questions
How do I edit a message using message.edit()?
Currently, here's my code:
// Require the necessary discord.js classes
const { Client, Events, GatewayIntentBits } = require('discord.js');
const dotenv = require('dotenv')
const repl = require('repl')
const commands = require('./commands')
dotenv.config()
var message;
// Create a new client instance
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
let channel;
// When the client is ready, run this code (only once)
// We use 'c' for the event parameter to keep it separate from the already defined 'client'
client.once(Events.ClientReady, c => {
channel = client.channels.cache.get('1053340012021821593');
});
client.login(process.env.token);
const prompt = repl.start({
prompt: "Message to send: ",
eval: function(cmd,context,filename,callback){
message = channel.send(cmd)
this.displayPrompt()
}
})
prompt.defineCommand('test',{
help: "Test command",
action(newMessage){
message.edit(newMessage)
.then()
.catch(console.log(e))
},
})
// Log in to Discord with your client's token
// Require the necessary discord.js classes
const { Client, Events, GatewayIntentBits } = require('discord.js');
const dotenv = require('dotenv')
const repl = require('repl')
const commands = require('./commands')
dotenv.config()
var message;
// Create a new client instance
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
let channel;
// When the client is ready, run this code (only once)
// We use 'c' for the event parameter to keep it separate from the already defined 'client'
client.once(Events.ClientReady, c => {
channel = client.channels.cache.get('1053340012021821593');
});
client.login(process.env.token);
const prompt = repl.start({
prompt: "Message to send: ",
eval: function(cmd,context,filename,callback){
message = channel.send(cmd)
this.displayPrompt()
}
})
prompt.defineCommand('test',{
help: "Test command",
action(newMessage){
message.edit(newMessage)
.then()
.catch(console.log(e))
},
})
// Log in to Discord with your client's token
30 replies