Updated to Discord4J v3, permission injection, improvements #99

Merged
NorbiPeti merged 25 commits from dev into master 2019-06-06 20:45:22 +00:00
3 changed files with 44 additions and 18 deletions
Showing only changes of commit 9edfcf6a3d - Show all commits

View file

@ -102,20 +102,34 @@ public final class DPUtils {
public static boolean disableIfConfigError(@Nullable Component<DiscordPlugin> component, ConfigData<?>... configs) {
for (val config : configs) {
Object v = config.get();
//noinspection ConstantConditions
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.");
if (disableIfConfigErrorRes(component, config, v))
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;
}

View file

@ -66,9 +66,14 @@ public class DiscordPlugin extends ButtonPlugin {
}
private ConfigData<Optional<Guild>> mainServer() {
return getIConfig().getDataPrimDef("mainServer", 219529124321034241L,
id -> dc.getGuildById(Snowflake.of((long) id))
.onErrorContinue((t, o) -> getLogger().warning("Failed to get guild: " + t)).blockOptional(),
return getIConfig().getDataPrimDef("mainServer", 0L,
id -> {
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));
}
@ -102,7 +107,7 @@ public class DiscordPlugin extends ButtonPlugin {
File privateFile = new File(getDataFolder(), "private.yml");
val conf = YamlConfiguration.loadConfiguration(privateFile);
token = conf.getString("token");
if (token == null) {
if (token == null || token.equalsIgnoreCase("Token goes here")) {
conf.set("token", "Token goes here");
conf.save(privateFile);
@ -133,7 +138,10 @@ public class DiscordPlugin extends ButtonPlugin {
private void handleReady(List<GuildCreateEvent> event) {
try {
System.out.println("w t f: " + mainServer);
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 (event.size() == 0) {
getLogger().severe("Main server not found! Invite the bot and do /discord reset");
@ -182,7 +190,8 @@ public class DiscordPlugin extends ButtonPlugin {
TBMCCoreAPI.SendException(
"Won't load because we're in testing mode and not using a separate account.",
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);
}
TBMCCoreAPI.SendUnsentExceptions();

View file

@ -79,7 +79,8 @@ public class MinecraftChatModule extends Component<DiscordPlugin> {
@Override
protected void enable() {
if (DPUtils.disableIfConfigError(this, chatChannel())) return;
if (DPUtils.disableIfConfigErrorRes(this, chatChannel(), chatChannelMono()))
return;
listener = new MCChatListener(this);
TBMCCoreAPI.RegisterEventsForExceptions(listener, getPlugin());
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);
} catch (Exception e) {
TBMCCoreAPI.SendException("Failed to init LuckPerms injector", e);
} catch (NoClassDefFoundError e) {
getPlugin().getLogger().info("No LuckPerms, not injecting");
}
}