How to get first message on channel? I have really no idea how to do this and i didnt found anything on internet. I need it for getting first mention on channel, so if there is some easier way (than getting first message), you can
also post it.
You can get all messages with fetchMessages() and then loop over and check for mentions:
message.channel.fetchMessages().then(messages => {
messages.forEach((item, index)=>{
// do something with
// item.mentions
// documentation: https://discord.js.org/#/docs/main/stable/class/MessageMentions
});
}).catch(err => {
console.log('Error while getting mentions: ');
console.log(err);
});
Related
I'm making a suggestion module on my bot, and I have everything else down, but am struggling on fetching the message the bot sent and editing it using a command that was sent in a different channel.
I tried doing await message.guild.channels.cache.get("channel-id-here").messages.fetch("the-actual-messages-id-here").edit({ content: 'Test' }) but it didn't work.
Your code, while in theory, should work, most likely doesnt due to unresolved promises.
In the line
await message.guild.channels.cache.get("channel-id-here").messages.fetch("the-actual-messages-id-here").edit({ content: 'Test' })
The await at the beginning is trying to resolve the promise of the edit method, where there is another promise to be resolved at the <MessagesManager>#fetch method. In other words you're ignoring the promise returned from the fetch which will resolve no messages. Consider the following change:
const messages = await message.guild.channels.cache.get("channel-id-here").messages;
await messages.fetch("the-actual-messages-id-here").edit({ content: 'Test' });
Or if you really want a one-liner (I dont suggest this as it compromises on readability) you could use:
await (await message.guild.channels.cache.get("channel-id-here").messages.fetch("the-actual-messages-id-here")).edit({ content: 'Test'
I have been trying to implement an interaction collector as per the discord.js guide, but the guide does not explain much of anything and the pieces I have puzzled together from other sources do not fit together. Here's the example from the guide:
message.awaitMessageComponent({ filter, componentType: 'SELECT_MENU', time: 60000 })
.then(interaction => interaction.editReply(`You selected ${interaction.values.join(', ')}!`))
.catch(err => console.log(`No interactions were collected.`));
It took me a day to figure out that you can define a message object like so:
message = await interaction.channel.send(content: 'text', components: >select menu<)
That works to run .awaitMessageComponent() and to grab the input from the select menu, however interaction.editReply() gives an error: INTERACTION_NOT_REPLIED
Moreover, I need it to be a reply anyway, however
message = await interaction.reply(content: 'text', components: >select menu<)
leaves message as undefined, so of course I cannot run .awaitMessageComponent() on it.
So I do not understand what I am supposed to do to do what that guide is doing.
I would very much appreciate any insight into this issue.
Unfortunately all other guides (usually on collectors) also start with a .send() message and most other resources go way over my head, as I have no real background in coding/scripting.
EDIT:
As Malik Lahlou points out, you can assign the reply by including the fetchReply:option, like so:
message = await interaction.reply({ components: [select menu], fetchReply: true })
However that still does not allow the guide's code to run (error INTERACTION_NOT_REPLIED, so after some more research I finally found a way to use .awaitMessageComponent() and applied to the guide's example it would look like this:
await interaction.reply({ components: [select menu], ephemeral: true });
const collectedSelect = await interaction.awaitMessageComponent({ filter, componentType: 'SELECT_MENU', time: 60000 })
.then(interaction => interaction.editReply(`You selected ${interaction.values.join(', ')}!`))
.catch(err => console.log(`No interactions were collected.`));
Simple answer, add the fetchReply option when replying to the interaction :
const message = await interaction.reply({
// content : ...,
// components : ...,
fetchReply: true
})
Another way would be to use interaction.fetchReply after awaiting the reply but it's better to directly give it as a parameter so discord.js does this stuff for us :D Here's an example on the official discord.js guide
I'm currently developing a bot that pins messages in a text file to bypass the message pin limit in discord servers, and my system works by having a user reply to a message with -pin and it will save that message and the message author in a text file. The problem is that I don't know how to get the content of the replied to message and the author of said message. Any help would be appreciated.
if(command === 'pin'){
message.channel.send('Ok, pinning that. Use -seepins to see all the pins.'),
//i have no clue how to get the message that was replied to please help let pinned =
//im begging you please i have no idea let pinnedauthor = message.
fs.writeFile('messages.txt', pinned + ' written by ' + pinnedauthor + '\n', (err) => {
if (err) throw err;
});
}
this is what I currently have. I just need to find out how to get the two things mentioned in the first paragraph and it should work.
Any help is appreciated, and thank you for reading this through.
When you receive the message object, you have a variable named reference. If your message is a reply reference should not be null and contains the ids of channelID, guildID, messageID.
With this you can then get the message content of the previous message with a line that looks like
client.on('message', async message => {
const repliedTo = await message.channel.messages.fetch(message.reference.messageID);
console.log(repliedTo.content);
});
Just be careful with the message.reference.messageID you have to add error handling to avoid crash when looking for an undefined variable.
I am looking to send a message to my logging channel when a user types something important (for a game server) what I have now doesn't work and just spams messages for some reason. if you know how to fix this and make it work and not spam messages, that would be good. (no errors in console)
client.on('message', (message) => {
if (message.content.startsWith("####")) return; {}
const logChannel = client.channels.cache.get('783009285709496371');
logChannel.send(`User: <#${message.author.id}> | Message: ${message.content}`);
})
Invert the return condition. Right now you are sensing all messages that dont start with #####.
Put a ! in front of the content check.
I'm making a bot which on a react to a certain will create a channel...
That all works perfectly expect I want a nessage to be posted when the cahnnel is created which has a specfic beginning.
client.on('channelCreate', (channel, message) => {
if(channel.name.startsWith('ticket-')){
message.channel.send('test');
});
I'm not getting any errors, just nothing...
You can't use the message variable in the channelCreate event. The only thing you're receiving is a channel object, so you need to use channel.send():
client.on('channelCreate', (channel, message) => {
if(channel.name.startsWith('ticket-')){
channel.send('test');
});