Main server fix, clean test OK

WTF ID fixed
This commit is contained in:
Norbi Peti 2019-06-05 00:29:34 +02:00
parent 939c6f4970
commit 9edfcf6a3d
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
3 changed files with 44 additions and 18 deletions

View file

@ -102,20 +102,34 @@ public final class DPUtils {
public static boolean disableIfConfigError(@Nullable Component<DiscordPlugin> component, ConfigData<?>... configs) { public static boolean disableIfConfigError(@Nullable Component<DiscordPlugin> component, ConfigData<?>... configs) {
for (val config : configs) { for (val config : configs) {
Object v = config.get(); Object v = config.get();
//noinspection ConstantConditions if (disableIfConfigErrorRes(component, config, v))
if (v == null || (v instanceof Mono<?> && !((Mono<?>) v).hasElement().block())) {
String path = null;
try {
if (component != null)
Component.setComponentEnabled(component, false);
path = config.getPath();
} catch (Exception e) {
TBMCCoreAPI.SendException("Failed to disable component after config error!", e);
}
getLogger().warning("The config value " + path + " isn't set correctly " + (component == null ? "in global settings!" : "for component " + component.getClass().getSimpleName() + "!"));
getLogger().warning("Set the correct ID in the config" + (component == null ? "" : " or disable this component") + " to remove this message.");
return true; return true;
}
return false;
}
/**
* Disables the component if one of the given configs return null. Useful for channel/role configs.
*
* @param component The component to disable if needed
* @param config The (snowflake) config to check for null
* @param result The result of getting the value
* @return Whether the component got disabled and a warning logged
*/
public static boolean disableIfConfigErrorRes(@Nullable Component<DiscordPlugin> component, ConfigData<?> config, Object result) {
//noinspection ConstantConditions
if (result == null || (result instanceof Mono<?> && !((Mono<?>) result).hasElement().block())) {
String path = null;
try {
if (component != null)
Component.setComponentEnabled(component, false);
path = config.getPath();
} catch (Exception e) {
TBMCCoreAPI.SendException("Failed to disable component after config error!", e);
} }
getLogger().warning("The config value " + path + " isn't set correctly " + (component == null ? "in global settings!" : "for component " + component.getClass().getSimpleName() + "!"));
getLogger().warning("Set the correct ID in the config" + (component == null ? "" : " or disable this component") + " to remove this message.");
return true;
} }
return false; return false;
} }

View file

@ -66,9 +66,14 @@ public class DiscordPlugin extends ButtonPlugin {
} }
private ConfigData<Optional<Guild>> mainServer() { private ConfigData<Optional<Guild>> mainServer() {
return getIConfig().getDataPrimDef("mainServer", 219529124321034241L, return getIConfig().getDataPrimDef("mainServer", 0L,
id -> dc.getGuildById(Snowflake.of((long) id)) id -> {
.onErrorContinue((t, o) -> getLogger().warning("Failed to get guild: " + t)).blockOptional(), System.out.println("WTF ID: " + id); //TODO: It attempts to get the default as well
if ((long) id == 0L)
return Optional.empty(); //Hack?
return dc.getGuildById(Snowflake.of((long) id))
.onErrorResume(t -> Mono.fromRunnable(() -> getLogger().warning("Failed to get guild: " + t.getMessage()))).blockOptional();
},
g -> g.map(gg -> gg.getId().asLong()).orElse(0L)); g -> g.map(gg -> gg.getId().asLong()).orElse(0L));
} }
@ -102,7 +107,7 @@ public class DiscordPlugin extends ButtonPlugin {
File privateFile = new File(getDataFolder(), "private.yml"); File privateFile = new File(getDataFolder(), "private.yml");
val conf = YamlConfiguration.loadConfiguration(privateFile); val conf = YamlConfiguration.loadConfiguration(privateFile);
token = conf.getString("token"); token = conf.getString("token");
if (token == null) { if (token == null || token.equalsIgnoreCase("Token goes here")) {
conf.set("token", "Token goes here"); conf.set("token", "Token goes here");
conf.save(privateFile); conf.save(privateFile);
@ -133,7 +138,10 @@ public class DiscordPlugin extends ButtonPlugin {
private void handleReady(List<GuildCreateEvent> event) { private void handleReady(List<GuildCreateEvent> event) {
try { try {
System.out.println("w t f: " + mainServer);
mainServer = mainServer().get().orElse(null); //Shouldn't change afterwards mainServer = mainServer().get().orElse(null); //Shouldn't change afterwards
System.out.println("Main server: " + mainServer);
System.out.println("wtf: " + mainServer().get());
if (mainServer == null) { if (mainServer == null) {
if (event.size() == 0) { if (event.size() == 0) {
getLogger().severe("Main server not found! Invite the bot and do /discord reset"); getLogger().severe("Main server not found! Invite the bot and do /discord reset");
@ -182,7 +190,8 @@ public class DiscordPlugin extends ButtonPlugin {
TBMCCoreAPI.SendException( TBMCCoreAPI.SendException(
"Won't load because we're in testing mode and not using a separate account.", "Won't load because we're in testing mode and not using a separate account.",
new Exception( new Exception(
"The plugin refuses to load until you change the token to a testing account. (The account needs to have \"test\" in its name.)")); "The plugin refuses to load until you change the token to a testing account. (The account needs to have \"test\" in its name.)"
+ "\nYou can disable test mode in ThorpeCore config."));
Bukkit.getPluginManager().disablePlugin(this); Bukkit.getPluginManager().disablePlugin(this);
} }
TBMCCoreAPI.SendUnsentExceptions(); TBMCCoreAPI.SendUnsentExceptions();

View file

@ -79,7 +79,8 @@ public class MinecraftChatModule extends Component<DiscordPlugin> {
@Override @Override
protected void enable() { protected void enable() {
if (DPUtils.disableIfConfigError(this, chatChannel())) return; if (DPUtils.disableIfConfigErrorRes(this, chatChannel(), chatChannelMono()))
return;
listener = new MCChatListener(this); listener = new MCChatListener(this);
TBMCCoreAPI.RegisterEventsForExceptions(listener, getPlugin()); TBMCCoreAPI.RegisterEventsForExceptions(listener, getPlugin());
TBMCCoreAPI.RegisterEventsForExceptions(new MCListener(this), getPlugin());//These get undone if restarting/resetting - it will ignore events if disabled TBMCCoreAPI.RegisterEventsForExceptions(new MCListener(this), getPlugin());//These get undone if restarting/resetting - it will ignore events if disabled
@ -113,6 +114,8 @@ public class MinecraftChatModule extends Component<DiscordPlugin> {
new LPInjector(MainPlugin.Instance); new LPInjector(MainPlugin.Instance);
} catch (Exception e) { } catch (Exception e) {
TBMCCoreAPI.SendException("Failed to init LuckPerms injector", e); TBMCCoreAPI.SendException("Failed to init LuckPerms injector", e);
} catch (NoClassDefFoundError e) {
getPlugin().getLogger().info("No LuckPerms, not injecting");
} }
} }