I am making a discord bot in discord.jsV12 that auto sets up a server. For this to happen i need the bot to delete all channels. I have tried msg.guild.channels.forEach(channel => channel.delete())
But nothing happens and I get 0 errors.
What you attempted is actually the solution.
You can use it on the message object, which means it's an action after you make a message, like so:
client.on('message', message => {
if (message.content === '.deleteChannels') {
message.guild.channels.forEach(channel => channel.delete());
}
});
triggered by making a discord message containing ".deleteChannels"
Make sure your bot has permissions to delete channels as well.
Be aware that this is a reply to a message. Otherwise, message would not be defined.
discord.js v12+ uses Managers, which means you'll have to go through an extra step and pass through the cache property.
msg.guild.channels.cache.forEach(channel => channel.delete());
Guide to switch from v11 to v12
More info about updating
Related
There are a lot of to ban a user in a server, you can do it manually or with another bot. I want my bot to scan a banlist, and if someone will get banned, it will send a message "user (id) has been banned
I started writing smt but now, I had no idea what can I do to check it.
EDIT: It's working now, but it still need to find this user id.
client.on('guildBanAdd', message => {
client.user.cache.find() //idk how to define it
const channel1 = client.channels.cache.find(channel => channel.id === "158751278127895");
channel1.send('12512');
})
You don't have to fetch bans because the guildBanAdd event parses a GuildBan object,which als includes a User object.
client.on('guildBanAdd', ban => {
console.log(ban.user.username); // Username
})
message.channel won't work because there isn't a message object.
If you want to get the executor (moderator) who performed the ban, kick etc.
There is a good guide for this Working with Audit Logs
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 want to use .react() from DM way. But the reaction into the post is sended by bot. I need send a reaction by the user.
var guild = bot.guilds.get(GUILD_ID);
if(guild && guild.channels.get(ch_ID)){
guild.channels.get(ch_ID).fetchMessage(msg_ID).then(message => {
message.react('🤔')
})
}
As I shared in a comment...
A client cannot add a reaction as another user.
Unfortunately, that means that you can't accomplish what you're trying to do within the Discord API. You can read reactions, but the bot simply would never be able to force someone to add a reaction.
I'm currently making a discord bot that will send out messages. Unfortunately the messages are receiving errors such as bot.sendMessage is not a function. I'm fairly new to coding so this one has me stumped. Even any google searches have not been able to help me to the point where I can understand it.
I've tried bot.send as maybe .sendMessage is now outdated I had read somewhere I believe.
var exampleSocket = new WebSocket(dataUrl);
bot.send({to: flowChannel,message: 'Websocket connected'});
exampleSocket.onopen = function (event) {
logger.info('got to here');
The output should post in my channel that the websocket connected.
bot.send({to: flowChannel,message: 'Websocket connected'});
Client.sendMessage() was removed in Discord.js 9.0 back in 2016.
I've tried bot.send as maybe .sendMessage is now outdated I had read somewhere I believe.
TextBasedChannel.sendMessage(), TextBasedChannel.sendCode(), TextBasedChannel.sendEmbed(), TextBasedChannel.sendFile(), and TextBasedChannel.sendFiles() are all deprecated.
The equivalent of your code today is as follows...
flowChannel.send('Websocket connected')
.catch(console.error); // Catch the rejected promise in the event of an error.
Discord.js Docs (Stable)
#sendMessage is deprecated.
You need to pass 'message' through and use message.channel.send()
Channel can also be a DM.
TextChannel.sendMessage (and DMchannel.sendMessage) along with all type of sendXX functions, is deprecated.
The function TextBasedChannel.send take as argument a MessageOptions value (or directly an Attachment or RichEmbed) if you want to pass anything other than just text.
To access the send function, you need a TextChannel or a DMchannel which you can get:
In the message that triggered your function like this: client.on('messages', (msg) => {msg.channel.send('Hello')})
If you have a Guild instance: guild.channels.get('channel id') or guild.channels.find(d => d.name === '<channel name>)') (note: you'll have to check if the channel is a TextChannel and not a voice one)
with the Client.channels method, which list all of the Channels that the client is currently handling, mapped by their IDs - as long as sharding isn't being used, this will be every channel in every guild, and all DM channels
I try to make my bot on Discord server. Want to make a function, which will copy all deleted message in text channel, but, messageDelete hear only deleted message which was writing after bot start. When I delete message which make earlier bot start, its not work.
{
client.on ("messageDelete", messageDelete =>{
let channel = client.channels.find(channel => channel.name === 'log-deleted-message')
console.log(`Deleted :${messageDelete.content}`)
channel.send(`${messageDelete.author.username} write : ${messageDelete.content}`
})
}
The above answer is now outdated.
With Discord.js V12 you can now Cache messages on the messageDelete event, You would need to enable Partials. Once this is enabled, you can fetch the message beforehand like this:
if(message.partial){
let msg = await message.fetch()
console.log(msg.content)
}
That will then log the content of the previously uncached message.
messageDelete is an event that is called when a message is deleted while the bot is on. If a message is deleted before the bot is turned on there is no way to recover it, which is why it's known as deleted. The only way to accomplish the goal that you want is to leave the bot on permanently. Read more in the docs if you want more information.