<commit_before>package com.mrpowergamerbr.loritta.utils;

import com.github.kevinsawicki.http.HttpRequest;
import com.mongodb.client.FindIterable;
import com.mongodb.client.model.Filters;
import com.mrpowergamerbr.loritta.Loritta;
import com.mrpowergamerbr.loritta.LorittaLauncher;
import com.mrpowergamerbr.loritta.commands.CommandContext;
import com.mrpowergamerbr.loritta.userdata.LorittaProfile;
import com.mrpowergamerbr.loritta.userdata.ServerConfig;
import com.mrpowergamerbr.loritta.utils.music.GuildMusicManager;
import com.mrpowergamerbr.loritta.utils.reminders.Reminder;
import net.dv8tion.jda.core.OnlineStatus;
import net.dv8tion.jda.core.Permission;
import net.dv8tion.jda.core.entities.*;
import net.dv8tion.jda.core.entities.Message.Attachment;
import net.dv8tion.jda.core.events.guild.member.GuildMemberJoinEvent;
import net.dv8tion.jda.core.events.guild.member.GuildMemberLeaveEvent;
import net.dv8tion.jda.core.exceptions.PermissionException;
import org.apache.commons.io.IOUtils;
import org.bson.Document;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import static com.mrpowergamerbr.loritta.utils.TextUtilsKt.f;

public class LorittaUtils {
	public static final String ERROR = "<:erro:326509900115083266>";

	public static boolean canUploadFiles(CommandContext context) {
		if (!context.isPrivateChannel() && !context.getGuild().getSelfMember().hasPermission(context.event.getTextChannel(), Permission.MESSAGE_ATTACH_FILES)) {
			context.sendMessage(ERROR + " **|** " + context.getAsMention(true) + f(context.locale.IMAGE_UPLOAD_NO_PERM) + " \uD83D\uDE22");
			return false;
		}
		return true;
	}

	public static boolean handleIfBanned(CommandContext context, LorittaProfile profile) {
		if (profile.isBanned()) {
			// Se um usuário está banido...
			context.getUserHandle()
					.openPrivateChannel()
					.complete()
					.sendMessage("\uD83D\uDE45 **|** " + context.getAsMention(true) + f(context.locale.USER_IS_LORITTABANNED, profile.getBanReason())).complete();
			LorittaLauncher.loritta.getIgnoreIds().add(context.getUserHandle().getId());
			return true;
		}
		return false;
	}

	public static void warnOwnerNoPermission(Guild guild, TextChannel textChannel, ServerConfig serverConf) {
		// TODO: Localization
		if (serverConf.warnOnMissingPermission()) {
			for (Member member : guild.getMembers()) {
				if (member.isOwner()) {
					member.getUser().openPrivateChannel().complete().sendMessage("Hey, eu estou sem permissão no **" + textChannel.getName() + "** na guild **" + guild.getName() + "**! Você pode configurar o meu grupo para poder falar lá? Obrigada! 😊").complete();
				}
			}
		}
	}

	public static String replaceTokens(String message, GuildMemberJoinEvent e) {
		message = message.replace("{@user}", e.getMember().getAsMention());
		message = message.replace("{user}", e.getMember().getUser().getName());
		message = message.replace("{nickname}", e.getMember().getEffectiveName());
		message = message.replace("{guild}", e.getGuild().getName());
		return message;
	}

	public static String replaceTokens(String message, GuildMemberLeaveEvent e) {
		message = message.replace("{@user}", e.getMember().getAsMention());
		message = message.replace("{user}", e.getMember().getUser().getName());
		message = message.replace("{nickname}", e.getMember().getEffectiveName());
		message = message.replace("{guild}", e.getGuild().getName());
		return message;
	}

	/**
	 * Verifica se uma imagem é válida (ou seja, diferente de null), caso seja null, a Loritta irá avisar ao usuário que ela não tem nenhuma imagem "utilizável"
	 * @param context
	 * @param image
	 * @return
	 */
	public static boolean isValidImage(CommandContext context, Image image) {
		if (image == null) {
			context.sendMessage(ERROR + " **|** " + context.getAsMention(true) + f(context.locale.NO_VALID_IMAGE));
			return false;
		}
		return true;
	}

	/**
	 * Retorna uma imagem dependendo do contexto
	 *
	 * @param context
	 * @param argument
	 * @return uma BufferedImage com a imagem
	 */
	public static BufferedImage getImageFromContext(CommandContext context, int argument) {
		return getImageFromContext(context, argument, 25);
	}

	/**<commit_msg>:sparkles: | Adicionar novos replacers na mensagem de entrada/saida do servidor<commit_after>package com.mrpowergamerbr.loritta.utils;

import com.github.kevinsawicki.http.HttpRequest;
import com.mongodb.client.FindIterable;
import com.mongodb.client.model.Filters;
import com.mrpowergamerbr.loritta.Loritta;
import com.mrpowergamerbr.loritta.LorittaLauncher;
import com.mrpowergamerbr.loritta.commands.CommandContext;
import com.mrpowergamerbr.loritta.userdata.LorittaProfile;
import com.mrpowergamerbr.loritta.userdata.ServerConfig;
import com.mrpowergamerbr.loritta.utils.music.GuildMusicManager;
import com.mrpowergamerbr.loritta.utils.reminders.Reminder;
import net.dv8tion.jda.core.OnlineStatus;
import net.dv8tion.jda.core.Permission;
import net.dv8tion.jda.core.entities.*;
import net.dv8tion.jda.core.entities.Message.Attachment;
import net.dv8tion.jda.core.events.guild.member.GuildMemberJoinEvent;
import net.dv8tion.jda.core.events.guild.member.GuildMemberLeaveEvent;
import net.dv8tion.jda.core.exceptions.PermissionException;
import org.apache.commons.io.IOUtils;
import org.bson.Document;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import static com.mrpowergamerbr.loritta.utils.TextUtilsKt.f;

public class LorittaUtils {
	public static final String ERROR = "<:erro:326509900115083266>";

	public static boolean canUploadFiles(CommandContext context) {
		if (!context.isPrivateChannel() && !context.getGuild().getSelfMember().hasPermission(context.event.getTextChannel(), Permission.MESSAGE_ATTACH_FILES)) {
			context.sendMessage(ERROR + " **|** " + context.getAsMention(true) + f(context.locale.IMAGE_UPLOAD_NO_PERM) + " \uD83D\uDE22");
			return false;
		}
		return true;
	}

	public static boolean handleIfBanned(CommandContext context, LorittaProfile profile) {
		if (profile.isBanned()) {
			// Se um usuário está banido...
			context.getUserHandle()
					.openPrivateChannel()
					.complete()
					.sendMessage("\uD83D\uDE45 **|** " + context.getAsMention(true) + f(context.locale.USER_IS_LORITTABANNED, profile.getBanReason())).complete();
			LorittaLauncher.loritta.getIgnoreIds().add(context.getUserHandle().getId());
			return true;
		}
		return false;
	}

	public static void warnOwnerNoPermission(Guild guild, TextChannel textChannel, ServerConfig serverConf) {
		// TODO: Localization
		if (serverConf.warnOnMissingPermission()) {
			for (Member member : guild.getMembers()) {
				if (member.isOwner()) {
					member.getUser().openPrivateChannel().complete().sendMessage("Hey, eu estou sem permissão no **" + textChannel.getName() + "** na guild **" + guild.getName() + "**! Você pode configurar o meu grupo para poder falar lá? Obrigada! 😊").complete();
				}
			}
		}
	}

	public static String replaceTokens(String message, GuildMemberJoinEvent e) {
		message = message.replace("{@user}", e.getMember().getAsMention());
		message = message.replace("{user}", e.getMember().getUser().getName());
		message = message.replace("{nickname}", e.getMember().getEffectiveName());
		message = message.replace("{guild}", e.getGuild().getName());
		message = message.replace("{guildsize}", String.valueOf(e.getGuild().getMembers().size()));
		message = message.replace("{@owner}", e.getGuild().getOwner().getAsMention());
		message = message.replace("{owner}", e.getGuild().getOwner().getEffectiveName());
		return message;
	}

	public static String replaceTokens(String message, GuildMemberLeaveEvent e) {
		message = message.replace("{@user}", e.getMember().getAsMention());
		message = message.replace("{user}", e.getMember().getUser().getName());
		message = message.replace("{nickname}", e.getMember().getEffectiveName());
		message = message.replace("{guild}", e.getGuild().getName());
		message = message.replace("{guildsize}", String.valueOf(e.getGuild().getMembers().size()));
		message = message.replace("{@owner}", e.getGuild().getOwner().getAsMention());
		message = message.replace("{owner}", e.getGuild().getOwner().getEffectiveName());
		return message;
	}

	/**
	 * Verifica se uma imagem é válida (ou seja, diferente de null), caso seja null, a Loritta irá avisar ao usuário que ela não tem nenhuma imagem "utilizável"
	 * @param context
	 * @param image
	 * @return
	 */
	public static boolean isValidImage(CommandContext context, Image image) {
		if (image == null) {
			context.sendMessage(ERROR + " **|** " + context.getAsMention(true) + f(context.locale.NO_VALID_IMAGE));
			return false;
		}
		return true;
	}

	/**
	 * Retorna uma imagem dependendo do contexto
	 *
	 * @param context
	 * @param argument
	 * @return uma BufferedImage com a imagem
	 */
	public static BufferedImage getImageFromContext(CommandContext context, int argument) {
		return getImageFromContext(context, argument, 25);
	}

	/**
{'fraction_non_alphanumeric': 0.13862433862433862, 'fraction_numerical': 0.007407407407407408, 'mean_word_length': 5.342953020134228, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1087778', 'n_tokens_mistral': 3254, 'n_tokens_neox': 3023, 'n_words': 579}