How to store old messages - javascript

So I am new to discord API and I am currently trying to create a small simple discord bot.
The thing that I am trying to do is archive the messages that have been sent in a specific channel. Not only those deleted, or edited but all of them.
Any ideas?

You can try .fetchMessages([options]) .
Default messages get is 50.
Options object you can put as where you want to start collecting the messages from. Place the message inside options and it will start downloading starting from the message.

Related

Messages leaderboard issue discord.js

How are you?
I'm coding a Discord bot and I'm making a message leaderboard command, which displays up to 15 members with most messages (quick.db package to store data). Basically, It's alright when I run the code, but when someone who has so much messages and should appear in the leaderboard leave the server, it returns an error when I try to get the members tag and undefined in the message.
Is it possible to hide the user or delete them from the database?
What have I tried to solve the problem?
I tried to read Discord docs. and quick.db package to see if I can delete the user when they leave (using guildMemberRemove event), but what if they get back to the server?
οΎ 
A better way is to delete user(lefted)* data from database once user left the server in guildMemberRemove event

Discord.js | Fetching data when bot has been added to a new server

Is this safe to be added on the function once you added a bot to a server or I can be abused?
I'm kinda scared of getting banned or rate limited So I want to make sure that everything is safe. Currently the fetch in Docs v12 is not that well documented and lack of infos about this. All I know is it fetch all data to the server. The reason I need this is to get all of the information like roles, members, etc because when trying to list all members that has the specific role all try to list members without roles it require me to fetch data since if I don't instead of members(100) it will only fetch like members(3) since the cache is not yet ready or data is not fully loaded to the cache.
await message.guild.members.fetch()
.then(console.log)
Currently I have this command /fetch which will fetch the data with a 1 hour cooldown. I am planning to remove this and add it to "once they added the bot" function. I use this fetch to instances/commands like listing users without roles
message.guild.members.cache.filter(member => member.roles.cache.array().length < 2).map(member => member.user.tag);
If you have like recommendation or a better way to do this you can check the project at PruneBot/commands/custom where full code of this is included:
fetch data
kick no role
kick users with specific role
list users without role
list users with specific role
if you have a large scale bot, that is joining many servers then you might have issues with this call, but if you're not having this issue, then you can pull it when the bot is added to a server, by putting it in the guildCreate event handler. Something like:
client.on('guildCreate', guild=> {
guild.members.fetch()....

is there a way to save reaction role data?

I've currently got reaction roles set up in my code and they work, however I have to rerun the command every time I restart the bot, which isn't optimal.
Is it possible at all to cache the data so that there is only one reaction role message and the roles are given out regardless of how many times the bot has rebooted?
Any help with this is greatly appreciated!
in order to do this you need to have a database setup and store the message's id.
Then when your bot starts you can fetch that message inside messageReactionAdd/messageReactionRemove client event(s) and track for reactions on the message id you have previously stored.

How do I retrieve user information from discord.js events?

Right now I'm working on a Discord bot ignore command will ignore a channel's command inputs when this action is true. Right now my current js file is here.
What I need the bot to do is:
Detect when the message "GCMignore" is posted
Find if their role is "Moderator".
Right now I'm not able to get retrieve the user who sent the message to detect if they are a moderator. I've tried guildmember.roles and I understand how a map works, it's just that the code is not able to relate back to the person who sent the message.
See this link.
You can use User.hasRole() function to see what roles they have. Also to be able to create roles please see this link

how to pull message data from discord.js?

Discord.js is an API for Discord that allows the developers to make plugins for the program, discord. here's the link to the API code it's in js, https://github.com/hydrabolt/discord.js/
Discord is setup to be like a server where you connect and chat on channels, my problem is how do I pull the message data from the channels.
What they did is setup all the channels in a JSON cache and within the channel, objects is another cache with the messages objects(what documentation says). But when I get to the message cache all I see is messages: Cache { limit: 1000 } }. How do I pull all the message objects from the channel?
For those using discord.js v8 or lower.
If you want to pull all the message objects from the channel I recomend ignoring the cach and instead using getChannelLogs(channel, limit, options, callback) Which will allow you to fetch up to 100 messages at once, but those messages do not have to be cached within discord.js. You can quite easily create a recursive function that on the callback fetches more messages to fetch as many messages as you want.
That said server.channel.messages will be all of the messages in it that discord.js has cached. If it appears empty chances are no-one sent a message since the bot was activated.
source: http://discordjs.readthedocs.io/en/latest/docs_client.html#getchannellogs-channel-limit-options-callback

Categories

Resources