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.
Related
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
So I've been trying to figure this out but have had a hard time. I can't seem to figure it out. How can I make it so that if a message sent isn't an emoji, it will delete it?
I have adapted this answer to detect if the message consists entirely of emojis, and if it doesn't, it gets deleted.
client.on('message', message => {
if (!/[\u{1f300}-\u{1f5ff}\u{1f900}-\u{1f9ff}\u{1f600}-\u{1f64f}\u{1f680}-\u{1f6ff}\u{2600}-\u{26ff}\u{2700}-\u{27bf}\u{1f1e6}-\u{1f1ff}\u{1f191}-\u{1f251}\u{1f004}\u{1f0cf}\u{1f170}-\u{1f171}\u{1f17e}-\u{1f17f}\u{1f18e}\u{3030}\u{2b50}\u{2b55}\u{2934}-\u{2935}\u{2b05}-\u{2b07}\u{2b1b}-\u{2b1c}\u{3297}\u{3299}\u{303d}\u{00a9}\u{00ae}\u{2122}\u{23f3}\u{24c2}\u{23e9}-\u{23ef}\u{25b6}\u{23f8}-\u{23fa}]+$/u.test(message.content)) {
message.delete();
}
});
Alternatively, you could use emoji-regex, as pointed out by Jakye in the comments.
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.
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
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);