'How do I get all messages on a channel and post to hastebin? discord.js
I'm idealizing a doubt bot. In view of this, I must explain that at the end of the member's question, the channel must be closed and before that the bot must send a link from hastebin.com with the logs of the created channel.
An example in practice would be: I create the ticket, the channel is created so that only me and the staff can see the channel. I ask and am answered. After having my answer, the channel is closed (deleted). Before being deleted, the bot creates a log and sends it to the hastebin, which stays the same in the print below.
Please, help me!
edit: I know how to do most of them, but my problem is to get the channel log and post it in hastebin, formatted.
Solution 1:[1]
after we talk about we really got something here. I updated your code to something more efficiently and I think whatever people that uses discord.js will be able to understand what is going on here.
Old Code
message.channel.fetchMessages().then(messages => {
console.log(`${messages.size} procuradas.`);
messages.array().reverse().forEach(msg => {
console.log(`[${moment(msg.createdTimestamp).format("DD/MM/YYYY - hh:mm:ss a").replace("pm", "PM").replace("am", "AM")}] ` +
`[${msg.author.username.toString()}]` + ": " + msg.content);
});
})
New Code
message.channel.fetchMessages().then(async messages => {
console.log(`${messages.size} procuradas.`);
let finalArray = [];
const putInArray = async (data) => finalArray.push(data);
const handleTime = (timestamp) => moment(timestamp).format("DD/MM/YYYY - hh:mm:ss a").replace("pm", "PM").replace("am", "AM");
for (const message of messages.array().reverse()) await putInArray(`${handleTime(message.createdTimestamp)} ${message.author.username} : ${message.content}`);
console.log(finalArray);
console.log(finalArray.length);
});
Hope it helps!
Solution 2:[2]
An update to the accepted answer: the Discord.js package has changed since the answer was written and the new way to fetch messages is with
message.channel.messages.fetch().then(...);
https://discord.js.org/#/docs/discord.js/stable/class/MessageManager?scrollTo=fetch
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Bullyen |
Solution 2 | Robert VanLonkhuyzen |