Skip to content

Commit

Permalink
Cancel discord message event
Browse files Browse the repository at this point in the history
Prevents integration channels from registering to essdiscord, and stops messages with invalid message types from being sent.
  • Loading branch information
Draycia committed Sep 27, 2024
1 parent 993613b commit cba044d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* CarbonChat
*
* Copyright (c) 2024 Josua Parks (Vicarious)
* Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.draycia.carbon.paper.integration;

import net.draycia.carbon.common.channels.ConfigChatChannel;

public abstract class IntegrationChannel extends ConfigChatChannel {
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
import com.massivecraft.factions.Faction;
import com.massivecraft.factions.perms.Relation;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.common.channels.ConfigChatChannel;
import net.draycia.carbon.paper.integration.IntegrationChannel;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.framework.qual.DefaultQualifier;
import org.jspecify.annotations.NonNull;

@DefaultQualifier(NonNull.class)
abstract class AbstractFactionsChannel extends ConfigChatChannel {
abstract class AbstractFactionsChannel extends IntegrationChannel {

protected final @Nullable Faction faction(final CarbonPlayer player) {
final FPlayer fPlayer = FPlayers.getInstance().getById(player.uuid().toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
import net.draycia.carbon.api.channels.ChannelPermissions;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.users.UserManager;
import net.draycia.carbon.common.channels.ConfigChatChannel;
import net.draycia.carbon.common.channels.messages.ConfigChannelMessageSource;
import net.draycia.carbon.common.messages.CarbonMessages;
import net.draycia.carbon.paper.integration.IntegrationChannel;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.key.Key;
import org.bukkit.Bukkit;
Expand All @@ -46,7 +46,7 @@

@DefaultQualifier(NonNull.class)
@ConfigSerializable
public class McmmoPartyChannel extends ConfigChatChannel {
public class McmmoPartyChannel extends IntegrationChannel {

public static final String FILE_NAME = "mcmmo-party.conf";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import net.draycia.carbon.api.channels.ChannelPermissions;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.api.users.UserManager;
import net.draycia.carbon.common.channels.ConfigChatChannel;
import net.draycia.carbon.paper.integration.IntegrationChannel;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import org.bukkit.entity.Player;
Expand All @@ -40,7 +40,7 @@
import static net.draycia.carbon.api.channels.ChannelPermissionResult.channelPermissionResult;

@DefaultQualifier(NonNull.class)
abstract class ResidentListChannel<T extends ResidentList> extends ConfigChatChannel {
abstract class ResidentListChannel<T extends ResidentList> extends IntegrationChannel {

protected static final String TOWNY_CHANNEL_HEADER = """
See the Towny Wiki at https://github.com/TownyAdvanced/Towny/wiki/Placeholders
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import net.draycia.carbon.api.CarbonChat;
import net.draycia.carbon.api.channels.ChatChannel;
import net.draycia.carbon.api.users.CarbonPlayer;
import net.draycia.carbon.paper.integration.IntegrationChannel;
import net.essentialsx.api.v2.events.discord.DiscordMessageEvent;
import net.essentialsx.api.v2.events.discord.DiscordRelayEvent;
import net.essentialsx.api.v2.services.discord.DiscordService;
Expand Down Expand Up @@ -63,14 +64,22 @@ public void onDiscordMessage(final DiscordMessageEvent event) {

final CarbonPlayer result = this.carbonChat.userManager().user(event.getUUID()).join();

// TODO: Trigger this logic ourselves, so we don't have to assume which channel the player is currently using?
final ChatChannel channel = result.selectedChannel();

if (channel == null) {
event.setCancelled(true);
return;
}

final MessageType messageType = this.channelMessageTypes.get(channel.key());

// Don't send messages from integration channels
if (messageType == null) {
event.setCancelled(true);
return;
}

event.setType(messageType);
}

Expand All @@ -83,9 +92,19 @@ public void init() {

if (discord != null) {
this.carbonChat.channelRegistry().allKeys(key -> {
final ChatChannel channel = this.carbonChat.channelRegistry().channel(key);

if (channel == null) {
return;
}

if (channel instanceof IntegrationChannel) {
return;
}

final MessageType channelMessageType = new MessageType(key.value());
discord.registerMessageType(this.plugin, channelMessageType);
this.channelMessageTypes.put(key, channelMessageType);
discord.registerMessageType(this.plugin, channelMessageType);
});
}
}
Expand Down

0 comments on commit cba044d

Please sign in to comment.