Search in sources :

Example 1 with CachedMessage

use of net.kodehawa.mantarobot.core.listeners.entities.CachedMessage in project MantaroBot by Mantaro.

the class MantaroListener method logEdit.

private void logEdit(GuildMessageUpdateEvent event) {
    try {
        String hour = df.format(new Date(System.currentTimeMillis()));
        String logChannel = MantaroData.db().getGuild(event.getGuild()).getData().getGuildLogChannel();
        if (logChannel != null) {
            TextChannel tc = event.getGuild().getTextChannelById(logChannel);
            if (tc == null)
                return;
            User author = event.getAuthor();
            CachedMessage editedMessage = CommandListener.getMessageCache().get(event.getMessage().getId(), Optional::empty).orElse(null);
            if (editedMessage != null && !editedMessage.getContent().isEmpty() && !event.getChannel().getId().equals(logChannel)) {
                if (MantaroData.db().getGuild(event.getGuild()).getData().getLogExcludedChannels().contains(event.getChannel().getId())) {
                    return;
                }
                if (MantaroData.db().getGuild(event.getGuild()).getData().getModlogBlacklistedPeople().contains(editedMessage.getAuthor().getId())) {
                    return;
                }
                tc.sendMessage(String.format(EmoteReference.WARNING + "`[%s]` Message created by **%s#%s** in channel **%s** was modified.\n```diff\n-%s\n+%s```", hour, author.getName(), author.getDiscriminator(), event.getChannel().getName(), editedMessage.getContent().replace("```", ""), event.getMessage().getContentDisplay().replace("```", ""))).queue();
                CommandListener.getMessageCache().put(event.getMessage().getId(), Optional.of(new CachedMessage(event.getAuthor().getIdLong(), event.getMessage().getContentDisplay())));
                logTotal++;
            }
        }
    } catch (Exception e) {
        if (!(e instanceof NullPointerException) && !(e instanceof IllegalArgumentException) && !(e instanceof CacheLoader.InvalidCacheLoadException) && !(e instanceof PermissionException)) {
            log.warn("Unexpected error while logging a edit.", e);
        }
    }
}
Also used : PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) DBUser(net.kodehawa.mantarobot.db.entities.DBUser) CacheLoader(com.google.common.cache.CacheLoader) Date(java.util.Date) PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) ExecutionException(java.util.concurrent.ExecutionException) CachedMessage(net.kodehawa.mantarobot.core.listeners.entities.CachedMessage)

Example 2 with CachedMessage

use of net.kodehawa.mantarobot.core.listeners.entities.CachedMessage in project MantaroBot by Mantaro.

the class CommandListener method onEvent.

@Override
public void onEvent(Event event) {
    if (event instanceof ShardMonitorEvent) {
        if (MantaroBot.getInstance().getShardedMantaro().getShards()[shardId].getEventManager().getLastJDAEventTimeDiff() > 30000)
            return;
        // Hey, this listener is alive! (This won't pass if somehow this is blocked)
        ((ShardMonitorEvent) event).alive(shardId, ShardMonitorEvent.COMMAND_LISTENER);
        return;
    }
    if (event instanceof GuildMessageReceivedEvent) {
        GuildMessageReceivedEvent msg = (GuildMessageReceivedEvent) event;
        // Inserts a cached message into the cache. This only holds the id and the content, and is way lighter than saving the entire jda object.
        messageCache.put(msg.getMessage().getId(), Optional.of(new CachedMessage(msg.getAuthor().getIdLong(), msg.getMessage().getContentDisplay())));
        // Ignore myself and bots.
        if (msg.getAuthor().isBot() || msg.getAuthor().equals(msg.getJDA().getSelfUser()))
            return;
        shard.getCommandPool().execute(() -> onCommand(msg));
    }
}
Also used : ShardMonitorEvent(net.kodehawa.mantarobot.core.listeners.events.ShardMonitorEvent) GuildMessageReceivedEvent(net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent) CachedMessage(net.kodehawa.mantarobot.core.listeners.entities.CachedMessage)

Example 3 with CachedMessage

use of net.kodehawa.mantarobot.core.listeners.entities.CachedMessage in project MantaroBot by Mantaro.

the class MantaroListener method logDelete.

private void logDelete(GuildMessageDeleteEvent event) {
    try {
        String hour = df.format(new Date(System.currentTimeMillis()));
        String logChannel = MantaroData.db().getGuild(event.getGuild()).getData().getGuildLogChannel();
        if (logChannel != null) {
            TextChannel tc = event.getGuild().getTextChannelById(logChannel);
            if (tc == null)
                return;
            CachedMessage deletedMessage = CommandListener.getMessageCache().get(event.getMessageId(), Optional::empty).orElse(null);
            if (deletedMessage != null && !deletedMessage.getContent().isEmpty() && !event.getChannel().getId().equals(logChannel) && !deletedMessage.getAuthor().getId().equals(event.getJDA().getSelfUser().getId())) {
                if (MantaroData.db().getGuild(event.getGuild()).getData().getModlogBlacklistedPeople().contains(deletedMessage.getAuthor().getId())) {
                    return;
                }
                if (MantaroData.db().getGuild(event.getGuild()).getData().getLogExcludedChannels().contains(event.getChannel().getId())) {
                    return;
                }
                logTotal++;
                tc.sendMessage(String.format(EmoteReference.WARNING + "`[%s]` Message created by **%s#%s** in channel **%s** was deleted.\n" + "```diff\n-%s```", hour, deletedMessage.getAuthor().getName(), deletedMessage.getAuthor().getDiscriminator(), event.getChannel().getName(), deletedMessage.getContent().replace("```", ""))).queue();
            }
        }
    } catch (Exception e) {
        if (!(e instanceof IllegalArgumentException) && !(e instanceof NullPointerException) && !(e instanceof CacheLoader.InvalidCacheLoadException) && !(e instanceof PermissionException)) {
            log.warn("Unexpected exception while logging a deleted message.", e);
        }
    }
}
Also used : PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) CacheLoader(com.google.common.cache.CacheLoader) Date(java.util.Date) PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) ExecutionException(java.util.concurrent.ExecutionException) CachedMessage(net.kodehawa.mantarobot.core.listeners.entities.CachedMessage)

Aggregations

CachedMessage (net.kodehawa.mantarobot.core.listeners.entities.CachedMessage)3 CacheLoader (com.google.common.cache.CacheLoader)2 Date (java.util.Date)2 ExecutionException (java.util.concurrent.ExecutionException)2 PermissionException (net.dv8tion.jda.core.exceptions.PermissionException)2 GuildMessageReceivedEvent (net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent)1 ShardMonitorEvent (net.kodehawa.mantarobot.core.listeners.events.ShardMonitorEvent)1 DBUser (net.kodehawa.mantarobot.db.entities.DBUser)1