Search in sources :

Example 1 with GameChatMessagePostProcessEvent

use of github.scarsz.discordsrv.api.events.GameChatMessagePostProcessEvent in project DiscordSRV by Scarsz.

the class DiscordSRV method processChatMessage.

public void processChatMessage(Player player, String message, String channel, boolean cancelled) {
    // log debug message to notify that a chat message was being processed
    debug("Chat message received, canceled: " + cancelled);
    if (player == null) {
        debug("Received chat message was from a null sender, not processing message");
        return;
    }
    // return if player doesn't have permission
    if (!player.hasPermission("discordsrv.chat")) {
        debug("User " + player.getName() + " sent a message but it was not delivered to Discord due to lack of permission");
        return;
    }
    // return if mcMMO is enabled and message is from party or admin chat
    if (Bukkit.getPluginManager().isPluginEnabled("mcMMO")) {
        boolean usingAdminChat = com.gmail.nossr50.api.ChatAPI.isUsingAdminChat(player);
        boolean usingPartyChat = com.gmail.nossr50.api.ChatAPI.isUsingPartyChat(player);
        if (usingAdminChat || usingPartyChat)
            return;
    }
    // return if event canceled
    if (getConfig().getBoolean("DontSendCanceledChatEvents") && cancelled) {
        debug("User " + player.getName() + " sent a message but it was not delivered to Discord because the chat event was canceled and DontSendCanceledChatEvents is true");
        return;
    }
    // return if should not send in-game chat
    if (!getConfig().getBoolean("DiscordChatChannelMinecraftToDiscord")) {
        debug("User " + player.getName() + " sent a message but it was not delivered to Discord because DiscordChatChannelMinecraftToDiscord is false");
        return;
    }
    // return if doesn't match prefix filter
    if (!DiscordUtil.strip(message).startsWith(getConfig().getString("DiscordChatChannelPrefix"))) {
        debug("User " + player.getName() + " sent a message but it was not delivered to Discord because the message didn't start with \"" + getConfig().getString("DiscordChatChannelPrefix") + "\" (DiscordChatChannelPrefix): \"" + message + "\"");
        return;
    }
    GameChatMessagePreProcessEvent preEvent = (GameChatMessagePreProcessEvent) api.callEvent(new GameChatMessagePreProcessEvent(channel, message, player));
    if (preEvent.isCancelled()) {
        DiscordSRV.debug("GameChatMessagePreProcessEvent was cancelled, message send aborted");
        return;
    }
    // update channel from event in case any listeners modified it
    channel = preEvent.getChannel();
    // update message from event in case any listeners modified it
    message = preEvent.getMessage();
    String userPrimaryGroup = VaultHook.getPrimaryGroup(player);
    boolean hasGoodGroup = StringUtils.isNotBlank(userPrimaryGroup);
    // capitalize the first letter of the user's primary group to look neater
    if (hasGoodGroup)
        userPrimaryGroup = userPrimaryGroup.substring(0, 1).toUpperCase() + userPrimaryGroup.substring(1);
    String discordMessage = (hasGoodGroup ? LangUtil.Message.CHAT_TO_DISCORD.toString() : LangUtil.Message.CHAT_TO_DISCORD_NO_PRIMARY_GROUP.toString()).replaceAll("%time%|%date%", TimeUtil.timeStamp()).replace("%channelname%", channel != null ? channel.substring(0, 1).toUpperCase() + channel.substring(1) : "").replace("%primarygroup%", userPrimaryGroup).replace("%username%", DiscordUtil.strip(DiscordUtil.escapeMarkdown(player.getName()))).replace("%world%", player.getWorld().getName()).replace("%worldalias%", DiscordUtil.strip(MultiverseCoreHook.getWorldAlias(player.getWorld().getName())));
    if (PluginUtil.pluginHookIsEnabled("placeholderapi"))
        discordMessage = me.clip.placeholderapi.PlaceholderAPI.setPlaceholders(player, discordMessage);
    discordMessage = discordMessage.replace("%displayname%", DiscordUtil.strip(DiscordUtil.escapeMarkdown(player.getDisplayName()))).replace("%message%", DiscordUtil.strip(message));
    discordMessage = DiscordUtil.strip(discordMessage);
    if (getConfig().getBoolean("DiscordChatChannelTranslateMentions"))
        discordMessage = DiscordUtil.convertMentionsFromNames(discordMessage, getMainGuild());
    GameChatMessagePostProcessEvent postEvent = (GameChatMessagePostProcessEvent) api.callEvent(new GameChatMessagePostProcessEvent(channel, discordMessage, player, preEvent.isCancelled()));
    if (postEvent.isCancelled()) {
        DiscordSRV.debug("GameChatMessagePostProcessEvent was cancelled, message send aborted");
        return;
    }
    // update channel from event in case any listeners modified it
    channel = postEvent.getChannel();
    // update message from event in case any listeners modified it
    discordMessage = postEvent.getProcessedMessage();
    if (!getConfig().getBoolean("Experiment_WebhookChatMessageDelivery")) {
        if (channel == null) {
            DiscordUtil.sendMessage(getMainTextChannel(), discordMessage);
        } else {
            DiscordUtil.sendMessage(getDestinationTextChannelForGameChannelName(channel), discordMessage);
        }
    } else {
        if (channel == null)
            channel = getMainChatChannel();
        TextChannel destinationChannel = getDestinationTextChannelForGameChannelName(channel);
        if (!DiscordUtil.checkPermission(destinationChannel.getGuild(), Permission.MANAGE_WEBHOOKS)) {
            DiscordSRV.error("Couldn't deliver chat message as webhook because the bot lacks the \"Manage Webhooks\" permission.");
            return;
        }
        if (PluginUtil.pluginHookIsEnabled("placeholderapi"))
            message = me.clip.placeholderapi.PlaceholderAPI.setPlaceholders(player, message);
        message = DiscordUtil.strip(message);
        if (getConfig().getBoolean("DiscordChatChannelTranslateMentions"))
            message = DiscordUtil.convertMentionsFromNames(message, getMainGuild());
        WebhookUtil.deliverMessage(destinationChannel, player, message);
    }
}
Also used : TextChannel(net.dv8tion.jda.core.entities.TextChannel) GameChatMessagePreProcessEvent(github.scarsz.discordsrv.api.events.GameChatMessagePreProcessEvent) GameChatMessagePostProcessEvent(github.scarsz.discordsrv.api.events.GameChatMessagePostProcessEvent)

Aggregations

GameChatMessagePostProcessEvent (github.scarsz.discordsrv.api.events.GameChatMessagePostProcessEvent)1 GameChatMessagePreProcessEvent (github.scarsz.discordsrv.api.events.GameChatMessagePreProcessEvent)1 TextChannel (net.dv8tion.jda.core.entities.TextChannel)1