I'm actually making a discord bot with discord.js, and I was wondering how to do a command to delete a specific channel with a name
ex : !delete #general
I already tried to do the following:
if (command == "delete") {
channel.delete(args.join(" "))
}
but it doesn't work so I'm kinda stuck
thank you
You have to use the .delete method to delete a guild textchannel.
I added a new variable fetchedChannel which tries to fetch the channel by its name from args.
Try to use the following code:
const fetchedChannel = message.guild.channels.find(r => r.name === args.join(' '));
if (command === 'delete') {
fetchedChannel.delete();
}
That code now is old if you are going to up-date(with discord.js v12) it try with:
const fetchedChannel = message.guild.channels.cache.get(channel_id);
fetchedChannel.delete();
If you want to delete a specific channel with eval command then use this code
t!eval
const fetchedChannel = message.guild.channels.cache.get("CHANNEL_ID");
fetchedChannel.delete();
use:
message.channel.delete();
you can put it in client.on like this
client.on("message", message => {
message.channel.delete()
})
Related
I'm trying to create a modmail system and whenever I try to make it, it says "channel.send is not a function, here is my code."
const Discord = require("discord.js")
const client = new Discord.Client()
const db = require('quick.db')
// ...
client.on('message', message => {
if(db.fetch(`ticket-${message.author.id}`)){
if(message.channel.type == "dm"){
const channel = client.channels.cache.get(id => id.name == `ticket-${message.author.id}`)
channel.send(message.content)
}
}
})
// ...
client.login("MYTOKEN")
I'm trying this with version 12.0.0
EDIT:
I found my issue, for some reason the saved ID is the bots ID, not my ID
As MrMythical said, you should use the find function instead of get. I believe the issue is that you're grabbing a non-text channel, since channel is defined, you just can't send anything to it.
You could fix this by adding an additional catch to ensure you are getting a text channel, and not a category or voice channel. I would also return (or do an error message of sorts) if channel is undefined.
Discord.js v12:
const channel = client.channels.cache.find(c => c.name === `ticket-${message.author.id}` && c.type === 'text');
Discord.js v13:
const channel = client.channels.cache.find(c => c.name === `ticket-${message.author.id}` && c.type === 'GUILD_TEXT');
Edit:
You can tell channel is defined because if it weren't it would say something along the lines of: Cannot read property 'send' of undefined.
You are trying to find it with a function. Use .find for that instead:
const channel = client.channels.cache.find(id => id.name == `ticket-${message.author.id}`)
I am working on a discord bot who is supposed to ban members that reacted to a specific message with a specific emote. The file I created for the messageReactionAdd event currently contains the following code:
module.exports = {
name: 'messageReactionAdd',
execute(client, reaction, user) {
const channel = client.channels.cache.find(channel => channel.name === 'test');
let message = 874736592542105640;
let emotes = ['kannathinking', '🍎'];
let roleID = (reaction.emoji.name == emotes[0] ? '874730080486686730' : '874729987310235738')
if (message == reaction.message.id && (emotes[0] == reaction.emoji.name || emotes[1] == reaction.emoji.name)) {
user.ban();
channel.send(`${user} was banned`);
}
}
}
However this code doesn't work and throws me the following error:
user.ban() is not a function
After doing some research I found out that the ba command only works on GuildMember objects. Unfortunately those aren't created when messageReactionAdd is called. Does anyone have an idea how to work around it?
You don't have to get the GuildMember object, you can just ban the user by id from GuildMemberManager which can be found from the reaction object via reaction.message.guild.members
So instead of using user.ban() you can use
reaction.message.guild.members.ban(user.id)
discordjs version 11.4.2
I type !a hello in channel but bot don't sent message
if(command === "!a hello"){
const msg = await message.channel.send("Checking Command...")
msg.edit("hello");
}
First of all, you need to define 'command', but in your case you can change out command with msg.content === '!a hello'. You also don't have to define 'msg', although I suppose you could do that still.
You can do this:
bot.on("message", msg => {
if (msg.content === "!a hello") {
msg.channel.send("Checking Command...").then(msg => {
msg.edit(`hello`);
});
}
});
Tell me if you need anything or need to remove something or add something. Hope this helps :)
My guess is that you have defined "command" to something like this somewhere in your code
const args = message.content.split(/ +/g);
const command = args.shift().toLowerCase();
What's causing you this problem would be this part of it: ".split(/ +/g);"
This splits everything with a space in between into substrings. So how can you fix it now? Either use the command "!a-hello" or don't use "command"
if (message.content === "!a hello") {
const msg = await message.channel.send("Checking Command...")
msg.edit("hello");
};
So I have been utterly frustrated these past few days because I have not been able to find a single resource online which properly documents how to find emojis when writing a discord bot in javascript. I have been referring to this guide whose documentation about emojis seems to be either wrong, or outdated:
https://anidiots.guide/coding-guides/using-emojis
What I need is simple; to just be able to reference an emoji using the .find() function and store it in a variable. Here is my current code:
const Discord = require("discord.js");
const config = require("./config.json");
const fs = require("fs");
const client = new Discord.Client();
const guild = new Discord.Guild();
const bean = client.emojis.find("name", "bean");
client.on("message", (message) => {
if (bean) {
if (!message.content.startsWith("#")){
if (message.channel.name == "bean" || message.channel.id == "478206289961418756") {
if (message.content.startsWith("<:bean:" + bean.id + ">")) {
message.react(bean.id);
}
}
}
}
else {
console.error("Error: Unable to find bean emoji");
}
});
p.s. the whole bean thing is just a test
But every time I run this code it just returns this error and dies:
(node:3084) DeprecationWarning: Collection#find: pass a function instead
Is there anything I missed? I am so stumped...
I never used discord.js so I may be completely wrong
from the warning I'd say you need to do something like
client.emojis.find(emoji => emoji.name === "bean")
Plus after looking at the Discord.js Doc it seems to be the way to go. BUT the docs never say anything about client.emojis.find("name", "bean") being wrong
I've made changes to your code.
I hope it'll help you!
const Discord = require("discord.js");
const client = new Discord.Client();
client.on('ready', () => {
console.log('ready');
});
client.on('message', message => {
var bean = message.guild.emojis.find(emoji => emoji.name == 'bean');
// By guild id
if(message.guild.id == 'your guild id') {
if(bean) {
if(message.content.startsWith("<:bean:" + bean.id + ">")) {
message.react(bean.id);
}
}
}
});
Please check out the switching to v12 discord.js guide
v12 introduces the concept of managers, you will no longer be able to directly use collection methods such as Collection#get on data structures like Client#users. You will now have to directly ask for cache on a manager before trying to use collection methods. Any method that is called directly on a manager will call the API, such as GuildMemberManager#fetch and MessageManager#delete.
In this specific situation, you need to add the cache object to your expression:
var bean = message.guild.emojis.cache?.find(emoji => emoji.name == 'bean');
In case anyone like me finds this while looking for an answer, in v12 you will have to add cache in, making it look like this:
var bean = message.guild.emojis.cache.find(emoji => emoji.name == 'bean');
rather than:
var bean = message.guild.emojis.find(emoji => emoji.name == 'bean');
Is there any way to make a command send a private message to all members of the discord group using discord.js?
Exemple: /private TEST
This message is sent to everyone in the group in private chat instead of channel chat.
You can iterate through Guild.members.
When you receive a message that starts with /private, you take the rest and send it to every member of the guild by using Guild.members.forEach().
Here's a quick example:
client.on('message', msg => {
if (msg.guild && msg.content.startsWith('/private')) {
let text = msg.content.slice('/private'.length); // cuts off the /private part
msg.guild.members.forEach(member => {
if (member.id != client.user.id && !member.user.bot) member.send(text);
});
}
});
This is just a basic implementation, you can obviously use this concept with your command checks or modify that by adding additional text and so on.
Hope this solves the problem for you, let me know if you have any further questions :)
The updated code for discord.js v12 is just adding cache to the forEach.
client.on('message', msg => {
if (msg.guild && msg.content.startsWith('/private')) {
let text = msg.content.slice('/private'.length); // cuts off the /private part
msg.guild.members.cache.forEach(member => {
if (member.id != client.user.id && !member.user.bot) member.send(text);
});
}
});