Search in sources :

Example 81 with EmbedBuilder

use of sx.blah.discord.util.EmbedBuilder in project DisCal-Discord-Bot by NovaFox161.

the class Logger method debug.

public void debug(@Nullable IUser author, String message, @Nullable String info, Class clazz, boolean post) {
    String timeStamp = new SimpleDateFormat("yyyy/MM/dd-HH:mm:ss").format(Calendar.getInstance().getTime());
    if (Main.client != null) {
        if (post) {
            IUser bot = Main.getSelfUser();
            EmbedBuilder em = new EmbedBuilder();
            assert bot != null;
            em.withAuthorIcon(bot.getAvatarURL());
            if (author != null) {
                em.withAuthorName(author.getName());
                em.withThumbnail(author.getAvatarURL());
            }
            em.withColor(239, 15, 0);
            em.withFooterText(clazz.getName());
            em.appendField("Time", timeStamp, true);
            if (info != null) {
                em.appendField("Additional Info", info, true);
            }
            // Get DisCal guild and channel..
            IGuild guild = Main.client.getGuildByID(266063520112574464L);
            IChannel channel = guild.getChannelByID(302249332244217856L);
            Message.sendMessage(em.build(), "```" + message + "```", channel);
        }
    }
    // ALWAYS LOG TO FILE!
    try {
        FileWriter file = new FileWriter(debugFile, true);
        file.write("DEBUG --- " + timeStamp + " ---" + MessageUtils.lineBreak);
        if (author != null) {
            file.write("user: " + author.getName() + "#" + author.getDiscriminator() + MessageUtils.lineBreak);
        }
        if (message != null) {
            file.write("message: " + message + MessageUtils.lineBreak);
        }
        if (info != null) {
            file.write("info: " + info + MessageUtils.lineBreak);
        }
        file.close();
    } catch (IOException io) {
        io.printStackTrace();
    }
}
Also used : EmbedBuilder(sx.blah.discord.util.EmbedBuilder) IChannel(sx.blah.discord.handle.obj.IChannel) FileWriter(java.io.FileWriter) IUser(sx.blah.discord.handle.obj.IUser) IGuild(sx.blah.discord.handle.obj.IGuild) IOException(java.io.IOException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 82 with EmbedBuilder

use of sx.blah.discord.util.EmbedBuilder in project de-DiscordBot by DACH-Discord.

the class DiscordBot method handleMessage.

/**
 *  Erhaltene/geänderte Nachricht verarbeiten
 */
private void handleMessage(final IMessage message) {
    final String messageContent = message.getContent();
    // Message doesn't start with the prefix
    if (!messageContent.startsWith(this.prefix)) {
        return;
    }
    final String messageCommand = (messageContent.contains(" ") ? // Message has Arguments
    messageContent.substring(this.prefix.length(), messageContent.indexOf(' ')) : // Message doesn't have arguments
    messageContent.substring(this.prefix.length())).toLowerCase();
    if (messageCommand.equalsIgnoreCase("help")) {
        this.command_help(message);
        return;
    }
    if (commands.containsKey(messageCommand)) {
        final Command command = commands.get(messageCommand);
        final int userPermissionLevel = this.getUserPermissionLevel(message.getAuthor(), message.getGuild());
        if (userPermissionLevel < command.permissionLevel) {
            Util.sendMessage(message.getChannel(), "Dieser Befehl ist für deine Gruppe (" + CommandPermissions.getPermissionLevelName(userPermissionLevel) + ") nicht verfügbar.");
            return;
        }
        if (message.getChannel().isPrivate() && !command.pmAllowed) {
            Util.sendMessage(message.getChannel(), "Dieser Befehl ist nicht in Privatnachrichten verfügbar!");
            return;
        }
        try {
            final int parameterCount = command.parameterCount;
            final boolean passContext = command.passContext;
            ArrayList<String> params = parseParameters(messageContent, parameterCount, passContext);
            switch(parameterCount) {
                case 0:
                    {
                        command.method.invoke(command.object, message);
                        break;
                    }
                case 1:
                    {
                        command.method.invoke(command.object, message, params.get(0));
                        break;
                    }
                case 2:
                    {
                        command.method.invoke(command.object, message, params.get(0), params.get(1));
                        break;
                    }
                case 3:
                    {
                        command.method.invoke(command.object, message, params.get(0), params.get(1), params.get(2));
                        break;
                    }
                case 4:
                    {
                        command.method.invoke(command.object, message, params.get(0), params.get(1), params.get(2), params.get(3));
                        break;
                    }
                case 5:
                    {
                        command.method.invoke(command.object, message, params.get(0), params.get(1), params.get(2), params.get(3), params.get(4));
                        break;
                    }
                default:
                    {
                        Util.error(new RuntimeException("Invalid number of parameters!"), message.getChannel());
                    }
            }
        } catch (IllegalAccessException | InvocationTargetException e) {
            final Throwable cause = e.getCause();
            cause.printStackTrace(System.err);
            System.err.println(cause.getMessage());
            final EmbedBuilder embedBuilder = new EmbedBuilder();
            embedBuilder.withColor(new Color(255, 42, 50));
            embedBuilder.appendField("Fehler aufgetreten", cause.toString(), false);
            embedBuilder.withFooterText("Mehr Infos in der Konsole");
            Util.sendEmbed(message.getChannel(), embedBuilder.build());
        }
    }
}
Also used : EmbedBuilder(sx.blah.discord.util.EmbedBuilder) Color(java.awt.Color) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 83 with EmbedBuilder

use of sx.blah.discord.util.EmbedBuilder in project de-DiscordBot by DACH-Discord.

the class UserLog method userJoinNotify.

private void userJoinNotify(final IUser user) {
    final LocalDateTime joinTimeStamp = user.getCreationDate();
    int joinedDays = (int) joinTimeStamp.until(LocalDateTime.now(), ChronoUnit.DAYS);
    // String für Embed
    String embedString = "**Name:** " + user.getName() + '#' + user.getDiscriminator() + '\n' + "**ID:** " + user.getStringID() + '\n' + "**Discord beigetreten:** vor " + joinedDays + " Tagen";
    if (joinedDays <= 1) {
        embedString = embedString + '\n' + ":exclamation: Neuer Nutzer! :exclamation:";
    }
    // Embed
    final EmbedBuilder embedBuilder = new EmbedBuilder();
    embedBuilder.appendField(":white_check_mark: Nutzer ist dem Server beigetreten!", embedString, false);
    embedBuilder.withThumbnail(user.getAvatarURL());
    embedBuilder.withFooterText(LocalDateTime.now().format(DateTimeFormatter.ofPattern("dd.MM. | HH:mm")));
    embedBuilder.withColor(new Color(119, 178, 85));
    final EmbedObject embedObject = embedBuilder.build();
    Util.sendEmbed(userLogChannel, embedObject);
}
Also used : LocalDateTime(java.time.LocalDateTime) EmbedBuilder(sx.blah.discord.util.EmbedBuilder) EmbedObject(sx.blah.discord.api.internal.json.objects.EmbedObject)

Example 84 with EmbedBuilder

use of sx.blah.discord.util.EmbedBuilder in project Shadbot by Shadorc.

the class MoneyStatsManager method getAverageEmbed.

public static EmbedBuilder getAverageEmbed() {
    Map<String, AtomicLong> moneyGained = MONEY_STATS_MAP.getOrDefault(MoneyEnum.MONEY_GAINED, Collections.emptyMap());
    Map<String, AtomicLong> moneyLost = MONEY_STATS_MAP.getOrDefault(MoneyEnum.MONEY_LOST, Collections.emptyMap());
    EmbedBuilder embed = EmbedUtils.getDefaultEmbed().withAuthorName("Stats: average");
    List<String> gamesName = new ArrayList<>();
    gamesName.addAll(moneyGained.keySet());
    gamesName.addAll(moneyLost.keySet());
    gamesName = gamesName.stream().distinct().collect(Collectors.toList());
    if (gamesName.isEmpty()) {
        return embed.withDescription("No statistics yet.");
    }
    Map<String, AtomicInteger> commandsUsed = CommandStatsManager.get(CommandEnum.COMMAND_USED);
    Map<String, Pair<Float, Long>> averageMap = new HashMap<>();
    for (String gameName : gamesName) {
        long gains = moneyGained.getOrDefault(gameName, new AtomicLong(0)).get();
        long losses = moneyLost.getOrDefault(gameName, new AtomicLong(0)).get();
        long usages = commandsUsed.get(gameName).get();
        float average = ((float) gains - losses) / usages;
        averageMap.put(gameName, new Pair<Float, Long>(average, usages));
    }
    Comparator<Entry<String, Pair<Float, Long>>> comparator = (v1, v2) -> v1.getValue().getSecond().compareTo(v2.getValue().getSecond());
    Map<String, Pair<Float, Long>> sortedMap = Utils.sortByValue(averageMap, comparator.reversed());
    return embed.appendField("Name", FormatUtils.format(sortedMap.keySet().stream(), Object::toString, "\n"), true).appendField("Average", FormatUtils.format(sortedMap.values().stream().map(Pair::getFirst), num -> FormatUtils.formatNum(num.intValue()), "\n"), true).appendField("Count", FormatUtils.format(sortedMap.values().stream().map(Pair::getSecond), num -> FormatUtils.formatNum(num), "\n"), true);
}
Also used : StatsJSON(me.shadorc.shadbot.data.stats.annotation.StatsJSON) LottoManager(me.shadorc.shadbot.data.lotto.LottoManager) HashMap(java.util.HashMap) StatsInit(me.shadorc.shadbot.data.stats.annotation.StatsInit) FormatUtils(me.shadorc.shadbot.utils.FormatUtils) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Pair(me.shadorc.shadbot.utils.object.Pair) AtomicLong(java.util.concurrent.atomic.AtomicLong) EmbedBuilder(sx.blah.discord.util.EmbedBuilder) List(java.util.List) JSONObject(org.json.JSONObject) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) AbstractCommand(me.shadorc.shadbot.core.command.AbstractCommand) Entry(java.util.Map.Entry) CommandEnum(me.shadorc.shadbot.data.stats.CommandStatsManager.CommandEnum) EmbedUtils(me.shadorc.shadbot.utils.embed.EmbedUtils) Comparator(java.util.Comparator) Collections(java.util.Collections) Utils(me.shadorc.shadbot.utils.Utils) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AtomicLong(java.util.concurrent.atomic.AtomicLong) EmbedBuilder(sx.blah.discord.util.EmbedBuilder) Entry(java.util.Map.Entry) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicLong(java.util.concurrent.atomic.AtomicLong) Pair(me.shadorc.shadbot.utils.object.Pair)

Example 85 with EmbedBuilder

use of sx.blah.discord.util.EmbedBuilder in project Shadbot by Shadorc.

the class EmbedUtils method getStatsEmbed.

public static <K, V extends Number> EmbedBuilder getStatsEmbed(Map<K, V> statsMap, String name) {
    EmbedBuilder embed = EmbedUtils.getDefaultEmbed().withAuthorName(String.format("Stats: %s", name.toLowerCase()));
    if (statsMap == null) {
        return embed.withDescription("No statistics yet.");
    }
    Comparator<? super Map.Entry<K, V>> comparator = (v1, v2) -> new BigDecimal(v1.getValue().toString()).compareTo(new BigDecimal(v2.getValue().toString()));
    Map<K, V> sortedMap = Utils.sortByValue(statsMap, comparator.reversed());
    return embed.appendField("Name", FormatUtils.format(sortedMap.keySet().stream(), key -> key.toString().toLowerCase(), "\n"), true).appendField("Value", FormatUtils.format(sortedMap.values().stream(), value -> FormatUtils.formatNum(Long.parseLong(value.toString())), "\n"), true);
}
Also used : BigDecimal(java.math.BigDecimal) EmbedBuilder(sx.blah.discord.util.EmbedBuilder) Map(java.util.Map) Comparator(java.util.Comparator) Config(me.shadorc.shadbot.Config) FormatUtils(me.shadorc.shadbot.utils.FormatUtils) Shadbot(me.shadorc.shadbot.Shadbot) Utils(me.shadorc.shadbot.utils.Utils) EmbedBuilder(sx.blah.discord.util.EmbedBuilder) Map(java.util.Map) BigDecimal(java.math.BigDecimal)

Aggregations

EmbedBuilder (sx.blah.discord.util.EmbedBuilder)103 IOException (java.io.IOException)19 Random (java.util.Random)17 IUser (sx.blah.discord.handle.obj.IUser)14 MissingArgumentException (me.shadorc.shadbot.exception.MissingArgumentException)13 LoadingMessage (me.shadorc.shadbot.utils.object.LoadingMessage)13 EmbedObject (sx.blah.discord.api.internal.json.objects.EmbedObject)12 JSONObject (org.json.JSONObject)11 IMessage (sx.blah.discord.handle.obj.IMessage)10 List (java.util.List)9 AbstractCommand (me.shadorc.shadbot.core.command.AbstractCommand)9 EmbedUtils (me.shadorc.shadbot.utils.embed.EmbedUtils)9 JSONException (org.json.JSONException)9 EventColor (com.cloudcraftgaming.discal.api.enums.event.EventColor)8 Utils (me.shadorc.shadbot.utils.Utils)8 IChannel (sx.blah.discord.handle.obj.IChannel)8 EventData (com.cloudcraftgaming.discal.api.object.event.EventData)7 FormatUtils (me.shadorc.shadbot.utils.FormatUtils)7 File (java.io.File)6 CommandCategory (me.shadorc.shadbot.core.command.CommandCategory)6