S
S
Sonada2019-01-29 14:08:50
JavaScript
Sonada, 2019-01-29 14:08:50

How to make a discord bot ban on command?

How to make the bot ban on command? For example, I enter the command: ban @ member # 1234 Time Reason and he banned him

Answer the question

In order to leave comments, you need to log in

4 answer(s)
F
fgvnovoross, 2019-05-30
@Sonada

We need arguments!

var prefix = ":";

client.on("message", message => {
if(!message.guild) return; //чтобы команда не работала в личных сообщениях!
if(message.author === client.user) return; //чтобы бот не написал сам себе
if(!message.content.startsWith(":")) return; //иначе может начать работать с любым префиксом
const args = message.content.slice(prefix.length).trim().split(/ +/g); //вычисляем аргументы
        const command = args.shift().toLowerCase(); //отделяем аргументы от команды
if(command === "ban"){
if(!message.member.hasPermission("BAN_MEMBERS")) return (msg.reply("вы не имеете прав на использование этой команды"));
if(!message.guild.me.hasPermission("BAN_MEMBERS")) return (msg.reply("у меня нет права банить пользователей на этом сервере!"));
let member = message.mentions.members.first();
let arg = args[0];
if(!member) return (msg.reply("вы не указали пользователя!"));
if(!member = arg) return (msg.reply("неправильно введены данные!!"));
let reason = args.slice(1).join(" ");
if(reason) {
member.ban(reason).catch(err => console.log(err));
}else{
member.ban().catch(err => console.log(err));
}
}
});

Phew! It seems that there should be no errors! Unfortunately, there is no time, only a reason. I just don't know how to do it myself. If there are errors - write:
fgvnovoross#3014

A
Artem2046, 2020-04-20
@Artem2046

Unfortunately, there are errors( *in my opinion*
Here is the corrected code :
var prefix = "!";
client.on("message", message => {
if(!message.guild) return; //so that the command does not work in personal if(message.author === client.user
) return; //so that the bot doesn't write to itself
if(!message.content.startsWith("!")) return; //otherwise it can start working with any
const prefix args = message.content.slice(prefix.length).trim().split(/ +/g); //calculate arguments
const command = args.shift().toLowerCase(); //separate arguments from command
if( command === "ban"){
if(!message.member.hasPermission("BAN_MEMBERS")) return (msg.reply("You don't have permission to ban!"));
if(!message.guild.me.hasPermission("BAN_MEMBERS")) return (msg.reply("I don't have ban rights!"));
let member = message.mentions.members.first();
let args = args[0];
if(!member) return (msg.reply("You didn't write a nickname!"));
if(!member ) arg; return (msg.reply("You entered the wrong data!"));
let reason = args.slice(1).join(" ");
if(reason) {
member.ban(reason).catch(err => console.log(err));
}else{
member.ban().catch(err => console.log(err));
}
}
});

A
AMOGUS7, 2021-10-16
@AMOGUS7

where does it lead? var prefix = "!";
client.on("message", message => {
if(!message.guild) return; //so that the command does not work in private messages!
if(message.author === client.user) return; //so that the bot does not wrote himself
if(!message.content.startsWith("!")) return; //otherwise it can start working with any prefix
const args = message.content.slice(prefix.length).trim().split(/ + /g); //calculate the arguments
const command = args.shift().toLowerCase(); //separate the arguments from the command
if(command === "ban"){
if(!message.member.hasPermission("BAN_MEMBERS" )) return (msg.reply("You don't have permission to ban!"));
if(!message.guild.me.hasPermission("BAN_MEMBERS")) return (msg.
let member = message.mentions.members.first();
let args = args[0];
if(!member) return (msg.reply("You didn't write a nickname!"));
if(!member ) arg; return (msg.reply("You entered the wrong data!"));
let reason = args.slice(1).join(" ");
if(reason) {
member.ban(reason).catch(err => console.log(err));
}else{
member.ban().catch(err => console.log(err));
}
}
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question