Updates to configs

This commit is contained in:
Vankka 2023-06-10 16:36:54 +03:00
parent 05949fe6b0
commit cd9d8047c0
No known key found for this signature in database
GPG Key ID: 6E50CB7A29B96AD0
18 changed files with 185 additions and 84 deletions

View File

@ -27,7 +27,7 @@ import com.discordsrv.common.api.util.ApiInstanceUtil;
import com.discordsrv.common.bootstrap.IBootstrap;
import com.discordsrv.common.channel.ChannelConfigHelper;
import com.discordsrv.common.channel.ChannelLockingModule;
import com.discordsrv.common.channel.ChannelUpdaterModule;
import com.discordsrv.common.channel.TimedUpdaterModule;
import com.discordsrv.common.channel.GlobalChannelLookupModule;
import com.discordsrv.common.command.discord.DiscordCommandModule;
import com.discordsrv.common.command.game.GameCommandModule;
@ -562,7 +562,7 @@ public abstract class AbstractDiscordSRV<B extends IBootstrap, C extends MainCon
// Modules
registerModule(ChannelLockingModule::new);
registerModule(ChannelUpdaterModule::new);
registerModule(TimedUpdaterModule::new);
registerModule(DiscordCommandModule::new);
registerModule(GameCommandModule::new);
registerModule(GlobalChannelLookupModule::new);
@ -658,7 +658,7 @@ public abstract class AbstractDiscordSRV<B extends IBootstrap, C extends MainCon
String provider = linkedAccountConfig.provider;
boolean permitMinecraftAuth = connectionConfig().minecraftAuth.allow;
if (provider.equals("auto")) {
provider = permitMinecraftAuth ? "minecraftauth" : "storage";
provider = permitMinecraftAuth && onlineMode().isOnline() ? "minecraftauth" : "storage";
}
switch (provider) {
case "minecraftauth":

View File

@ -20,7 +20,7 @@ package com.discordsrv.common.channel;
import com.discordsrv.api.discord.connection.jda.errorresponse.ErrorCallbackContext;
import com.discordsrv.common.DiscordSRV;
import com.discordsrv.common.config.main.ChannelUpdaterConfig;
import com.discordsrv.common.config.main.TimedUpdaterConfig;
import com.discordsrv.common.logging.NamedLogger;
import com.discordsrv.common.module.type.AbstractModule;
import net.dv8tion.jda.api.JDA;
@ -31,24 +31,27 @@ import net.dv8tion.jda.api.managers.channel.concrete.TextChannelManager;
import org.apache.commons.lang3.StringUtils;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
public class ChannelUpdaterModule extends AbstractModule<DiscordSRV> {
public class TimedUpdaterModule extends AbstractModule<DiscordSRV> {
private final Set<ScheduledFuture<?>> futures = new LinkedHashSet<>();
private boolean firstReload = true;
public ChannelUpdaterModule(DiscordSRV discordSRV) {
public TimedUpdaterModule(DiscordSRV discordSRV) {
super(discordSRV, new NamedLogger(discordSRV, "CHANNEL_UPDATER"));
}
@Override
public boolean isEnabled() {
boolean any = false;
for (ChannelUpdaterConfig channelUpdater : discordSRV.config().channelUpdaters) {
if (!channelUpdater.channelIds.isEmpty()) {
TimedUpdaterConfig config = discordSRV.config().timedUpdater;
for (TimedUpdaterConfig.UpdaterConfig updaterConfig : config.getConfigs()) {
if (updaterConfig.any()) {
any = true;
break;
}
@ -65,18 +68,21 @@ public class ChannelUpdaterModule extends AbstractModule<DiscordSRV> {
futures.forEach(future -> future.cancel(false));
futures.clear();
for (ChannelUpdaterConfig config : discordSRV.config().channelUpdaters) {
TimedUpdaterConfig config = discordSRV.config().timedUpdater;
for (TimedUpdaterConfig.UpdaterConfig updaterConfig : config.getConfigs()) {
long time = Math.max(updaterConfig.timeSeconds(), updaterConfig.minimumSeconds());
futures.add(discordSRV.scheduler().runAtFixedRate(
() -> update(config),
firstReload ? 0 : config.timeMinutes,
config.timeMinutes,
TimeUnit.MINUTES
() -> update(updaterConfig),
firstReload ? 0 : time,
time,
TimeUnit.SECONDS
));
}
firstReload = false;
}
public void update(ChannelUpdaterConfig config) {
public void update(TimedUpdaterConfig.UpdaterConfig config) {
try {
// Wait a moment in case we're (re)connecting at the time
discordSRV.waitForStatus(DiscordSRV.Status.CONNECTED, 15, TimeUnit.SECONDS);
@ -89,9 +95,26 @@ public class ChannelUpdaterModule extends AbstractModule<DiscordSRV> {
return;
}
String topicFormat = config.topicFormat;
String nameFormat = config.nameFormat;
if (config instanceof TimedUpdaterConfig.VoiceChannelConfig) {
updateChannel(
jda,
((TimedUpdaterConfig.VoiceChannelConfig) config).channelIds,
((TimedUpdaterConfig.VoiceChannelConfig) config).nameFormat,
null
);
} else if (config instanceof TimedUpdaterConfig.TextChannelConfig) {
updateChannel(
jda,
((TimedUpdaterConfig.TextChannelConfig) config).channelIds,
((TimedUpdaterConfig.TextChannelConfig) config).nameFormat,
((TimedUpdaterConfig.TextChannelConfig) config).topicFormat
);
}
}
private void updateChannel(JDA jda, List<Long> channelIds, String nameFormat, String topicFormat) {
if (topicFormat != null) {
topicFormat = discordSRV.placeholderService().replacePlaceholders(topicFormat);
}
@ -99,7 +122,7 @@ public class ChannelUpdaterModule extends AbstractModule<DiscordSRV> {
nameFormat = discordSRV.placeholderService().replacePlaceholders(nameFormat);
}
for (Long channelId : config.channelIds) {
for (Long channelId : channelIds) {
GuildChannel channel = jda.getGuildChannelById(channelId);
if (channel == null) {
continue;

View File

@ -23,7 +23,7 @@ import com.discordsrv.common.command.game.abstraction.GameCommand;
import com.discordsrv.common.command.game.commands.DiscordSRVGameCommand;
import com.discordsrv.common.command.game.commands.subcommand.LinkCommand;
import com.discordsrv.common.command.game.handler.ICommandHandler;
import com.discordsrv.common.config.main.CommandConfig;
import com.discordsrv.common.config.main.GameCommandConfig;
import com.discordsrv.common.module.type.AbstractModule;
import java.util.HashSet;
@ -48,7 +48,7 @@ public class GameCommandModule extends AbstractModule<DiscordSRV> {
@Override
public void reloadNoResult() {
CommandConfig config = discordSRV.config().command;
GameCommandConfig config = discordSRV.config().gameCommand;
if (config == null) {
return;
}

View File

@ -68,7 +68,7 @@ public class DiscordSRVGameCommand implements GameCommandExecutor {
@Override
public void execute(ICommandSender sender, GameCommandArguments arguments) {
MinecraftComponent component = discordSRV.componentFactory()
.textBuilder(discordSRV.config().command.discordFormat)
.textBuilder(discordSRV.config().gameCommand.discordFormat)
.addContext(sender)
.applyPlaceholderService()
.build();

View File

@ -1,43 +0,0 @@
/*
* This file is part of DiscordSRV, licensed under the GPLv3 License
* Copyright (c) 2016-2023 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.config.main;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import org.spongepowered.configurate.objectmapping.meta.Comment;
import java.util.ArrayList;
import java.util.List;
@ConfigSerializable
public class ChannelUpdaterConfig {
@Comment("The channel IDs.\n"
+ "The bot will need the \"View Channel\" and \"Manage Channels\" permissions for the provided channels, "
+ "additionally \"Connect\" is required for voice channels")
public List<Long> channelIds = new ArrayList<>();
@Comment("If this is blank, the name will not be updated")
public String nameFormat = "";
@Comment("If this is blank, the topic will not be updated. Unavailable for voice channels")
public String topicFormat = "";
@Comment("The time between updates in minutes. The minimum time is 10 minutes.")
public int timeMinutes = 10;
}

View File

@ -31,7 +31,7 @@ import java.util.Optional;
public class DiscordIgnoresConfig {
@Comment("User, bot and webhook ids to ignore")
public IDs usersAndWebhookIds = new IDs();
public IDs userBotAndWebhookIds = new IDs();
@Comment("Role ids for users and bots to ignore")
public IDs roleIds = new IDs();
@ -58,7 +58,7 @@ public class DiscordIgnoresConfig {
return true;
}
DiscordIgnoresConfig.IDs users = usersAndWebhookIds;
DiscordIgnoresConfig.IDs users = userBotAndWebhookIds;
if (users != null && users.ids.contains(author.getId()) != users.whitelist) {
return true;
}

View File

@ -22,7 +22,7 @@ import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import org.spongepowered.configurate.objectmapping.meta.Comment;
@ConfigSerializable
public class CommandConfig {
public class GameCommandConfig {
@Comment("If the /discord command should be set by DiscordSRV")
public boolean useDiscordCommand = true;
@ -30,6 +30,6 @@ public class CommandConfig {
@Comment("If /link should be used as a alias for /discord link")
public boolean useLinkAlias = false;
@Comment("Discord command format, player placeholders may be used")
@Comment("The Discord command response format (/discord), player placeholders may be used")
public String discordFormat = "[click:open_url:%discord_invite%]&b&lClick here &r&ato join our Discord server!";
}

View File

@ -57,13 +57,13 @@ public abstract class MainConfig implements Config {
public MemberCachingConfig memberCaching = new MemberCachingConfig();
public List<ChannelUpdaterConfig> channelUpdaters = new ArrayList<>(Collections.singletonList(new ChannelUpdaterConfig()));
public TimedUpdaterConfig timedUpdater = new TimedUpdaterConfig();
@Comment("Configuration options for group-role synchronization")
public GroupSyncConfig groupSync = new GroupSyncConfig();
@Comment("Command configuration")
public CommandConfig command = new CommandConfig();
@Comment("In-game command configuration")
public GameCommandConfig gameCommand = new GameCommandConfig();
@Comment("Configuration for the %discord_invite% placeholder. The below options will be attempted in the order they are in")
public DiscordInviteConfig invite = new DiscordInviteConfig();

View File

@ -0,0 +1,111 @@
/*
* This file is part of DiscordSRV, licensed under the GPLv3 License
* Copyright (c) 2016-2023 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.config.main;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import org.spongepowered.configurate.objectmapping.meta.Comment;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
@ConfigSerializable
public class TimedUpdaterConfig {
public List<VoiceChannelConfig> voiceChannels = new ArrayList<>(Collections.singletonList(new VoiceChannelConfig()));
public List<TextChannelConfig> textChannels = new ArrayList<>(Collections.singletonList(new TextChannelConfig()));
public List<UpdaterConfig> getConfigs() {
List<UpdaterConfig> configs = new ArrayList<>();
configs.addAll(voiceChannels);
configs.addAll(textChannels);
return configs;
}
public interface UpdaterConfig {
boolean any();
long timeSeconds();
long minimumSeconds();
}
public static class VoiceChannelConfig implements UpdaterConfig {
@Comment("The channel IDs.\n"
+ "The bot will need the \"View Channel\", \"Manage Channels\" and \"Connection\" permissions for the provided channels")
public List<Long> channelIds = new ArrayList<>();
@Comment("The format for the channel name(s), placeholders are supported.")
public String nameFormat = "";
@Comment("The time between updates in minutes. The minimum time is 10 minutes.")
public int timeMinutes = 10;
@Override
public boolean any() {
return !channelIds.isEmpty();
}
@Override
public long timeSeconds() {
return TimeUnit.MINUTES.toSeconds(timeMinutes);
}
@Override
public long minimumSeconds() {
return TimeUnit.MINUTES.toSeconds(10);
}
}
public static class TextChannelConfig implements UpdaterConfig {
@Comment("The channel IDs.\n"
+ "The bot will need the \"View Channel\" and \"Manage Channels\" permissions for the provided channels")
public List<Long> channelIds = new ArrayList<>();
@Comment("The format for the channel name(s), placeholders are supported.\n"
+ "If this is blank, the name will not be updated")
public String nameFormat = "";
@Comment("The format for the channel topic(s), placeholders are supported.\n"
+ "If this is blank, the topic will not be updated")
public String topicFormat = "";
@Comment("The time between updates in minutes. The minimum time is 10 minutes.")
public int timeMinutes = 10;
@Override
public boolean any() {
return !channelIds.isEmpty();
}
@Override
public long timeSeconds() {
return TimeUnit.MINUTES.toSeconds(timeMinutes);
}
@Override
public long minimumSeconds() {
return TimeUnit.MINUTES.toSeconds(10);
}
}
}

View File

@ -39,8 +39,11 @@ public class ChannelLockingConfig {
@Comment("Role ids for roles that should have the permissions taken while the server is offline")
public List<Long> roleIds = new ArrayList<>();
@Comment("If the \"View Channel\" permission should be taken from the specified roles")
public boolean read = false;
@Comment("If the \"Send Messages\" permission should be taken from the specified roles")
public boolean write = true;
@Comment("If the \"Add Reactions\" permission should be taken from the specified roles")
public boolean addReactions = true;
}

View File

@ -22,12 +22,10 @@ import com.discordsrv.api.discord.entity.message.DiscordMessageEmbed;
import com.discordsrv.api.discord.entity.message.SendableDiscordMessage;
import com.discordsrv.common.config.annotation.Untranslated;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import org.spongepowered.configurate.objectmapping.meta.Comment;
@ConfigSerializable
public class DeathMessageConfig implements IMessageConfig {
@Comment("Enable death message forwarding")
public boolean enabled = true;
@Untranslated(Untranslated.Type.VALUE)

View File

@ -30,7 +30,6 @@ import java.util.regex.Pattern;
@ConfigSerializable
public class DiscordToMinecraftChatConfig {
@Comment("Is Discord to Minecraft chat forwarding enabled")
public boolean enabled = true;
@Comment("The Discord to Minecraft message format for regular users and bots")

View File

@ -31,7 +31,6 @@ import java.util.regex.Pattern;
@ConfigSerializable
public class MinecraftToDiscordChatConfig implements IMessageConfig {
@Comment("Is Minecraft to Discord chat forwarding enabled")
public boolean enabled = true;
@Untranslated(Untranslated.Type.VALUE)

View File

@ -35,8 +35,10 @@ public class MirroringConfig {
+ "in order to prevent Discord users and in-game players with the same name being grouped together")
public String usernameFormat = "%user_effective_name% \uD83D\uDD03";
@Comment("Content to append to the beginning of a message if the message is replying to another")
public String replyFormat = "[In reply to %user_effective_name|user_name%](%message_jump_url%)\n";
@Comment("The format when a message is a reply.\n"
+ "%message% will be replaced with the message content\n"
+ "%message_jump_url% will be replaced with the url to the replied message in the channel the message is sent in")
public String replyFormat = "[In reply to %user_effective_name|user_name%](%message_jump_url%)\n%message%";
@Comment("Attachment related options")
public AttachmentConfig attachments = new AttachmentConfig();

View File

@ -30,7 +30,7 @@ public class LinkedAccountConfig {
@Comment("The linked account provider\n"
+ "\n"
+ " - auto: Defaults to using \"minecraftauth\" (if the " + ConnectionConfig.FILE_NAME + " permits it) otherwise \"storage\"\n"
+ " - auto: Uses \"minecraftauth\" if the " + ConnectionConfig.FILE_NAME + " permits it and the server is in online mode, otherwise \"storage\"\n"
+ " - minecraftauth: Uses minecraftauth.me as the linked account provider\n"
+ " - storage: Use the configured database for linked accounts")
public String provider = "auto";

View File

@ -20,12 +20,10 @@ package com.discordsrv.common.config.main.linking;
import com.discordsrv.common.config.annotation.Order;
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
import org.spongepowered.configurate.objectmapping.meta.Comment;
@ConfigSerializable
public abstract class RequiredLinkingConfig {
@Comment("If required linking is enabled")
@Order(-10)
public boolean enabled = false;
}

View File

@ -20,10 +20,20 @@ package com.discordsrv.common.debug.data;
public enum OnlineMode {
ONLINE,
OFFLINE,
BUNGEE,
VELOCITY;
ONLINE(true),
OFFLINE(false),
BUNGEE(true),
VELOCITY(true);
private final boolean online;
OnlineMode(boolean online) {
this.online = online;
}
public boolean isOnline() {
return online;
}
public static OnlineMode of(boolean onlineMode) {
return onlineMode ? OnlineMode.ONLINE : OnlineMode.OFFLINE;

View File

@ -309,7 +309,8 @@ public class DiscordMessageMirroringModule extends AbstractModule<DiscordSRV> {
content = discordSRV.placeholderService()
.replacePlaceholders(config.replyFormat, replyMessage.getMember(), replyMessage.getAuthor())
.replace("%message_jump_url%", jumpUrl) + content;
.replace("%message_jump_url%", jumpUrl)
.replace("%message%", content);
}
SendableDiscordMessage.Builder builder = SendableDiscordMessage.builder()