I want to make a discord bot but I have some problems updating my bot from v11 to v12.
I need help by my kick and ban code
this is my Kick code
var member= message.mentions.members.first();
member.kick().then((member) => {
message.channel.send(member.displayName + " has been successfully kicked :point_right: ");
}).catch(() => {
message.channel.send("Access Denied");
});
}
}
and ban
var member= message.mentions.members.first();
member.ban().then((member) => {
message.channel.send(member.displayName + " has been successfully banned");
}).catch(() => {
message.channel.send("Access Denied");
});
}
}
and I couldn't find it on https://discordjs.guide/additional-info/changes-in-v12.html#before-you-start
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am working on a discord bot i made a ban command but it is available to all users i am using discord.js 12.0
This is my code
const Discord = require("discord.js");
const client = new Discord.Client();
const express = require("express")
const app = express()
app.get("/", (req, res) =>{
console.log("helloe")
})
app.listen(3000, () => {
console.log("Ready")
})
client.once('ready', () => {
console.log('Beast Bot is ready');
client.user.setActivity('Discord Ping Messages || ~help || (Made by CaptainBeast#1394) ', { type:"WATCHING"}).catch(console.error)
});
const ownerId = "602113193489203232";
const owner2Id = "725629309854679092";
client.on("message", async message => {
if (message.author.bot) return false;
if (message.mentions.has(ownerId)) {
message.reply(`Thanks for pinging my owner you will be banned with in 24 hours :))`);
};
if (message.mentions.has(owner2Id)) {
message.reply(`Thanks for pinging my co-owner you will be banned with in 24 hours :))`);
};
if(message.content.startsWith("~mute")) {
if(message.member.hasPermission("KICK_MEMBERS"))
{
message.reply(`Thanks for pinging my co-owner you will be banned with in 24 hours :))`);
}
};
if (!message.guild) return;
// if the message content starts with "!ban"
if (message.content.startsWith('$ban')) {
// Assuming we mention someone in the message, this will return the user
// Read more about mentions over at https://discord.js.org/#/docs/main/master/class/MessageMentions
const user = message.mentions.users.first();
// If we have a user mentioned
if (user) {
// Now we get the member from the user
const member = message.guild.members.resolve(user);
// If the member is in the guild
if (member) {
/**
* Ban the member
* Make sure you run this on a member, not a user!
* There are big differences between a user and a member
* Read more about what ban options there are over at
* https://discord.js.org/#/docs/main/master/class/GuildMember?scrollTo=ban
*/
member
.ban({
reason: 'They were bad!',
})
.then(() => {
// We let the message author know we were able to ban the person
message.channel.send(`Successfully banned ${user.tag}`);
})
.catch(err => {
// An error happened
// This is generally due to the bot not being able to ban the member,
// either due to missing permissions or role hierarchy
message.channel.send('I was unable to ban the member');
// Log the error
console.error(err);
});
} else {
// The mentioned user isn't in this guild
message.channel.send("That user isn't in this guild!");
}
} else {
// Otherwise, if no user was mentioned
message.channel.send("You didn't mention the user to ban!");
}
}
});
// inside a command, event listener, etc.
const help = new Discord.MessageEmbed()
.setColor('#0099ff')
.setTitle('Commands')
.setURL('')
.setAuthor('CaptainBeast', 'https://www.clipartmax.com/png/full/307-3072086_discord-icon-discord-icon-png.png')
.setDescription('Here are the list of some available commands')
.setThumbnail('https://www.clipartmax.com/png/full/307-3072086_discord-icon-discord-icon-png.png')
.addFields(
{ name: 'Commands', value: ':) commands' },
{ name: '\u200B', value: '\u200B' },
{ name: '~help', value: 'Displays list of available commands', inline: true },
{ name: '~ping', value: 'sent pong ', inline: true },
)
.addField('More stuffs coming soon', 'the bot is still under development', true)
.setImage('')
.setTimestamp()
.setFooter('Made by CaptainBeast#1394', 'https://www.clipartmax.com/png/full/307-3072086_discord-icon-discord-icon-png.png');
const prefix = "~";
client.on("message", (message) => {
// Exit and stop if it's not there
if (!message.content.startsWith(prefix)) return;
if (message.content.startsWith(prefix + "help")) {
message.channel.send(help);
} else
if (message.content.startsWith(prefix + "foo")) {
message.channel.send("bar!");
}
if (message.content.startsWith(prefix + "ping")) {
message.channel.send("pong!");
}
});
client.login('censored')
This is my whole code
After your if condition to know if it's the ban command, you don't look at the permission of the person who wrote the message.
You need just to add the condition
For Disord.js V12
if (message.member.hasPermission("BAN_MEMBERS")) {
...
}
For Discord.js V13
if (message.member.permissions.has("BAN_MEMBERS")) {
...
}
I have been trying to fix this error for hours now, but I can't find out why it's happening. It doesn't even send the error message added to the code. When I try to use the command, it sends nothing in chat, and just spams the console with errors. It did all of this when I added the error embed. Here's my code:
if(!message.author.bot) {
if(message.member.roles.cache.some(r => r.name === "Admin") || message.member.roles.cache.some(r => r.name === "Mod")){
const user = message.mentions.users.first();
if (user) {
const member = message.guild.member(user);
if (member) {
member
let reason = args.slice(1).join(' ');
if(!reason) reason = "No reason provided";
let successkick = new Discord.MessageEmbed()
successkick.setTitle("SUCCESS")
successkick.setColor('GREEN')
successkick.setFooter('http://kroxbotweb.deflowo.repl.co/kick.html')
successkick.setThumbnail(member.user.displayAvatarURL())
successkick.setDescription(`${member} has been successfully kicked from the server. \n Reason: ${reason}`);
let errormsg = new Discord.MessageEmbed()
errormsg.setTitle('ERROR')
errormsg.setColor('RED')
errormsg.setFooter('http://kroxbotweb.deflowo.repl.co/kick.html')
errormsg.setThumbnail(member.user.displayAvatarURL())
errormsg.setDescription(`**An error has occurred.** \n **1.** Does this user have higher permissions than me? \n **2.** Did you ping the user? \n **3.** Do they have server permissions? \n \n Targetted user: ${member}`)
try {
member.kick(reason)
} catch (err) {
message.channel.send(errormsg)
return
}
} else {
message.reply("That user isn't in this guild!");
}
} else {
message.reply("You didn't mention the user to kick!");
}
} else {
message.reply("You need a role named Admin/Mod to do this!")
}
} else {
message.channel.send("Nice try.")
}
break;```
I fixed the issue. It was actually that I forgot to put await in try. What it should have looked like is put below. I noticed how with await it worked fine, but without it stopped working strangely. The code was skipping the try and telling me it worked, even though it didn't. I think await stops the code from going forward until it is finished. I hope this helps other people. :)
if(message.member.roles.cache.some(r => r.name === "Admin") || message.member.roles.cache.some(r => r.name === "Mod")){
const user = message.mentions.users.first();
if (user) {
const member = message.guild.member(user);
if (member) {
member
let reason = args.slice(1).join(' ');
if(!reason) reason = "No reason provided";
let successkick = new Discord.MessageEmbed()
successkick.setTitle("SUCCESS")
successkick.setColor('GREEN')
successkick.setFooter('http://kroxbotweb.deflowo.repl.co/kick.html')
successkick.setThumbnail(member.user.displayAvatarURL())
successkick.setDescription(`${member} has been successfully kicked from the server. \n Reason: ${reason}`);
let errormsg = new Discord.MessageEmbed()
errormsg.setTitle('ERROR')
errormsg.setColor('RED')
errormsg.setFooter('http://kroxbotweb.deflowo.repl.co/kick.html')
errormsg.setThumbnail(member.user.displayAvatarURL())
errormsg.setDescription(`**An error has occurred.** \n **1.** Does this user have higher permissions than me? \n **2.** Did you ping the user? \n **3.** Do they have server permissions? \n \n Targetted user: ${member}`)
try {
await member.kick(reason)
} catch (err) {
message.channel.send(errormsg)
return
}
} else {
message.reply("That user isn't in this guild!");
}
} else {
message.reply("You didn't mention the user to kick!");
}
} else {
message.reply("You need a role named Admin/Mod to do this!")
}
} else {
message.channel.send("Nice try.")
}
break;
I'm trying to make a simple discord bot with node.js but when I try to use a command, nothing happens. Nothing that should get logged gets logged on the console except the discord bot going online.
const Prefix = '$';
bot.on('ready', () => {
console.log('The Bot Is Logged In');
});
bot.on('message', (message) => {
if (message.author.bot === true) return;
if (message.content.startsWith === Prefix) {
const [Cmd_name, ...args] = message.content
.trim()
.substring(Prefix.length)
.split(/\s+/);
if (Cmd_name === 'kick') {
if (args.length === 0) return message.reply('Please Provide A User ID');
const member = message.guild.members.cache.get(args[0]);
if (member) {
member.kick();
} else {
message.reply('That member was not found');
}
}
}
});
bot.login(process.env.DISCORD_BOT_TOKEN);
Can someone explain what went wrong?
String.prototype.startsWith is a function, not a string, and thus you must call it as a function.
if (message.content.startsWith(Prefix)) {
so I am working on a command that can take everyone mentioned in the command and gives them each a role.
client.on('message', async message => {
if (message.content.startsWith(prefix + "test")) {
let muteUsers = message.mentions.members (idk this part)
muteUsers.roles.add(rolewhatever)
}
})
Loop through the members and give them the role
client.on('message', async message => {
if (message.content.startsWith(prefix + "test")) {
message.mentions.members.forEach(member => member.roles.add(rolewhatever))
}
})
I'm creating a discord bot using the discord.js v12.2.0+ library. Im creating a !enslave command which gets the bot to ping the target person once every 2 seconds 60 times. So for example !enslave #Hello123 gets the bot to spam this "Torturing #Hello123 :evil:" but the bot doesn't if I do !enslave 35203573250237 and it sends "Torturing undefined :evil:". The long number being the discord user's id.
This is my enslave.js code:
module.exports = {
run: async(client, message, args) => {
if(!message.member.hasPermission('ADMINISTRATOR')) {
message.channel.send("You don't have permission to use that command.");
}
else {
try {
let counter = 0;
message.delete()
let user = message.mentions.members.first() || await message.guild.members.fetch(args[0])
console.log(args[0])
let i = setInterval(function(){
const bot = client.emojis.cache.get('712787936734609419');
message.channel.send(`Torturing ${user} ${bot}`);
counter++;
if(counter === 60) {
clearInterval(i);
}
}, 2000);
}
catch(err) {
console.log(err);
}
}
},
aliases: [],
description: 'Make ImmortusMC torture someone'
}
This is my message.js code:
const PREFIX = process.env.PREFIX;
module.exports = (client, message) => {
if(message.author.bot) return;
if(!message.content.startsWith(PREFIX)) return;
let cmdName = message.content.substring(message.content.indexOf(PREFIX)+1).split(new RegExp(/\s+/)).shift();
let argsToParse = message.content.substring(message.content.indexOf(' ')+1);
if(client.commands.get(cmdName))
client.commands.get(cmdName)(client, message, argsToParse);
else
console.log("Command does not exist.");
};
Use message.guild.members.fetch(args[0]) instead of message.guild.members.cache.get(args[0]).
The reason why you're getting undefined is that the member has not been cached yet so you can't get them using the members cache.
let user = message.mentions.members.first() || await message.guild.members.fetch(args[0])
Sorry for the late answer, but ry:
let user = message.mentions.members.first() || message.guild.members.cache.get(args[0]);
You should use .get() when you are going to use the IDs. I also removed the await and added the ; at the end.