<commit_before>const GReYBot = require('../greybot');

exports.commands = [
	'prune',
	'kick',
	'ban'
]

var lastPruned = new Date().getTime() - (GReYBot.Config.pruneInterval * 1000);

function resolveMention(usertxt){
	var userid = usertxt;
	if(usertxt.startsWith('<@!')){
		userid = usertxt.substr(3,usertxt.length-4);
	} else {
		if(usertxt.startsWith('<@')){
			userid = usertxt.substr(2,usertxt.length-3);
		}
	}
	return userid;
}

exports.prune = {
	usage: '<count|1-100>',
	process: (msg, suffix) => {
		var count = parseInt(suffix) || 0;

		if (msg.member.hasPermission('MANAGE_MESSAGES', true)) {
			var timeSinceLastPrune = Math.floor(new Date().getTime() - lastPruned);

			if (timeSinceLastPrune > (GReYBot.Config.pruneInterval * 1000)) {
				count++;
				if (count > 1) {
					if (count > GReYBot.Config.pruneMax) count = GReYBot.Config.pruneMax;

					msg.channel.fetchMessages({ limit: count })
						.then(messages => messages.map(m => m.delete()))
						.then(function () {
							msg.channel.send({
								embed: {
									color: GReYBot.Config.defaultEmbedColor,
									description: `Pruning ${count - 1} messages...`
								}
							}).then(message => message.delete(5000));
						});

					lastPruned = new Date().getTime();
				}
			}
			else {
				var wait = Math.floor(GReYBot.Config.pruneInterval - (timeSinceLastPrune / 1000));
				msg.channel.send({
					embed: {
						color: GReYBot.Config.defaultEmbedColor,
						description: `You can't do that yet, please wait ${wait} second${wait > 1 ? 's' : ''}`
					}
				}).then(message => message.delete(5000));
			}
		}
		else {
			msg.channel.send({
				embed: {
					color: GReYBot.Config.defaultEmbedColor,
					description: `You can't do that ${msg.member}...`
				}
			}).then(message => message.delete(5000));
		}
	}
}

exports.kick = {
	usage: '<user> [reason]',
	description: 'Kick a user with an optional reason. Requires both the command user and the bot to have kick permission',
	process: (msg, suffix) => {
		let args = suffix.split(' ');
		if (args.length > 0 && args[0]) {
			let hasPermissonToKick =  msg.guild.me.hasPermission('KICK_MEMBERS', true);
			if (!hasPermissonToKick) {
				msg.channel.send( 'I don\'t have permission to kick people!');
				return;
			}
			if (!msg.member.hasPermission('KICK_MEMBERS', true)) {
				msg.channel.send( 'You don\'t have permission to kick people!');
				return;
			}
			let targetId = resolveMention(args[0]);
			msg.guild.fetchMember(targetId).then(member => {
				if (member != undefined) {
					if (!member.kickable) {
						msg.channel.send('I can\'t kick ' + member + '. Do they have the same or a higher role than me?');
						return;
					}
					if (args.length > 1) {
						let reason = args.slice(1).join(' ');
						member.kick(reason).then(x => {
							msg.channel.send('Kicking ' + member + ' from ' + msg.guild + ' for ' + reason + '!');
						}).catch(err => msg.channel.send('Kicking ' + member + ' failed:\n'));
					} else {
						member.kick().then(x => {
							msg.channel.send('Kicking ' + member + ' from ' + msg.guild + '!');
						}).catch(err => msg.channel.send('Kicking ' + member + ' failed:\n'));
					}
				} else {
					msg.channel.send('I couldn\'t find a user ' + args[0]);
				}
			});
		} else {
			msg.channel.send('You must specify a user to kick!');
		}
	}
}

exports.ban = {
	usage: '<user> [days of messages to delete] [reason]',
	description: 'bans the user, optionally deleting messages from them in the last x days',
	process: function(msg, suffix){
		var args = suffix.split(' ');
		if (args.length > 0 && args[0]) {
			if (!msg.guild.me.hasPermission('BAN_MEMBERS', true)) {
				msg.channel.send( 'I don\'t have permission to ban people!');
				return;
			}
			if (!msg.member.hasPermission('BAN_MEMBERS', true)) {
				msg.channel.send( 'You don\'t have permission to ban people!');
				return;
			}
			let targetId = resolveMention(args[0]);
			msg.guild.fetchMember(targetId).then(member => {
				if (member != undefined) {
					if (!member.bannable) {
						msg.channel.send('I can\'t ban ' + member + '. Do they have the same or a higher role than me?');
						return;
					}
					if (args.length > 1) {
						if(typeof args[1] === 'number') {
							if (args.length > 2) {
								let days = args[1];
								let reason = args.slice(2).join(' ');
								member.ban(reason).then(x => {
									msg.channel.send('Banning ' + member + ' from ' + msg.guild + ' for ' + reason + '!');
								}).catch(err => msg.channel.send('Banning ' + member + ' failed:\n'));
							} else {
								let days = args[1];
								member.ban(days).then(x => {
									msg.channel.send('Banning ' + member + ' from ' + msg.guild + '!');
								}).catch(err => msg.channel.send('Banning ' + member + ' failed:\n'));
							}
						} else {
							let reason = args.slice(1).join(' ');
							member.ban(reason).then(x => {
								msg.channel.send('Banning ' + member + ' from ' + msg.guild + ' for ' + reason + '!');
							}).catch(err => msg.channel.send('Banning ' + member + ' failed:\n'));
						}
					} else {
						member.ban().then(x => {
							msg.channel.send('Banning ' + member + ' from ' + msg.guild + '!');
						}).catch(err => msg.channel.send('Banning ' + member + ' failed:\n'));
					}
				}
			});
		} else {
			msg.channel.send('You must specify a user to ban!');
		};
	}
}<commit_msg>Added permissions checks for prune - Resolves #65<commit_after>const GReYBot = require('../greybot');

exports.commands = [
	'ban',
	'kick',
	'prune'
]

var lastPruned = new Date().getTime() - (GReYBot.Config.pruneInterval * 1000);

function resolveMention(usertxt) {
	var userid = usertxt;
	if (usertxt.startsWith('<@!')) {
		userid = usertxt.substr(3, usertxt.length - 4);
	} else {
		if (usertxt.startsWith('<@')) {
			userid = usertxt.substr(2, usertxt.length - 3);
		}
	}
	return userid;
}

exports.ban = {
	usage: '<user> [days of messages to delete] [reason]',
	description: 'bans the user, optionally deleting messages from them in the last x days',
	process: function (msg, suffix) {
		var args = suffix.split(' ');
		if (args.length > 0 && args[0]) {
			if (!msg.guild.me.hasPermission('BAN_MEMBERS', true) || !msg.guild.me.hasPermission('ADMINISTRATOR')) {
				msg.channel.send({
					embed: {
						color: GReYBot.Config.defaultEmbedColor,
						description: `I don't have permission to ban people!`
					}
				}).then(message => message.delete(5000));

				return;
			}

			if (!msg.member.hasPermission('BAN_MEMBERS', true) || !msg.member.hasPermission('ADMINISTRATOR')) {
				msg.channel.send({
					embed: {
						color: GReYBot.Config.defaultEmbedColor,
						description: `You don't have permission to ban people, ${msg.member}!`
					}
				}).then(message => message.delete(5000));

				return;
			}

			msg.guild.fetchMember(resolveMention(args[0])).then(member => {
				if (member != undefined) {
					if (!member.bannable) {
						msg.channel.send({
							embed: {
								color: GReYBot.Config.defaultEmbedColor,
								description: `I can't ban ${member}. Do they have the same or a higher role than me?`
							}
						}).then(message => message.delete(5000));
						return;
					}
					if (args.length > 1) {
						if (typeof args[1] === 'number') {
							if (args.length > 2) {
								let days = args[1];
								let reason = args.slice(2).join(' ');
								member.ban({ days: parseFloat(days), reason: reason }).then(x => {
									msg.channel.send({
										embed: {
											color: GReYBot.Config.defaultEmbedColor,
											description: `Banning ${member} from ${msg.guild} for ${reason}!`
										}
									});
								}).catch(err => msg.channel.send({
									embed: {
										color: GReYBot.Config.defaultEmbedColor,
										description: `Banning ${member} from ${msg.guild} failed!`
									}
								}));
							} else {
								let days = args[1];
								member.ban({ days: parseFloat(days) }).then(x => {
									msg.channel.send(`Banning ${member} from ${msg.guild}!`);
								}).catch(err => msg.channel.send({
									embed: {
										color: GReYBot.Config.defaultEmbedColor,
										description: `Banning ${member} from ${msg.guild} failed!`
									}
								}));
							}
						} else {
							let reason = args.slice(1).join(' ');
							member.ban({ reason: reason }).then(x => {
								msg.channel.send({
									embed: {
										color: GReYBot.Config.defaultEmbedColor,
										description: `Banning ${member} from ${msg.guild} for ${reason}!`
									}
								});
							}).catch(err => msg.channel.send({
								embed: {
									color: GReYBot.Config.defaultEmbedColor,
									description: `Banning ${member} from ${msg.guild} failed!`
								}
							}));
						}
					} else {
						member.ban().then(x => {
							msg.channel.send(`Banning ${member} from ${msg.guild}!`);
						}).catch(err => msg.channel.send({
							embed: {
								color: GReYBot.Config.defaultEmbedColor,
								description: `Banning ${member} from ${msg.guild} failed!`
							}
						}));
					}
				}
			});
		} else {
			msg.channel.send({
				embed: {
					color: GReYBot.Config.defaultEmbedColor,
					description: 'You must specify a user to ban.'
				}
			});
		};
	}
}


exports.kick = {
	usage: '<user> [reason]',
	description: 'Kick a user with an optional reason. Requires both the command user and the bot to have kick permission',
	process: (msg, suffix) => {
		let args = suffix.split(' ');

		if (args.length > 0 && args[0]) {
			if (!msg.guild.me.hasPermission('KICK_MEMBERS', true)) {
				msg.channel.send({
					embed: {
						color: GReYBot.Config.defaultEmbedColor,
						description: `I don't have permission to kick people!`
					}
				}).then(message => message.delete(5000));

				return;
			}

			if (!msg.member.hasPermission('KICK_MEMBERS', true)) {
				msg.channel.send({
					embed: {
						color: GReYBot.Config.defaultEmbedColor,
						description: `I don't have permission to kick people, ${msg.member}!`
					}
				}).then(message => message.delete(5000));

				return;
			}

			msg.guild.fetchMember(resolveMention(args[0])).then(member => {
				if (member != undefined) {
					if (!member.kickable) {
						msg.channel.send(`I can't kick ${member}. Do they have the same or a higher role than me?`);
						return;
					}
					if (args.length > 1) {
						let reason = args.slice(1).join(' ');
						member.kick().then(x => {
							msg.channel.send({
								embed: {
									color: GReYBot.Config.defaultEmbedColor,
									description: `Kicking ${member} from ${msg.guild} for ${reason}!`
								}
							})
						}).catch(err => msg.channel.send(`Kicking ${member} failed!`));
					} else {
						member.kick().then(x => {
							msg.channel.send({
								embed: {
									color: GReYBot.Config.defaultEmbedColor,
									description: `Kicking ${member} from ${msg.guild}!`
								}
							})
						}).catch(err => msg.channel.send({
							embed: {
								color: GReYBot.Config.defaultEmbedColor,
								description: `Kicking ${member} failed!`
							}
						}).then(message => message.delete(5000)));
					}
				} else {
					msg.channel.send(`I couldn't find a user ${args[0]}`);
				}
			});
		} else {
			msg.channel.send({
				embed: {
					color: GReYBot.Config.defaultEmbedColor,
					description: 'You must specify a user to kick.'
				}
			});
		}
	}
}


exports.prune = {
	usage: '<count|1-100>',
	process: (msg, suffix) => {
		if (!suffix) {
			msg.channel.send({
				embed: {
					color: GReYBot.Config.defaultEmbedColor,
					description: 'You must specify a number of messages to prune.'
				}
			}).then(message => message.delete(5000));

			return;
		}

		if (!msg.guild.me.hasPermission('KICK_MEMBERS', true) || !msg.guild.me.hasPermission('ADMINISTRATOR')) {
			msg.channel.send({
				embed: {
					color: GReYBot.Config.defaultEmbedColor,
					description: `I can't prune messages, ${msg.member}...`
				}
			}).then(message => message.delete(5000));

			return;
		}

		if (!msg.member.hasPermission('MANAGE_MESSAGES', true) || !msg.member.hasPermission('ADMINISTRATOR')) {
			msg.channel.send({
				embed: {
					color: GReYBot.Config.defaultEmbedColor,
					description: `You can't prune messages, ${msg.member}...`
				}
			}).then(message => message.delete(5000));

			return;
		}

		var timeSinceLastPrune = Math.floor(new Date().getTime() - lastPruned);

		if (timeSinceLastPrune > (GReYBot.Config.pruneInterval * 1000)) {
			let count = parseInt(suffix)++ || 0;
			if (count > 1) {
				if (count > GReYBot.Config.pruneMax) count = GReYBot.Config.pruneMax;

				msg.channel.fetchMessages({ limit: count })
					.then(messages => messages.map(m => m.delete()))
					.then(function () {
						msg.channel.send({
							embed: {
								color: GReYBot.Config.defaultEmbedColor,
								description: `Pruning ${(count === 100) ? 100 : count - 1} messages...`
							}
						}).then(message => message.delete(5000));
					});

				lastPruned = new Date().getTime();
			}
		} else {
			var wait = Math.floor(GReYBot.Config.pruneInterval - (timeSinceLastPrune / 1000));
			msg.channel.send({
				embed: {
					color: GReYBot.Config.defaultEmbedColor,
					description: `You can't do that yet, please wait ${wait} second${wait > 1 ? 's' : ''}`
				}
			}).then(message => message.delete(5000));
		}
	}
}
{'fraction_non_alphanumeric': 0.17208499136571814, 'fraction_numerical': 0.010811622494181244, 'mean_word_length': 2.5873956369512525, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3476691', 'n_tokens_mistral': 6041, 'n_tokens_neox': 4448, 'n_words': 951}