Skip to content
This repository has been archived by the owner on Oct 12, 2024. It is now read-only.

Add Spicord Hook, two-way Discord support #139

Merged
merged 11 commits into from
Sep 18, 2023
1 change: 1 addition & 0 deletions bukkit/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ shadowJar {
relocate 'net.william278.profanitycheckerapi', 'net.william278.huskchat.libraries.profanitycheckerapi'
relocate 'net.william278.desertwell', 'net.william278.huskchat.libraries.desertwell'
relocate 'de.themoep', 'net.william278.huskchat.libraries'
relocate 'dev.vankka', 'net.william278.huskchat.libraries'

relocate 'org.apache', 'net.william278.huskchat.libraries'
relocate 'org.jetbrains', 'net.william278.huskchat.libraries'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import net.william278.huskchat.command.ShortcutCommand;
import net.william278.huskchat.config.Locales;
import net.william278.huskchat.config.Settings;
import net.william278.huskchat.config.Webhook;
import net.william278.huskchat.discord.DiscordHook;
import net.william278.huskchat.event.EventDispatcher;
import net.william278.huskchat.getter.DataGetter;
import net.william278.huskchat.getter.DefaultDataGetter;
Expand Down Expand Up @@ -62,7 +62,7 @@ public static BukkitHuskChat getInstance() {
private Settings settings;
private List<BukkitCommand> commands;
private BukkitEventDispatcher eventDispatcher;
private Webhook webhook;
private DiscordHook discordHook;
private Locales locales;
private DataGetter playerDataGetter;
private PlayerCache playerCache;
Expand Down Expand Up @@ -116,11 +116,6 @@ public void onEnable() {
)))
.toList());

// Initialize webhook dispatcher
if (getSettings().doDiscordIntegration()) {
this.webhook = new Webhook(this);
}

// Initialise metrics and log
this.checkForUpdates();
log(Level.INFO, "Enabled HuskChat version " + this.getVersion());
Expand Down Expand Up @@ -173,8 +168,13 @@ public DataGetter getDataGetter() {
}

@Override
public Optional<Webhook> getWebhook() {
return Optional.ofNullable(webhook);
public Optional<DiscordHook> getDiscordHook() {
return Optional.ofNullable(discordHook);
}

@Override
public void setDiscordHook(@NotNull DiscordHook discordHook) {
this.discordHook = discordHook;
}

@NotNull
Expand Down Expand Up @@ -232,8 +232,12 @@ public boolean isPluginPresent(@NotNull String dependency) {
}

@Override
public void log(@NotNull Level level, @NotNull String message, @NotNull Throwable... throwable) {
getLogger().log(level, message, throwable);
public void log(@NotNull Level level, @NotNull String message, @NotNull Throwable... exceptions) {
if (exceptions.length > 0) {
getLogger().log(level, message, exceptions[0]);
return;
}
getLogger().log(level, message);
}

@NotNull
Expand Down
3 changes: 2 additions & 1 deletion bukkit/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ api-version: 1.16
folia-supported: true
softdepend:
- 'LuckPerms'
- 'PlaceholderAPI'
- 'PlaceholderAPI'
- 'Spicord'
1 change: 1 addition & 0 deletions bungee/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ shadowJar {
relocate 'net.william278.profanitycheckerapi', 'net.william278.huskchat.libraries.profanitycheckerapi'
relocate 'net.william278.desertwell', 'net.william278.huskchat.libraries.desertwell'
relocate 'de.themoep', 'net.william278.huskchat.libraries'
relocate 'dev.vankka', 'net.william278.huskchat.libraries'

relocate 'org.apache', 'net.william278.huskchat.libraries'
relocate 'org.jetbrains', 'net.william278.huskchat.libraries'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import net.william278.huskchat.command.ShortcutCommand;
import net.william278.huskchat.config.Locales;
import net.william278.huskchat.config.Settings;
import net.william278.huskchat.config.Webhook;
import net.william278.huskchat.discord.DiscordHook;
import net.william278.huskchat.getter.DataGetter;
import net.william278.huskchat.getter.DefaultDataGetter;
import net.william278.huskchat.getter.LuckPermsDataGetter;
Expand Down Expand Up @@ -66,7 +66,7 @@ public static BungeeHuskChat getInstance() {
private Settings settings;
private List<BungeeCommand> commands;
private BungeeEventDispatcher eventDispatcher;
private Webhook webhook;
private DiscordHook discordHook;
private Locales locales;
private DataGetter playerDataGetter;
private PlayerCache playerCache;
Expand Down Expand Up @@ -119,11 +119,6 @@ public void onEnable() {
)))
.toList());

// Initialize webhook dispatcher
if (getSettings().doDiscordIntegration()) {
this.webhook = new Webhook(this);
}

// Initialise metrics and log
new Metrics(this, METRICS_ID);
this.checkForUpdates();
Expand Down Expand Up @@ -183,8 +178,13 @@ public DataGetter getDataGetter() {
}

@Override
public Optional<Webhook> getWebhook() {
return Optional.ofNullable(webhook);
public Optional<DiscordHook> getDiscordHook() {
return Optional.ofNullable(discordHook);
}

@Override
public void setDiscordHook(@NotNull DiscordHook discordHook) {
this.discordHook = discordHook;
}

@NotNull
Expand Down Expand Up @@ -245,8 +245,12 @@ public boolean isPluginPresent(@NotNull String dependency) {
}

@Override
public void log(@NotNull Level level, @NotNull String message, @NotNull Throwable... throwable) {
getLogger().log(level, message, throwable);
public void log(@NotNull Level level, @NotNull String message, @NotNull Throwable... exceptions) {
if (exceptions.length > 0) {
getLogger().log(level, message, exceptions[0]);
return;
}
getLogger().log(level, message);
}

@NotNull
Expand Down
3 changes: 2 additions & 1 deletion bungee/src/main/resources/bungee.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ author: 'William278'
main: 'net.william278.huskchat.bungeecord.BungeeHuskChat'
softDepends:
- 'LuckPerms'
- 'PAPIProxyBridge'
- 'PAPIProxyBridge'
- 'Spicord'
5 changes: 4 additions & 1 deletion common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ dependencies {
api 'de.themoep:minedown-adventure:1.7.2-SNAPSHOT'
api 'net.william278:ProfanityCheckerAPI:2.0.1'
api 'net.william278:desertwell:2.0.4'
api 'dev.vankka:mcdiscordreserializer:4.3.0'

compileOnly 'org.jetbrains:annotations:24.0.1'
compileOnly 'net.william278:papiproxybridge:1.2.2'
compileOnly 'net.kyori:adventure-api:4.14.0'
compileOnly 'net.luckperms:api:5.4'
compileOnly 'org.jetbrains:annotations:24.0.1'
compileOnly 'com.github.Spicord.Spicord:spicord-common:5.1.0'
compileOnly 'net.dv8tion:JDA:5.0.0-beta.4'

testImplementation 'net.william278:ProfanityCheckerAPI:2.0.1'
testImplementation 'net.kyori:adventure-api:4.14.0'
Expand Down
17 changes: 14 additions & 3 deletions common/src/main/java/net/william278/huskchat/HuskChat.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
import net.william278.desertwell.util.Version;
import net.william278.huskchat.config.Locales;
import net.william278.huskchat.config.Settings;
import net.william278.huskchat.config.Webhook;
import net.william278.huskchat.discord.DiscordHook;
import net.william278.huskchat.discord.SpicordHook;
import net.william278.huskchat.discord.WebHook;
import net.william278.huskchat.event.EventDispatcher;
import net.william278.huskchat.getter.DataGetter;
import net.william278.huskchat.placeholders.PlaceholderReplacer;
Expand Down Expand Up @@ -60,6 +62,7 @@ public interface HuskChat {

default void loadConfig() {
try {
// Set settings
this.setSettings(new Settings(YamlDocument.create(
new File(getDataFolder(), "config.yml"),
Objects.requireNonNull(getResource("config.yml")),
Expand All @@ -69,6 +72,12 @@ default void loadConfig() {
UpdaterSettings.builder().setVersioning(new BasicVersioning("config-version")).build()
)));
this.setLocales(new Locales(this));

// Initialize webhook dispatcher
if (getSettings().doDiscordIntegration()) {
setDiscordHook(getSettings().useSpicord() && isPluginPresent("Spicord")
? new SpicordHook(this) : new WebHook(this));
}
} catch (Throwable e) {
log(Level.SEVERE, "Failed to load plugin config/locale files", e);
}
Expand All @@ -94,7 +103,9 @@ default CompletableFuture<String> replacePlaceholders(@NotNull Player player, @N
@NotNull
DataGetter getDataGetter();

Optional<Webhook> getWebhook();
Optional<DiscordHook> getDiscordHook();

void setDiscordHook(@NotNull DiscordHook discordHook);

@NotNull
Version getVersion();
Expand Down Expand Up @@ -136,7 +147,7 @@ default void checkForUpdates() {
getUpdateChecker().check().thenAccept(checked -> {
if (!checked.isUpToDate()) {
log(Level.WARNING, "A new version of HuskChat is available: v"
+ checked.getLatestVersion() + " (running v" + getVersion() + ")");
+ checked.getLatestVersion() + " (running v" + getVersion() + ")");
}
});
}
Expand Down
51 changes: 46 additions & 5 deletions common/src/main/java/net/william278/huskchat/config/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import dev.dejvokep.boostedyaml.YamlDocument;
import dev.dejvokep.boostedyaml.block.implementation.Section;
import net.william278.huskchat.channel.Channel;
import net.william278.huskchat.discord.WebHook;
import net.william278.huskchat.filter.*;
import net.william278.huskchat.replacer.EmojiReplacer;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -97,7 +98,11 @@ public class Settings {
// Discord integration
private boolean doDiscordIntegration;
private Map<String, URL> webhookUrls;
private Webhook.Format webhookFormat;
private WebHook.Format webhookFormat;
private boolean useSpicord;
private String discordUsernameFormat;
private Map<String, String> spicordReceiveChannelMap;
private Map<String, String> spicordSendChannelMap;

// Server names
private Map<String, String> serverNameReplacement;
Expand Down Expand Up @@ -179,10 +184,24 @@ private void loadConfig(@NotNull YamlDocument configFile) {
chatFilters = fetchChatFilters(configFile);

// Discord integration
doDiscordIntegration = configFile.getBoolean("discord.enabled", false);
webhookFormat = Webhook.Format.getMessageFormat(configFile.getString("discord.format_style", "inline"))
.orElse(Webhook.Format.INLINE);
webhookFormat = WebHook.Format.getMessageFormat(configFile.getString("discord.format_style", "inline"))
.orElse(WebHook.Format.INLINE);
webhookUrls = fetchWebhookUrls(configFile);
doDiscordIntegration = configFile.getBoolean("discord.enabled", false);
useSpicord = configFile.getBoolean("discord.spicord.enabled", true);
discordUsernameFormat = configFile.getString("discord.username_format", "@%discord_handle%");
spicordReceiveChannelMap = new LinkedHashMap<>();
if (configFile.contains("discord.spicord.receive_channel_map")) {
for (String channelID : configFile.getSection("discord.spicord.receive_channel_map").getRoutesAsStrings(false)) {
spicordReceiveChannelMap.put(channelID, configFile.getString("discord.spicord.receive_channel_map." + channelID));
}
}
spicordSendChannelMap = new LinkedHashMap<>();
if (configFile.contains("discord.spicord.send_channel_map")) {
for (String channelID : configFile.getSection("discord.spicord.send_channel_map").getRoutesAsStrings(false)) {
spicordSendChannelMap.put(channelID, configFile.getString("discord.spicord.send_channel_map." + channelID));
}
}

// Server name replacement
serverNameReplacement = new LinkedHashMap<>();
Expand Down Expand Up @@ -640,14 +659,36 @@ public boolean doDiscordIntegration() {
return doDiscordIntegration;
}

@NotNull
public Map<String, URL> getWebhookUrls() {
return webhookUrls;
}

public Webhook.Format getWebhookMessageFormat() {
@NotNull
public WebHook.Format getDiscordMessageFormat() {
return webhookFormat;
}

public boolean useSpicord() {
return useSpicord;
}

@NotNull
public String getDiscordUsernameFormat() {
return discordUsernameFormat;
}

@NotNull
public Map<String, String> getSpicordReceiveChannelMap() {
return spicordReceiveChannelMap;
}

@NotNull
public Map<String, String> getSpicordSendChannelMap() {
return spicordSendChannelMap;
}

@NotNull
public Map<String, String> getServerNameReplacement() {
return serverNameReplacement;
}
Expand Down
Loading