Get Message By ID: Discord.js - bots

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);

Related

How do I get a server ID from a message in Discord.js?

I'm sorry for asking a relatively simple question, but I can't seem to find the answer online
You can get the guild of a message using someMessage.guild.
https://discord.js.org/#/docs/main/stable/class/Message?scrollTo=guild
That gives an instance of guild which has an id property. You can get it using someMessage.guild.id
https://discord.js.org/#/docs/main/stable/class/Guild?scrollTo=id
Easy! <message>.guild.id
client.on('message', msg => {
msg.channel.send(msg.guild.id);
})

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.

Make an invite to every server bot is in

So I want to make an invite to every server bot is inside.
The bot needs "CREATE_INSTANT_INVITE" permission within the channel.
I made something like this but it seems not to work.
client.guilds.cache.forEach(guild => {
guild.channels.cache.first().createInvite()
.then(inv => console.log(`${guild.name} | ${inv.url}`));
});
Error:
DiscordAPIError: Unknown Channel
I think I get the error because bot tries to make an invite but he does not have permission to do so.
Good afternoon,
The first() channel is probably a category. You're not able to make invites on categories :sob: however, you could filter the guild's channels so that categories are not included.
Then you need to get a random() channel and log that.
client.guilds.cache.forEach(guild => {
guild.channels.cache.filter(x => x.type != "category").random().createInvite()
.then(inv => console.log(`${guild.name} | ${inv.url}`));
});
The rest of your code was fine and should work as expected.
I hope this helps, don't forget to upvote the answer and mark it with a tick if it works.

Get specific message from specific channel

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

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.

Resources