I tried to code a temp ban command. Unfortunately that command doesn't seem to work and I don't know why. I don't get any error codes it just doesn't work.
case 'tempban':
if(!message.member.permissions.has(Discord.Permissions.FLAGS.BAN_MEMBERS)) return;
const member = message.mentions.first()
if(!member) return;
let time = args[1]
if(!time) return;
let timer = ms(time)
message.channel.send('The User is banned')
await member.ban({reason: `User was banned by ${message.author.tag} for ${time}` })
await setTimeout(async function () {
await message.guild.members.unban(member.id)
}, timer)
break;
Try removing the setTimeout and...
member.ban({ days: days, reason: reason });
Related
My bot is not responding to any commands except for the .purge command.
Here is my code.
const { Client, MessageEmbed } = require('discord.js');
const client = new Client();
const { prefix, token } = require('./config.json');
client.on('ready', () => {
client.user.setStatus('invisible');
console.log('Bot ready!');
client.user.setActivity('Bot is in WIP Do not expect stuff to work', {
type: 'STREAMING',
url: "https://www.twitch.tv/jonkps4"
});
console.log('Changed status!');
});
client.on('message', message => {
if (message.content.startsWith(".") || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
if (message === 'apply') {
message.reply("The Small Developers Application form link is:")
message.reply("https://forms.gle/nb6QwNySjC63wSMUA")
}
if (message === 'kick') {
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.member(user);
// If the member is in the guild
if (member) {
member
.kick('Optional reason that will display in the audit logs')
.then(() => {
// We let the message author know we were able to kick the person
message.reply(`Successfully kicked ${user.tag}`);
})
.catch(err => {
message.reply('I was unable to kick the member \n Maybe due to I having missing permissions or My role is not the higher than the role the person to kick has');
// Log the error
console.error(err);
});
} else {
// The mentioned user isn't in this guild
message.reply("That user isn't in this guild!");
}
// Otherwise, if no user was mentioned
} else {
message.reply("You didn't mention the user to kick!");
}
}
if (command === 'purge') {
const amount = parseInt(args[0]) + 1;
if (isNaN(amount)) {
return message.reply('Not a valid number');
} else if (amount > 100) {
return message.reply('Too many messages to clear. \n In order to clear the whole channel or clear more please either ```1. Right click on the channel and click Clone Channel``` or ```2. Execute this command again but more times and a number less than 100.```');
} else if (amount <= 1) {
return message.reply('Amount of messages to clear **MUST** not be less than 1 or more than 100.')
}
message.channel.bulkDelete(amount, true).catch(err => {
console.error(err);
message.channel.send('**There was an error trying to prune messages in this channel!**');
});
}
});
client.login(token);
I need a specific command to work which is the .apply command
and i would like to know why my embeds do not work.
I tried this embed example It didn't work.
const embed = new MessageEmbed()
// Set the title of the field
.setTitle('A slick little embed')
// Set the color of the embed
.setColor(0xff0000)
// Set the main content of the embed
.setDescription('Hello, this is a slick embed!');
.setThumbnail('https://tr.rbxcdn.com/23e104f6348dd71d597c3246990b9d84/420/420/Decal/Png')
// Send the embed to the same channel as the message
message.channel.send(embed);
What did I do wrong? I am quite new to Discord.JS Any help would be needed.
You used the message parameter instead of command. Instead of message === 'xxx' put command === 'xxx'. Simple mistake, I think that was what you meant anyways. Of course the purge command worked because you put command === 'purge' there
client.on("message", (message) => {
if (message.member.hasPermission(["KICK_MEMBERS", "BAN_MEMBERS"])) {
if (message.content.startsWith(`${prefix}kick`)) {
let member = message.mentions.members.first();
if(!member) return message.channel.send('Cannot find this member');
member.kick().then((member) => {
message.channel.send("```" + member.displayName + " has been kicked ```");
});
}
}
This is the code of kick and ban.^^
https://sourceb.in/2e6ba31dc3 - this is my discord bot code where I want to put the ban and kick command code
You want to put the command inside of the client.on("message", (message) block.
I'll use it in your code as an example:
client.on('message', message =>{
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command === 'ping'){
client.commands.get('ping').execute(message, args);
}
// Kick start
if (command === 'kick') {
// Only check if the command caller has permission,
// AFTER the command is called, not before.
if (message.member.hasPermission(["KICK_MEMBERS", "BAN_MEMBERS"])) {
// Define which member needs to get kicked by grabbing the first mentioned guild member
let member = message.mentions.members.first();
// If the tagged member is not found in your guild,
// throw this message
if(!member) return message.channel.send('Cannot find this member');
// Proceed to kick the member
member.kick().then((member) => {
message.channel.send("```" + member.displayName + " has been kicked ```");
});
}
});
I hope this answers your question. Check the discordjs docs here if you haven't checked them already. They feature a nice tutorial on setting up your bot.
My setTimeout() is used for a certain amount of time as the user states. So, if the user put in 20, the muted member would be muted for 20 minutes. The problem I am having is that the time says it is undefined at the end. I'm not sure why it's not working. The syntax would be something like !mute user time reason.
module.exports = {
name: "mute",
description: "Mute those members!",
execute(message, args) {
const {RichEmbed} = require("discord.js");
setTimeout(() => {
if(!message.member.hasPermission("MANAGE_ROLES")) return message.channel.send("You do not have sufficient permissions to perform this command. Please try again");
let muteMember = message.mentions.members.first() || message.members.get(args[0]);
if(!muteMember) return message.channel.send("You did not provide a member to mute. Please provide a user to mute");
let time = parseInt(args[2]) * 60000;
if(!time) return message.channel.send("You did not provide how long to mute this member. Please provide a time in minutes");
let reason = args.slice(2).join(" ");
if(!reason) reason = "No reason provided";
if(!message.guild.me.hasPermission("MANAGE_ROLES")) return message.channel.send("I do not have sufficient permissions to mute this member. Please try again");
let muteRole = message.guild.roles.find(role => role.name === "Muted");
if(!muteRole) {
try {
muteRole = message.guild.createRole({
name: "Muted",
permissions: []
})
message.guild.channels.forEach(async (channel, id) => {
await channel.overwritePermissions(muteRole, {
SEND_MESSAGE: false,
ADD_REACTIONS: false,
SEND_TTS_MESSAGES: false,
ATTACH_FILES: false,
SPEAK: false
})
})
} catch (e) {
console.log(e.stack);
}
}
muteMember.addRole(muteRole.id). then(() => {
message.delete();
muteMember.send(`You have been muted in ${message.guild.name} for ${reason}`).catch(err =>console.log(err));
message.channel.send(`${muteMember.user.username} has been successfully muted`);
let muteEmbed = new RichEmbed()
.setColor("#FF0000")
.addField("Moderation Action", "mute")
.addField("Muted Member", muteMember.user.username)
.addField("Moderator", message.author.username)
.addField("Reason", reason)
.addField("Date:", message.createdAt.toLocaleAString());
let modChannel = message.guild.channels.find(channel => channel.name === "mod-log");
modChannel.send(muteEmbed);
})
}, time)
},
};
You need to get the time argument first before the reason argument since at the moment you're putting the time variable into the reason variable
if (isNaN(args[1]) return message.reply('Please enter a valid number');
let time = parseInt(args[2]) * 60000;
let reason = args.slice(2).join(" ");
if (!reason) reason = "No reason provided";
if (!message.guild.me.hasPermission("MANAGE_ROLES")) return message.channel.send("I do not have sufficient permissions to mute this member. Please try again");
hello I don't want the mods to do kick action in my server i see discord.js doesnt add guildKickAdd like guildMemberAdd
so how can i block kick or set limit kick ?
this is ban block when someone do ban action bot taking roles and gives him punished.
client.on("guildBanAdd", async function(guild, user) {
const entry = await guild
.fetchAuditLogs({ type: "MEMBER_BAN_ADD" })
.then(audit => audit.entries.first());
const yetkili = await guild.members.get(entry.executor.id);
setTimeout(async () => {
let logs = await guild.fetchAuditLogs({ type: "MEMBER_BAN_ADD" });
if (logs.entries.first().executor.bot) return;
guild.members
.get(logs.entries.first().executor.id)
.removeRoles(guild.members.get(logs.entries.first().executor.id).roles); ///TÜM ROLLERİNİ ALIR
setTimeout(() => {
guild.members
.get(logs.entries.first().executor.id)
.addRole("633026228537917460"); /// VERİLECEK CEZALI ROL İD
}, 3000);
const sChannel = guild.channels.find(c => c.id === "641032067840344064");
const cıks = new Discord.RichEmbed()
.setColor("RANDOM")
.setDescription(
`<#${yetkili.id}> ${user} adlı Kişiye Sağ tık ban Atıldığı için Banlayan Kişinin Yetkileri Alındı`
)
.setFooter("Created by Tokuchi");
sChannel.send(cıks);
guild.owner.send(
`Tokuchi Affetmez † Guard | ** <#${yetkili.id}> İsimili Yetkili <#${user.id}>** Adlı Kişiyi Banladı Ve Yetkilerini Aldım.`
);
}, 2000);
});```
You need to use guildMemberRemove event:
// When a member left. Maybe he left himself, but maybe he was kicked.
client.on("guildMemberRemove", (member) => {
// Get the last kick case of the server
const entry = await guild
.fetchAuditLogs({ type: "MEMBER_KICK" })
.then(audit => audit.entries.first());
// if there's not any kick case in this server
if(!entry) return;
// if the target was not the member who left
if(entry.target.id !== member.id) return;
// Else, you know the member was kicked, and you have the entry so you can do what you want
});
This is the best way to know if someone was kicked.
Well, I followed "The Source Code" "discord.js" tutorial, (even copy-pasted his code from GitHub) but the ban and kick commands he's shown don't work, I assume they got broken by a recent update. It sends the embed to the incidents channel but doesn't actually ban the player. Also, if you have any suggestions for me to change things, please suggest!
if (cmd === `${prefix}ban`) {
let bUser = message.guild.member(message.mentions.users.first() || message.guild.members.get(args[0]));
if (!bUser) return message.channel.send("Can't find user!");
let bReason = args.join(" ").slice(22);
if (!message.member.hasPermission("MANAGE_MEMBERS")) return message.channel.send("No can do pal!");
if (bUser.hasPermission("MANAGE_MESSAGES")) return message.channel.send("That person can't be banned!");
let banEmbed = new Discord.RichEmbed()
.setDescription("Ban Management")
.setColor("#bc0000")
.addField("Banned User", `${bUser} with ID ${bUser.id}`)
.addField("Banned By", `<#${message.author.id}> with ID ${message.author.id}`)
.addField("Banned In", message.channel)
.addField("Time", message.createdAt)
.addField("Reason", bReason);
let incidentchannel = message.guild.channels.find(`name`, "incidents");
if (!incidentchannel) return message.channel.send("Can't find incidents channel.");
message.guild.member(bUser).ban(bReason);
message.delete().catch(O_o => {});
incidentchannel.send(banEmbed);
return;
}
message.guild.member(bUser).ban(bReason);
This will not ban the member. The message has a member property so you don't need to use message.guild.member you can just easily use message.member.
So it should look like this:
if (cmd === `${prefix}ban`) {
let bUser = message.guild.member(message.mentions.members.first() || message.guild.members.get(args[0]));
if (!bUser) return message.channel.send("Can't find user!");
let bReason = args.join(" ").slice(22);
if (!message.member.hasPermission("MANAGE_MEMBERS")) return message.channel.send("No can do pal!");
if (bUser.hasPermission("MANAGE_MESSAGES")) return message.channel.send("That person can't be banned!");
let banEmbed = new Discord.RichEmbed()
.setDescription("Ban Management")
.setColor("#bc0000")
.addField("Banned User", `${bUser.user.tag} with ID ${bUser.id}`)
.addField("Banned By", `<#${message.author.id}> with ID ${message.author.id}`)
.addField("Banned In", message.channel.name)
.addField("Time", message.createdAt)
.addField("Reason", bReason);
let incidentchannel = message.guild.channels.find(`name`, "incidents");
if (!incidentchannel) return message.channel.send("Can't find incidents channel.");
message.guild.member(bUser).ban({
reason: bReason
});
message.delete();
incidentchannel.send({
embed: banEmbed
});
return;
}
I changed a lot because a lot was outdated and could not work. It could be that I haven't seen one or the other mistake.
Let me know if it worked! :)
Best regards,
Monkeyyy11
This seems awfully complicated, I hope my command can make things a bit easier!
bot.on('message', message => {
const arguments = message.content.slice(prefix.length).trim().split(/ +/g);
const commandName = arguments.shift().toLowerCase();
if (message.content.startsWith(prefix) && commandName == "kick") {
if(!message.member.hasPermission("KICK_MEMBERS")) return message.channel.send("Permissions invalid");
const userKick = message.mentions.users.first();
if (userKick) {
var member = message.guild.member(userKick);
if (member) {
member.kick({
reason: `This person was kicked using a bot's moderation system. We are so sorry if this caused problems.`
}).then(() => {
message.reply(`A user been kicked.`)
})
} else {
message.reply(`User not found`);
}
} else {
message.reply(`Please enter a name`)
}}})
bot.on('message', message => {
const arguments = message.content.slice(prefix.length).trim().split(/ +/g);
const commandName = arguments.shift().toLowerCase();
if (message.content.startsWith(prefix) && commandName == "ban") {
if(!message.member.hasPermission("BAN_MEMBERS")) return message.channel.send("Permissions invalid");
const userBan = message.mentions.users.first();
if (userBan) {
var member = message.guild.member(userBan);
if (member) {
member.ban({
reason: `This person was banned using a bot's moderation system. We are so sorry if this caused problems.`
}).then(() => {
message.reply(`a user has been banned!`)
})
} else {
message.reply(`User not found`);
}
} else {
message.reply(`Please enter a name`)
}}})
If you use discord.js v12 or higher, then RichEmbed is now deprecated. Instead, use MessageEmbed