Get specific message from specific channel - bots

Purpose: I'm trying to make my bot check for reactions from a specific message.
I've read this post: Get Message By ID: Discord.js
But it didn't help at all.
I've searched the internet to see how can I use .fetchMessage properly. But unfortunately didn't find any results.
This is my code:
client.channels.get('CHANNEL ID').fetchMessage('MESSAGE ID').then(async msg => { *CODE HERE* });
This is the error i get:
TypeError: client.channels.get is not a function
I do realise that client.channels.get is not a function and I should use this in a function but I don't know how to.
Discord.js version: 12.0.2
Node.js verison: 12.13

That answer was for v11, in v12 it has changed to:
client.channels.cache.get(chid).messages.cache.fetch(mesid)
However, it's important to note that client.channels.cache may contain non-text channels, if you are retrieving an ID that you know to be a TextChannel type, you will be fine but if the ID is being retrieved programmatically you need to check to ensure it is an instanceof TextChannel.

In v12 it has changed and uses managers and added this command to my bot and fixed it.
let channelMessage = client.channels.cache.get(channel_id) // Grab the channel
channelMessage.messages.fetch(message_id).then(messageFeteched => messageFeteched.delete({timeout: 5000})); // Delete the message after 5 seconds

Related

Need help fixing a Discord Bot command

I'm making a >bean {user.mention} command, which the bot would respond with {user.mention} has been beaned! I want the responce to not ping the user, but just say the users username and hashtag (ex: Example#1234).
Here is my code (node.js v12):
if (message.content.startsWith('!bean ')){
message.channel.send('${user} has been beaned!')
}
})
Read docs
Coming to your question
client.on("message", message =>{
if(message.content.startsWith('!bean')){
let target = message.mentions.users.first()
if(!target) {
message.reply(`Mention a user to bean!`)
} else {
message.channel.send(`${target.username} has been beaned`)
//.username returns their username like 'Wanda' & .tag returns their tag like 'Wanda#1234'
}
}
})
Some things you can change to make your future coding organised
• Use a command handler
2 things you must change
• Update to discord.js v13 since v12 is deprecated
• Update your node to v16 or higher
In-case you need help knowing initial setup changes, read the guide

How to add reaction to a specific message using the ID? (discord.py)

I've been trying for hours a command that react to a message using the ID.
If someone writes !react (the message ID) the bot reacts to the message.
Can someone help me? I have no clue how to do this.
Use a converter to get the discord.Message instance of the message:
#client.command()
async def react(ctx, message: discord.Message):
...
Then use Message.add_reaction to add a reaction to it, which I'm sure you can figure out by yourself.
Keep in mind that in case the message ID is invalid, can't be found, or is not in the same channel as where the command gets called, the converter will fail & throw you an exception. If you pass in the message's URL instead of the ID, Discord will be able to figure out what channel the message was sent in so you can call the command from wherever you want.
You can get a message's URL by selecting Copy Message Link, which might be better for your users as getting the id requires Developer Mode to be on, which most people don't have enabled. The MessageConverter mentioned in the beginning can parse both id's and URL's so you don't have to worry about that part.

TypeError: Cannot read property 'name' of undefined discord.js

Sorry if i sound dumb saying this im new to Node.js
I tried looking other places and couldent find an answer and the docs didnt make sense to me but im getting errors when i want my bot to send a embed that automatticly says the guilds name in it so im using message.guild.name and i defined message but now im getting the error =
TypeError: Cannot read property 'name' of undefined
So im not sure what to do, my main code is
exports.run = (message) => {
const embed = new RichEmbed()
.setDescription(`Hello! Welcome to ${message.guild.name}!`)
.setColor(0xdd9323)
.setFooter(`This bot was made by Fortnitewinner21#1076 and Hextanium#5890`);
message.channel.send(embed).then(m => m.react('✅')).catch(console.error);
};
BTW- im v11
You don't have a message to refer to in this case. I suggest putting this inside an event or making this into a command. But since you said you wanted it to be sent automatically you will need to use an Event or an Interval.

Discord.js sending a message to a specific channel

I'm failing to achieve something very simple. I can't send a message to a specific channel. I've browsed trough the documentation and similar threads on stack overflow.
client.channels.get().send()
does NOT work.
It is not a function.
I also do not see it as a method on the Channel class on the official documentation yet every thread I've found so far has told me to use that.
I managed to have the bot reply to a message by listening for a message and then using message.reply() but I don't want that. I want my bot to say something in a specific channel in client.on('ready')
What am I missing?
You didn't provide any code that you already tested so I will give you the code for your ready event that will work!
client.on('ready', client => {
client.channels.get('CHANNEL ID').send('Hello here!');
})
Be careful that your channel id a string.
Let me know if it worked, thank you!
2020 Jun 13 Edit:
A Discord.js update requires the cache member of channels before the get method.
If your modules are legacy the above would still work. My recent solution works changing the send line as follows.
client.channels.cache.get('CHANNEL ID').send('Hello here!')
If you are using TypeScript, you will need to cast the channel in order to use the TextChannel.send(message) method without compiler errors.
import { TextChannel } from 'discord.js';
( client.channels.cache.get('CHANNEL ID') as TextChannel ).send('Hello here!')
for v12 it would be the following:
client.on('messageCreate', message => {
client.channels.cache.get('CHANNEL ID').send('Hello here!');
})
edit: I changed it to log a message.
This will work for the newest version of node.js msg.guild.channels.cache.find(i => i.name === 'announcements').send(annEmbed)
This should work
client.channels.fetch('CHANNEL_ID')
.then(channel=>channel.send('booyah!'))
Here's how I send a message to a specific channel every time a message is deleted. This is helpful if you'd like to send the message without a channel ID.
client.on("messageDelete", (messageDelete) => {
const channel = messageDelete.guild.channels.find(ch => ch.name === 'channel name here');
channel.send(`The message : "${messageDelete.content}" by ${messageDelete.author} was deleted. Their ID is ${messageDelete.author.id}`);
});
Make sure to define it if you're not using messageDelete.

Get Message By ID: Discord.js

I am doing a report system for a discord bot and I want the player to report a specific message by the id so that the moderators can decide if it is offensive or not. I am struggling to find a way to get the message's text from the given id. Is there a possible way of doing this?
fetchMessage is no longer present in Discord.js starting in version 12, but you can use the fetch method of the MessageManager class from the messages property of the TextChannel class.
msg.channel.messages.fetch("701574160211771462")
.then(message => console.log(message.content))
.catch(console.error);
You can retrieve a message by id through
msg.channel.fetchMessage();
The documentation is here. If you want to be able to retrieve a message from any channel by id, you can loop through all channels and catch any errors.
From the official doc:
channel.messages.fetch('{messageIdGoesHere}')
.then(message => console.log(message.content))
.catch(console.error);

Resources