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.
Related
I'm trying to make a discord bot and it works fine but there's one thing that doesn't work. For some reason, it gives me this error when I try to do something with the bot. UnhandledPromiseRejectionWarning: ReferenceError: message is not defined I have no idea why. Heres the code that has the error: const collector = reactionMessage.createReactionCollector((reaction, user) => message.guild.members.cache.find((member) => member.id === user.id).hasPermission("MANAGE_NICKNAMES"), { dispose: true }
Heres's some context of what the bot is if that helps: It's a ticket bot, where members can report problems to mods in a private channel. It creates a channel, and sends a message saying "staff will be with you shortly" and puts reactions so that you can close the ticket. When I try to close the ticket, it gives the error. The error happens around user.id).haspermission("MANAGE_NICKNAMES") That code is supposed to make people without the MANAGE_NICKNAMES permission unable to use the reactions, and theres no place in the code that i'm supposed to put 'message' so why is this happening?
Try replacing places message to reaction.message and if it doesn't work try using partials
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);
})
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'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 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);