I want to make my discord bot make an announcement to a certain channel, based on the Channel ID.
I know how to make the bot respond in whatever channel it's talked to, using message.channel.send(), but I was wondering if there was a way to do message.channelID.send() to have the bot talk in a specific channel to make an announcement.
Thanks!
Nathan
client.channels.find("id", "what ever").send(/*...*/)
All channels are stored in a Collection under the Client.channels prpperty which you can use to get it.
Using client.channels.find("id", "what ever").send(/*...*/); will work to send a message to a certain channel, but in the console, an alert is thrown. Using client.channels.get("what ever").send("/*...*/"); does the same job, but without using a discord.js deprecated function.
Thanks to Jonas Wilms!
Related
I'm using discord.js and am trying to make a bot so that everytime this specific person says something from another server that the bot is in, I want the bot to repeat this message in a specific channel.
client.on('message', message => {
if (message.author === ("244921929495085057")){
client.channels.get("655598459268759593").send(message.content);
}
})
I can't figure out how to do this. Thank you!
I'm assuming you are using the latest version of discord.js which is v12 at the time of writing this.
According to the discord.js docs
You need to add .cache after client.channels to access the channels list.
I'm very new to using Discord.JS, so this didn't come naturally to me. Sorry if it feels like I'm just being stupid, but I have no idea how I am meant to get the last post in a channel using a Discord bot.
Any helps is appreciated, thanks!
TextChannels have a lastMessage property which lets you get the last last message sent in a channel. So, if you have a channel stored in the channel variable, you can get the last message sent via channel.lastMessage.
I'm coding a discord Bot with the discord.js library on a nodeJs server.
Here is my question: is that possible when an event occure (like someone sending a message) that the bot answers to members of a role, or to everyone. The message.reply("my reply") method only answer to the author of the message as I use it for now...
Your problem is that message.reply() is a very limited method: it always mentions the author of the message, and you can't override that.
You need to use a more generic method, channel.send(), and construct the mention yourself. .reply() is just a shortcut for a frequently used form of it, but you need something custom.
Presumably, you want it to happen in the same channel as the message, so you need message.channel.send("Whatever content you want").
Now, to add a role, you need to decide how to pick one. Is it fixed? Then you could hard-code a role mention by role ID: <#&134362454976102401> (of course, it needs to be the role ID you require).
If you want to find a role, for example by name, you need to do that via searching through the guild in question. You can access it though message.guild, but be warned that it will be undefined for DMs.
Then you can do something like
const role = message.guild.roles.find(role => role.name === "NameYouWant");
message.channel.send(`${role} something something`);
because Role objects become mentions when converted to strings.
How do I get the main channel from a server? Almost all major bots do this. For example, Rythm. The bot sends a message in the main channel that tells you how to use Rythm. That's kind of what I intend to do also, to let people know how to use my bot. If you could help me with this, that would be much appreciated. Thanks!
Guilds have a defaultChannel value you can access.
guild.defaultChannel.send(`Hello`);
Documentation:
https://discord.js.org/#/docs/main/stable/class/Guild?scrollTo=defaultChannel
Though this is Deprecated you can still use it unless the #general channel is removed.
Otherwise:
For the channel the invite refers to:
https://discord.js.org/#/docs/main/stable/class/Invite
invite.channel is the channel the invite refers to
I have been making a discord bot and wanted to make it send a message to a specific "Welcome" channel. Unfortunately, I have been unable to do so. I tried this.
const welcomeChannel = bot.channels.get("name", "welcome")
welcomeChannel.sendMessage("Welcome\n"+member.user.username);
However in this "welcomeChannel is undefined".
Edit:
I tried using
const welcomeChannel = bot.channels.get("id", "18NUMBERIDHERE")
welcomeChannel.sendMessage("Welcome\n"+member.user.username);
but this is still undefined, strangely
You should use the channnel id instead of it's name.
How to get the channel id of a channel:
Open up your Discord Settings
Go to Advanced
Tick Developer Mode (And close the Discord settings)
Right click on your desired channel
Now there's an option Copy ID to copy the channel id
Also checkout the discord.js documentation for (channel) collections
Furthermore your approach won't work because .get wants a channel id (see the linked documentation above). In case you REALLY want to get an channel by its name, use .find instead for that.
This is however a really bad idea in case your bot runs on more than one server since channel names can now occur multiple times.
You can also use
bot.channels.find("name","welcome").send("Welcome!")
I tried a lot with the same error, and that's how I fixed it. I used client as my Client().
client.channels.cache.get("18NUMBERIDHERE").send("Welcome!");
Your error could come from the fact that you're using bot.channels.get(), which isn't the best idea, because discord.js isn't very friendly when it comes to using .send() on multiple items.
Instead, try to use member.guild.channels.find("name", "channel").send();, if possible. If this is in the client.on("message"), then simply use message.member.channels.find("name", "channel").send();
Sidenote: My memory is mixed up, so if that doesn't work, try .get() instead of find.