mirror of
https://github.com/DiscordSRV/Ascension.git
synced 2024-11-01 08:39:31 +01:00
Add default value for user not having any roles
This commit is contained in:
parent
f35d8d67ee
commit
e7d93a4176
@ -62,6 +62,7 @@ public interface DiscordSRVApi {
|
|||||||
* DiscordSRV's own placeholder service.
|
* DiscordSRV's own placeholder service.
|
||||||
* @return the {@link PlaceholderService} instance.
|
* @return the {@link PlaceholderService} instance.
|
||||||
*/
|
*/
|
||||||
|
@NotNull
|
||||||
PlaceholderService placeholderService();
|
PlaceholderService placeholderService();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -29,7 +29,7 @@ import java.util.function.Supplier;
|
|||||||
|
|
||||||
public final class ResultMappers {
|
public final class ResultMappers {
|
||||||
|
|
||||||
private static final ThreadLocal<Boolean> PLAIN_COMPONENTS = new ThreadLocal<>();
|
private static final ThreadLocal<Boolean> PLAIN_COMPONENTS = ThreadLocal.withInitial(() -> false);
|
||||||
|
|
||||||
private ResultMappers() {}
|
private ResultMappers() {}
|
||||||
|
|
||||||
|
@ -47,7 +47,7 @@ allprojects {
|
|||||||
}
|
}
|
||||||
|
|
||||||
generateRuntimeDownloadResourceForRuntimeDownloadOnly {
|
generateRuntimeDownloadResourceForRuntimeDownloadOnly {
|
||||||
file = 'runtimeDownload-' + project.name + '.txt'
|
file = 'dependencies/runtimeDownload-' + project.name + '.txt'
|
||||||
}
|
}
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
|
@ -9,12 +9,12 @@ configurations {
|
|||||||
task generateResourceForH2Driver(type: GenerateDependencyDownloadResourceTask) {
|
task generateResourceForH2Driver(type: GenerateDependencyDownloadResourceTask) {
|
||||||
var conf = configurations.h2Driver
|
var conf = configurations.h2Driver
|
||||||
configuration = conf
|
configuration = conf
|
||||||
file = conf.name + '.txt'
|
file = 'dependencies/' + conf.name + '.txt'
|
||||||
}
|
}
|
||||||
task generateResourceForMySQLDriver(type: GenerateDependencyDownloadResourceTask) {
|
task generateResourceForMySQLDriver(type: GenerateDependencyDownloadResourceTask) {
|
||||||
var conf = configurations.mysqlDriver
|
var conf = configurations.mysqlDriver
|
||||||
configuration = conf
|
configuration = conf
|
||||||
file = conf.name + '.txt'
|
file = 'dependencies/' + conf.name + '.txt'
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
@ -22,7 +22,6 @@ import com.discordsrv.api.discord.connection.DiscordConnectionDetails;
|
|||||||
import com.discordsrv.api.event.bus.EventBus;
|
import com.discordsrv.api.event.bus.EventBus;
|
||||||
import com.discordsrv.api.event.events.lifecycle.DiscordSRVReloadEvent;
|
import com.discordsrv.api.event.events.lifecycle.DiscordSRVReloadEvent;
|
||||||
import com.discordsrv.api.event.events.lifecycle.DiscordSRVShuttingDownEvent;
|
import com.discordsrv.api.event.events.lifecycle.DiscordSRVShuttingDownEvent;
|
||||||
import com.discordsrv.api.placeholder.PlaceholderService;
|
|
||||||
import com.discordsrv.common.api.util.ApiInstanceUtil;
|
import com.discordsrv.common.api.util.ApiInstanceUtil;
|
||||||
import com.discordsrv.common.channel.ChannelConfigHelper;
|
import com.discordsrv.common.channel.ChannelConfigHelper;
|
||||||
import com.discordsrv.common.channel.DefaultGlobalChannel;
|
import com.discordsrv.common.channel.DefaultGlobalChannel;
|
||||||
@ -43,8 +42,9 @@ import com.discordsrv.common.listener.DiscordChatListener;
|
|||||||
import com.discordsrv.common.listener.GameChatListener;
|
import com.discordsrv.common.listener.GameChatListener;
|
||||||
import com.discordsrv.common.logging.DependencyLoggingFilter;
|
import com.discordsrv.common.logging.DependencyLoggingFilter;
|
||||||
import com.discordsrv.common.logging.logger.backend.LoggingBackend;
|
import com.discordsrv.common.logging.logger.backend.LoggingBackend;
|
||||||
import com.discordsrv.common.placeholder.PlaceholderServiceImpl;
|
|
||||||
import com.discordsrv.common.placeholder.ComponentResultStringifier;
|
import com.discordsrv.common.placeholder.ComponentResultStringifier;
|
||||||
|
import com.discordsrv.common.placeholder.PlaceholderServiceImpl;
|
||||||
|
import com.discordsrv.common.placeholder.context.GlobalTextHandlingContext;
|
||||||
import net.dv8tion.jda.api.JDA;
|
import net.dv8tion.jda.api.JDA;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
@ -67,7 +67,7 @@ public abstract class AbstractDiscordSRV<C extends MainConfig, CC extends Connec
|
|||||||
|
|
||||||
// DiscordSRVApi
|
// DiscordSRVApi
|
||||||
private EventBus eventBus;
|
private EventBus eventBus;
|
||||||
private PlaceholderService placeholderService;
|
private PlaceholderServiceImpl placeholderService;
|
||||||
private ComponentFactory componentFactory;
|
private ComponentFactory componentFactory;
|
||||||
private DiscordAPIImpl discordAPI;
|
private DiscordAPIImpl discordAPI;
|
||||||
private DiscordConnectionDetails discordConnectionDetails;
|
private DiscordConnectionDetails discordConnectionDetails;
|
||||||
@ -106,7 +106,7 @@ public abstract class AbstractDiscordSRV<C extends MainConfig, CC extends Connec
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull PlaceholderService placeholderService() {
|
public @NotNull PlaceholderServiceImpl placeholderService() {
|
||||||
return placeholderService;
|
return placeholderService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -245,8 +245,9 @@ public abstract class AbstractDiscordSRV<C extends MainConfig, CC extends Connec
|
|||||||
discordConnectionManager = new JDAConnectionManager(this);
|
discordConnectionManager = new JDAConnectionManager(this);
|
||||||
discordConnectionManager.connect().join();
|
discordConnectionManager.connect().join();
|
||||||
|
|
||||||
// Placeholder result stringifiers
|
// Placeholder result stringifiers & global contexts
|
||||||
placeholderService().addResultMapper(new ComponentResultStringifier(this));
|
placeholderService().addResultMapper(new ComponentResultStringifier(this));
|
||||||
|
placeholderService().addGlobalContext(new GlobalTextHandlingContext(this));
|
||||||
|
|
||||||
// Register PlayerProvider listeners
|
// Register PlayerProvider listeners
|
||||||
playerProvider().subscribe();
|
playerProvider().subscribe();
|
||||||
|
@ -30,6 +30,7 @@ import com.discordsrv.common.console.Console;
|
|||||||
import com.discordsrv.common.discord.api.DiscordAPIImpl;
|
import com.discordsrv.common.discord.api.DiscordAPIImpl;
|
||||||
import com.discordsrv.common.discord.connection.DiscordConnectionManager;
|
import com.discordsrv.common.discord.connection.DiscordConnectionManager;
|
||||||
import com.discordsrv.common.logging.logger.Logger;
|
import com.discordsrv.common.logging.logger.Logger;
|
||||||
|
import com.discordsrv.common.placeholder.PlaceholderServiceImpl;
|
||||||
import com.discordsrv.common.player.provider.AbstractPlayerProvider;
|
import com.discordsrv.common.player.provider.AbstractPlayerProvider;
|
||||||
import com.discordsrv.common.scheduler.Scheduler;
|
import com.discordsrv.common.scheduler.Scheduler;
|
||||||
import com.github.benmanes.caffeine.cache.Caffeine;
|
import com.github.benmanes.caffeine.cache.Caffeine;
|
||||||
@ -54,6 +55,10 @@ public interface DiscordSRV extends DiscordSRVApi {
|
|||||||
@NotNull
|
@NotNull
|
||||||
ComponentFactory componentFactory();
|
ComponentFactory componentFactory();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@NotNull
|
||||||
|
PlaceholderServiceImpl placeholderService();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@NotNull
|
@NotNull
|
||||||
AbstractPlayerProvider<?> playerProvider();
|
AbstractPlayerProvider<?> playerProvider();
|
||||||
|
@ -56,21 +56,29 @@ public class EnhancedTextBuilderImpl implements EnhancedTextBuilder {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull EnhancedTextBuilder addReplacement(Pattern target, Function<Matcher, Object> replacement) {
|
public @NotNull EnhancedTextBuilder addReplacement(Pattern target, Function<Matcher, Object> replacement) {
|
||||||
this.replacements.put(target, replacement);
|
this.replacements.put(target, wrapFunction(replacement));
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull EnhancedTextBuilder applyPlaceholderService() {
|
public @NotNull EnhancedTextBuilder applyPlaceholderService() {
|
||||||
this.replacements.put(PlaceholderService.PATTERN, matcher -> {
|
this.replacements.put(PlaceholderService.PATTERN, wrapFunction(
|
||||||
Object result = discordSRV.placeholderService().getResult(matcher, context);
|
matcher -> discordSRV.placeholderService().getResult(matcher, context)));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Function<Matcher, Object> wrapFunction(Function<Matcher, Object> function) {
|
||||||
|
return matcher -> {
|
||||||
|
Object result = function.apply(matcher);
|
||||||
if (result instanceof Color) {
|
if (result instanceof Color) {
|
||||||
// Convert Color to something it'll understand
|
// Convert Color to something it'll understand
|
||||||
return TextColor.color(((Color) result).rgb());
|
return TextColor.color(((Color) result).rgb());
|
||||||
|
} else if (result instanceof MinecraftComponent) {
|
||||||
|
// Convert to adventure component from API
|
||||||
|
return ComponentUtil.fromAPI((MinecraftComponent) result);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
});
|
};
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -29,20 +29,29 @@ import net.kyori.adventure.text.Component;
|
|||||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
public class DiscordSRVMinecraftRenderer extends DefaultMinecraftRenderer {
|
public class DiscordSRVMinecraftRenderer extends DefaultMinecraftRenderer {
|
||||||
|
|
||||||
private static final ThreadLocal<Long> GUILD_CONTEXT = new ThreadLocal<>();
|
private static final ThreadLocal<Long> GUILD_CONTEXT = ThreadLocal.withInitial(() -> 0L);
|
||||||
private final DiscordSRV discordSRV;
|
private final DiscordSRV discordSRV;
|
||||||
|
|
||||||
public DiscordSRVMinecraftRenderer(DiscordSRV discordSRV) {
|
public DiscordSRVMinecraftRenderer(DiscordSRV discordSRV) {
|
||||||
this.discordSRV = discordSRV;
|
this.discordSRV = discordSRV;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void inGuildContext(long guildId, Runnable runnable) {
|
public static void runInGuildContext(long guildId, Runnable runnable) {
|
||||||
GUILD_CONTEXT.set(guildId);
|
getWithGuildContext(guildId, () -> {
|
||||||
runnable.run();
|
runnable.run();
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T> T getWithGuildContext(long guildId, Supplier<T> supplier) {
|
||||||
|
GUILD_CONTEXT.set(guildId);
|
||||||
|
T output = supplier.get();
|
||||||
GUILD_CONTEXT.set(0L);
|
GUILD_CONTEXT.set(0L);
|
||||||
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -18,8 +18,6 @@
|
|||||||
|
|
||||||
package com.discordsrv.common.config.main.channels;
|
package com.discordsrv.common.config.main.channels;
|
||||||
|
|
||||||
import com.discordsrv.common.config.main.channels.discordtominecraft.DiscordToMinecraftChatConfig;
|
|
||||||
import com.discordsrv.common.config.main.channels.minecraftodiscord.MinecraftToDiscordChatConfig;
|
|
||||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||||
import org.spongepowered.configurate.ConfigurationNode;
|
import org.spongepowered.configurate.ConfigurationNode;
|
||||||
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
|
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.discordsrv.common.config.main.channels.discordtominecraft;
|
package com.discordsrv.common.config.main.channels;
|
||||||
|
|
||||||
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
|
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
|
||||||
import org.spongepowered.configurate.objectmapping.meta.Comment;
|
import org.spongepowered.configurate.objectmapping.meta.Comment;
|
||||||
@ -31,10 +31,10 @@ import java.util.regex.Pattern;
|
|||||||
public class DiscordToMinecraftChatConfig {
|
public class DiscordToMinecraftChatConfig {
|
||||||
|
|
||||||
@Comment("The Discord to Minecraft message format for regular users")
|
@Comment("The Discord to Minecraft message format for regular users")
|
||||||
public String format = "[ᛩF2Discord&r] [hover:show_text:Tag: %user_tag%&r\\nRoles: %user_roles_, %]%user_color%%user_effective_name%&r » %message%";
|
public String format = "[ᛩF2Discord&r] [hover:show_text:Tag: %user_tag%&r\\nRoles: %user_roles_, |text_&7&oNone%%]%user_color%%user_effective_name%&r » %message%";
|
||||||
|
|
||||||
@Comment("The Discord to Minecraft message format for webhook messages (if enabled)")
|
@Comment("The Discord to Minecraft message format for webhook messages (if enabled)")
|
||||||
public String webhookFormat = "[ᛩF2Discord&r] [hover:show_text:Webhook message]%user_name% » %message%";
|
public String webhookFormat = "[ᛩF2Discord&r] [hover:show_text:Webhook message]%user_name%&r » %message%";
|
||||||
|
|
||||||
@Comment("Users, bots and webhooks to ignore")
|
@Comment("Users, bots and webhooks to ignore")
|
||||||
public Ignores ignores = new Ignores();
|
public Ignores ignores = new Ignores();
|
@ -16,7 +16,7 @@
|
|||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.discordsrv.common.config.main.channels.minecraftodiscord;
|
package com.discordsrv.common.config.main.channels;
|
||||||
|
|
||||||
import com.discordsrv.api.discord.api.entity.message.SendableDiscordMessage;
|
import com.discordsrv.api.discord.api.entity.message.SendableDiscordMessage;
|
||||||
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
|
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
|
@ -33,7 +33,7 @@ import com.discordsrv.common.DiscordSRV;
|
|||||||
import com.discordsrv.common.component.renderer.DiscordSRVMinecraftRenderer;
|
import com.discordsrv.common.component.renderer.DiscordSRVMinecraftRenderer;
|
||||||
import com.discordsrv.common.component.util.ComponentUtil;
|
import com.discordsrv.common.component.util.ComponentUtil;
|
||||||
import com.discordsrv.common.config.main.channels.BaseChannelConfig;
|
import com.discordsrv.common.config.main.channels.BaseChannelConfig;
|
||||||
import com.discordsrv.common.config.main.channels.discordtominecraft.DiscordToMinecraftChatConfig;
|
import com.discordsrv.common.config.main.channels.DiscordToMinecraftChatConfig;
|
||||||
import com.discordsrv.common.function.OrDefault;
|
import com.discordsrv.common.function.OrDefault;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
import org.apache.commons.lang3.tuple.Pair;
|
import org.apache.commons.lang3.tuple.Pair;
|
||||||
@ -105,19 +105,19 @@ public class DiscordChatListener extends AbstractListener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
String format = chatConfig.opt(cfg -> webhookMessage ? cfg.format : cfg.webhookFormat)
|
String format = chatConfig.opt(cfg -> webhookMessage ? cfg.webhookFormat : cfg.format)
|
||||||
.map(option -> option.replace("\\n", "\n"))
|
.map(option -> option.replace("\\n", "\n"))
|
||||||
.orElse(null);
|
.orElse(null);
|
||||||
if (format == null) {
|
if (format == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
DiscordSRVMinecraftRenderer.inGuildContext(channel.getGuild().getId(), () -> {
|
|
||||||
Placeholders message = new Placeholders(event.getMessageContent());
|
Placeholders message = new Placeholders(event.getMessageContent());
|
||||||
chatConfig.opt(cfg -> cfg.contentRegexFilters)
|
chatConfig.opt(cfg -> cfg.contentRegexFilters)
|
||||||
.ifPresent(filters -> filters.forEach(message::replaceAll));
|
.ifPresent(filters -> filters.forEach(message::replaceAll));
|
||||||
|
|
||||||
Component messageComponent = discordSRV.componentFactory().minecraftSerializer().serialize(message.toString());
|
Component messageComponent = DiscordSRVMinecraftRenderer.getWithGuildContext(channel.getGuild().getId(), () ->
|
||||||
|
discordSRV.componentFactory().minecraftSerializer().serialize(message.toString()));
|
||||||
|
|
||||||
EnhancedTextBuilder componentBuilder = discordSRV.componentFactory()
|
EnhancedTextBuilder componentBuilder = discordSRV.componentFactory()
|
||||||
.enhancedBuilder(format)
|
.enhancedBuilder(format)
|
||||||
@ -134,6 +134,5 @@ public class DiscordChatListener extends AbstractListener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
gameChannel.sendMessage(component);
|
gameChannel.sendMessage(component);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,7 @@ import com.discordsrv.common.DiscordSRV;
|
|||||||
import com.discordsrv.common.component.util.ComponentUtil;
|
import com.discordsrv.common.component.util.ComponentUtil;
|
||||||
import com.discordsrv.common.config.main.channels.BaseChannelConfig;
|
import com.discordsrv.common.config.main.channels.BaseChannelConfig;
|
||||||
import com.discordsrv.common.config.main.channels.ChannelConfig;
|
import com.discordsrv.common.config.main.channels.ChannelConfig;
|
||||||
import com.discordsrv.common.config.main.channels.minecraftodiscord.MinecraftToDiscordChatConfig;
|
import com.discordsrv.common.config.main.channels.MinecraftToDiscordChatConfig;
|
||||||
import com.discordsrv.common.discord.api.message.ReceivedDiscordMessageClusterImpl;
|
import com.discordsrv.common.discord.api.message.ReceivedDiscordMessageClusterImpl;
|
||||||
import com.discordsrv.common.function.OrDefault;
|
import com.discordsrv.common.function.OrDefault;
|
||||||
import net.kyori.adventure.text.Component;
|
import net.kyori.adventure.text.Component;
|
||||||
|
@ -0,0 +1,38 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of DiscordSRV, licensed under the GPLv3 License
|
||||||
|
* Copyright (c) 2016-2021 Austin "Scarsz" Shapiro, Henri "Vankka" Schubin and DiscordSRV 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 com.discordsrv.common.placeholder.context;
|
||||||
|
|
||||||
|
import com.discordsrv.api.component.MinecraftComponent;
|
||||||
|
import com.discordsrv.api.placeholder.annotation.Placeholder;
|
||||||
|
import com.discordsrv.api.placeholder.annotation.PlaceholderRemainder;
|
||||||
|
import com.discordsrv.common.DiscordSRV;
|
||||||
|
|
||||||
|
public class GlobalTextHandlingContext {
|
||||||
|
|
||||||
|
private final DiscordSRV discordSRV;
|
||||||
|
|
||||||
|
public GlobalTextHandlingContext(DiscordSRV discordSRV) {
|
||||||
|
this.discordSRV = discordSRV;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Placeholder("text_")
|
||||||
|
public MinecraftComponent text(@PlaceholderRemainder String text) {
|
||||||
|
return discordSRV.componentFactory().enhancedBuilder(text).build();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user