I have these two lines of code which checks if the message author have a specific role into the main server of the bot :
const main = client.guilds.cache.get(698725823464734852);
// Blacklist Checker :
if (message.author.main.roles.cache.find(r => r.name === "BlackListed.")) message.reply("you are blacklisted...")
I have tried to connect the functions at message.author.main.roles.cache.find however, it didn't work.
There are two issues with the code you have provided us with;
Firstly the guild ID must be a string and secondly message.author.main will give you an error.
Please see the working code I have provided below.
const Discord = require('discord.js'); //Define discord
const client = new Discord.Client(); //Define client
client.on('message', message => {
const guild = client.guilds.cache.get('698725823464734852'); // Define guild (guild ID must be a string)
const member = guild.members.cache.get(message.author.id); //Find the message author in the guild
if (!member) return console.log('Member is not blacklisted'); //If the member is notin the guild
if (member.roles.cache.find(r => r.name.toLowerCase() === 'blacklisted')) return message.reply('Unfortunately, you have been blacklisted.'); //Let the user know they have been blacklisted.
});
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}`)
This question already has answers here:
Discord.js bot leave guild
(5 answers)
Closed 1 year ago.
const Discord = require('discord.js')
const client = new Discord.Client({ws: {intents: Discord.Intents.ALL}});
const botowner = 'xxx'
exports.run = async (bot,message,args) => {
let guildid = message.guild.id
if (message.author.id = botowner) {
toleave = client.get_server(guildid)
await client.leave_server(toleave)
} else {message.channel.send("You are not the bot owner")}
}
exports.help = {
name: ['leavediscordserver']
}
Whenever I run this code the following shows up:
Not sure why this is happening.
The error message tells you all you need to know. You attempt to use a function called get_server(), but this function does not exist ergo you cannot use it.
A discord server in Discord.js is referred to as a Guild so to get your guild you can just call const guild = client.guilds.cache.fetch(guildid) and to leave it all you have to do is call guild.leave().
As a separate issue, a comparison in an if statement is made with 2 equal signs == not 1 as you did in your if (message.author.id = botowner) line.
var guildID = client.guilds.cache.get(args[0]) //your argument would be the server id
if (message.author.id == botowner) { // use == to check if author and bot owner id match
await guildID.leave() // waiting for bot to leave the server
} else {message.channel.send("You are not the bot owner")}
}
Heyo,
I want my bot to send a embed message to my private discord server when it joins & leaves a server. But the problem is that it does not send anything anywhere. My code looks like this:
exports.run = async (client, guild) => {
if(!guild.available) return
if(!guild.owner && guild.ownerID) await guild.members.fetch(guild.ownerID);
if(!channel) return;
const embed = new MessageEmbed()
.setTitle(`Bot joined a server`)
.setDescription(`${guild.name}`)
.setColor(0x9590EE)
.setThumbnail(guild.iconURL())
.addField(`Owner", "${guild.owner.user.tag}`)
.addField(`Member Count", "${guild.memberCount}`)
.setFooter(`${guild.id}`)
client.channels.cache.get('ID').send(embed)
}
Your code doesn't activate upon joining the server. For that you have a nice event (that has a misleading name) guildCreate - it is emitted whenever the client joins a guild.
So, your code should look something like this
client.on('guildCreate', async guild => {
let YourChannel = await client.channels.fetch('channelid');
const embed = new Discord.MessageEmbed()
.setTitle(`Bot joined a server`)
.setDescription(`${guild.name}`)
.setColor(0x9590EE)
.setThumbnail(guild.iconURL())
.addField(`Owner`, `${guild.owner.user.tag}`)
.addField(`Member Count`, `${guild.memberCount}`)
.setFooter(`${guild.id}`)
YourChannel.send(embed);
});
Same works for leaving the guild, use guildDelete event.
Revoking A Ban Using Code
So, I am making a moderation discord bot using VS Code and I have already set up a ban command. I want to make a unban command (revoking a ban) too so that the user can easily unban a user and not have to go into Server Settings to do it.
I know you can do it because I have been using another bot, GAwesomeBot, that is able to do it.
Link to GAwesomeBot: https://gawesomebot.com
I am a little new to Stack Overflow and this is my first question so pardon me if I am doing anything wrong.
Consider using GuildMemberManager#unban
https://discord.js.org/#/docs/main/stable/class/GuildMemberManager?scrollTo=unban
let guildMemberManager, toUnbanSnowflake;
guildMemberManager.unban(toUnbanSnowflake); // Takes UserResolveable as argument
First you want to define the user that you are unbanning.
Because the user is already banned you will have to mention the user by their ID and then unbanning them.
let args = message.content.split(/ +/g); //Split the message by every space
let user = message.guild.members.cache.get(args[1]); //getting the user
user.unban({ reason: args[2].length > 0 ? args[2] : 'No reason provided.' }); //Unbanning the user
The full example:
//Define your variables
const Discord = require('discord.js');
const client = new Discord.Client();
var prefix = 'your-prefix-here';
//Add a message event listener
client.on('message', () => {
let args = message.content.split(/ +/g); //Split the message by every space
if (message.content.toLowerCase() === prefix + 'unban') {
let user = message.guild.members.cache.get(args[1]); //getting the user
if (!user) return message.channel.send('Please specify a user ID');
user.unban({ reason: args[2].length > 0 ? args[2] : 'No reason provided.' }).then(() => message.channel.send('Success');
}
});
Could someone help me set command to set channel for specific server
so that it does not interfere with each other? Actually I have this:
var testChannel = bot.channels.find(channel => channel.id === "hereMyChannelID");
I want to set command which Owner can use to set channel id for his server.
You can accomplish this task by creating a JSON file to hold the specified channels of each guild. Then, in your command, simply define the channel in the JSON. After that, anywhere else in your code, you can then find the channel specified by a guild owner and interact with it.
Keep in mind, a database would be a better choice due to the speed comparison and much lower risk of corruption. Find the right one for you and your code, and replace this JSON setup with the database.
guilds.json setup:
{
"guildID": {
"channel": "channelID"
}
}
Command code:
// -- Define these variables outside of the command. --
const guilds = require('./guilds.json');
const fs = require('fs');
// ----------------------------------------------------
const args = message.content.trim().split(/ +/g); // Probably already declared.
try {
if (message.author.id !== message.guild.ownerID) return await message.channel.send('Access denied.');
if (!message.mentions.channels.first()) return await message.channel.send('Invalid channel.');
guilds[message.guild.id].channel = message.mentions.channels.first().id;
fs.writeFileSync('./guilds.json', JSON.stringify(guilds));
await message.channel.send('Successfully changed channel.');
} catch(err) {
console.error(err);
}
Somewhere else:
const guilds = require('./guilds.json');
const channel = client.channels.get(guilds[message.guild.id].channel);
if (channel) {
channel.send('Found the right one!')
.catch(console.error);
} else console.error('Invalid or undefined channel.');