Reputation: 1
I’m making a discord bot that creates tickets to verify a customer with the ticket.
I want to be able to go into that channel and get the latest message from that channel and set it as my ticketid
variable. Is there a way to do that?
Upvotes: 0
Views: 5918
Reputation: 26
From the discord.js documentation here you can use the last message in the channel you sent to in the TextChannel class using lastmessage.id
method to get its id and use it for your ticketid.
const channelid = '222109930545610754'
const channel = client.channelmanager.cache.fetch(channelid);
// or const channel = client.channelmanager.cache.find(ids => ids.name === "Name");
// if you have a specified guild. const channel guild.channelmanager.cache.find(ids => ids.name === "Name");
var ticketnumber = channel.lastmessage.id
Upvotes: 0
Reputation: 8402
The easiest way would be to use the TextChannel.lastMessage
property:
<TextChannel>.lastMessage.content
You could also use MessageManager.fetch()
// get latest message in channel
<TextChannel>.messages.fetch({ limit: 1 }).then((message) => {
console.log(message.content);
});
Alternate method that only works if you're positive the message will be cached:
<TextChannel>.messages.cache.last()
(TextChannel.lastMessage
is by far the easiest and most efficient, but I thought I'd list other methods just in case)
Upvotes: 0