Reputation: 187
I'm working on my discord bot and I want it to save messages that i sent it to save, how do I do this since the rest of the internet doesn't ask this question for some reason. i've been looking for someone to point me in a direction but haven't found anything
Upvotes: 0
Views: 3540
Reputation: 319
This is a really simplified version of what you want to do but I'm sure if you read it you'll understand and it will get the job done.
const discord = require('discord.js'); // Import discord.js
const client = new discord.Client(); // Initialize new discord.js client
const messages = [];
client.on('message', (msg) => {
if (!msg.content.startsWith('+')) return; // If the message doesn't start with the prefix return
const args = msg.content.slice(1).split(' '); // Split all spaces so you can get the command out of the message
const cmd = args.shift().toLowerCase(); // Get the commands name
switch (cmd) {
case 'add':
messages.push(args.join(' ')); // Add the message's content to the messages array
break;
case 'get':
msg.channel.send(messages.map((message) => `${message}\n`)); /* Get all the stored messages and map them + send them */
break;
}
});
client.login(/* Your token */); // Login discord.js
Upvotes: 1