I need to select a previously sent message (from a bot) in discord.js
async () => {
let fetched;
fetched = await receivedMessage.channel.messages.startsWith("Please use the command:")
fetched.delete()
}
I tried this, but it doesn't seem to work.I'm trying to get a message by it's contents, and then delete it.
Edit: So apparently... "You could fetch the last 100 messages using channel.messages.fetch with a limit option, then filter that collection by message.content.startsWith, and then passing that into channel.bulkDelete()"
Which in my opinion, sounds like the way to do it - I will try it out soon.
You don't need to use startsWith you can just check if the message was sent by a bot using message.author.bot.
Related
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.
I've been making a suggestion command and I want it so that I can use a -approve <The message id> to edit the message. The problem is when I try to fetch info from it using
const Suggestionchannel = client.channels.cache.get("833796404383973442");
const messages = Suggestionchannel.messages.fetch({ limit: 100 })
it only fetches all the messages and not the embeds. I want it to fetch the embeds so I can get the data and edit the message by its id. With the data, I could display who made the suggestion and what the suggestion was while at the same time showing that it was approved. How would I get the info from an embed using a message id?
On a side note: I know I could use async and await to easily edit it while on the same code but I want it so that it's accessible. If I do that and restart the bot, I won't be able to approve the previous suggestions, only the new ones after I restart the bot.
First, you have to either await fetching the messages from the suggestion channel, or put it into a .then to not have messages be a Promise. Awaiting it wouldn't change anything in your ability to edit the message, you just have to make sure your message cache has the actual message in it.
Fetching the messages from a channel would return a Promise<Collection<Snowflake, Message>>. Once you resolve the message, you get a collection containing messages. On the Message class, there is a array called embeds. It contains all of the embeds part of the message, normally, if sent by a bot, only contains one MessageEmbed.
Another thing is, to make it easier for you, the fetch message also takes in a message ID, and if it's provided, it would fetch the message with that ID from the channel. Doing this would change what it returns to Promise<Message>
Implementing all of these changes in your code would look something like;
const Suggestionchannel = client.channels.cache.get("833796404383973442");
// make sure that your outer scope is asynchronous
const message = await Suggestionchannel.messages.fetch('INSERT MESSAGE ID OR VARIABLE HERE')
const embed = message.embeds[0]
//would be undefined if there is no embed
//would be a MessageEmbed if there is an embed
I am looking to send a message that has a different profile picture than the bot. Basically, like Tupper Bot does. I can't seem to figure out how to do it anywhere, I've read the documentation and searched around.
You can use a webhook to do what you describe. If you already have a webhook link see here on how to use it. This works from external programs too.
If you don't, you can create a webhook with your bot and then send messages to it. In this example we will create a simple webhook and send a message to it.
First we should check if the channel already has a webhook. That way you don't create a new one everytime you use the command. You do that with the fetchWebhooks() function which returns a collection if there is a webhook present. If that is the case we will get the first webhook in the collection and use that. Note: if you have more then one webhook you can of course add additional checks after the initial one.
If there are no webhooks present, we create a new one with createWebhook(). You can pass additional options after the name.
let webhook;
let webhookCollection = await message.channel.fetchWebhooks();
if (webhookCollection.first()) {
webhook = webhookCollection.first();
} else {
webhook = await message.channel.createWebhook("new webhook", {
avatar: "https://i.imgur.com/tX5nlQ3.jpeg"
});
}
Note: in this example we use the channel the message came from but you can use any channel you want
Technically we can now send messages already.
webhook.send("this is a test");
What you can also do is create a webhookClient. Thats a little like the discord client you create when you start your bot. It takes the webhook ID and token as well as additional options you can set for clients.
const hookclient = new Discord.WebhookClient(webhook.id, webhook.token);
This lets you use a few additional methods like setInterval.
hookclient.setInterval(() => {
hookclient.send("This is a timer test");
}, 1000)
This lets you use the webhook without creating or fetching one first.
so every time a new user joins I sent the users a message using
client.on ("guildMemberAdd", member => {
member.send("WELCOME")})
Now, is there any way for the bot to react to that message?
Thanks!
Similar to Syntle's answer, but uses promises instead. (My preferred use)
member.send("WELCOME").then(function(msg){
msg.react("😀");
}
You need to contain member.send() in a variable and then use the .react() method on it.
So your solution is:
const msg = await member.send("WELCOME")
await msg.react("🥚");
The title says all. I'm making a Discord bot in node.js, and one part that I'm trying to add is a .setup command, to make the bot less dependent on manually changing the value of client.channels.get().send(), allowing it to be more easily set up in the future.
Anyway, in my case, I'm trying to have the user reply to a message with a channel mention (like #welcome for example), and have the bot return that ID and save it to a variable.
Currently, I have this:
function setupChannel() {
client.channels.get(setupChannelID).send('Alright. What channel would you like me to send my reminders to?');
client.on('message', message => {
checkChannel = message.mentions.channels.first().id;
client.channels.get(setupChannelID).send('Ok. I\'ll send my reminders to that channel.');
});
}
message.mentions.channels returns undefined.
I know that the message.mentions.channels.first().id bit works when its a user instead of a channel, is there a different way of getting a channel mention in a message?
Here is the most easy and understandable way i found :
message.guild.channels.cache.get(args[0].replace('<#','').replace('>',''))
But it not really what we want we could use <#####9874829874##><<<547372>> and it will work. So i figured out this :
message.guild.channels.cache.get(args[0].substring(2).substring(0,18))
I like to just remove all non-integers from the message, as this allows for both IDs and mentions to be used easily, and it will return the ID for you from that, as mentions are just IDs with <# and > around them. To do this you would use
message.content.replace(/\D/g,'')
With this, your code would look like this:
function setupChannel() {
client.channels.get(setupChannelID).send('Alright. What channel would you like me to send my reminders to?');
client.on('message', message => {
checkChannel = message.content.replace(/\D/g,'');
client.channels.get(setupChannelID).send('Ok. I\'ll send my reminders to <#' + checkChannel + '>.');
});
}
From this you will always get an ID. And we can surround the ID to trun it into a mention.