Some more fixes

Made the channel data return a Mono and used that
#93
This commit is contained in:
Norbi Peti 2019-05-01 00:50:24 +02:00
parent eeb1955ebe
commit 4881f6bdd2
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
7 changed files with 88 additions and 70 deletions

View file

@ -46,7 +46,7 @@ public final class DPUtils {
return sanitizedString.toString(); return sanitizedString.toString();
} }
public static String escape(String message) { private static String escape(String message) {
return message.replaceAll("([*_~])", Matcher.quoteReplacement("\\") + "$1"); return message.replaceAll("([*_~])", Matcher.quoteReplacement("\\") + "$1");
} }
@ -69,15 +69,18 @@ public final class DPUtils {
}, ch -> ch.getId().asLong()); //We can afford to search for the channel in the cache once (instead of using mainServer) }, ch -> ch.getId().asLong()); //We can afford to search for the channel in the cache once (instead of using mainServer)
} }
public static ConfigData<Role> roleData(IHaveConfig config, String key, String defName) { public static ConfigData<Mono<Role>> roleData(IHaveConfig config, String key, String defName) {
return roleData(config, key, defName, DiscordPlugin.mainServer); return roleData(config, key, defName, Mono.just(DiscordPlugin.mainServer));
} }
public static ConfigData<Role> roleData(IHaveConfig config, String key, String defName, Guild guild) { /**
* Needs to be a {@link ConfigData} for checking if it's set
*/
public static ConfigData<Mono<Role>> roleData(IHaveConfig config, String key, String defName, Mono<Guild> guild) {
return config.getDataPrimDef(key, defName, name -> { return config.getDataPrimDef(key, defName, name -> {
if (!(name instanceof String)) return null; if (!(name instanceof String)) return Mono.empty();
return guild.getRoles().filter(r -> r.getName().equals(name)).blockFirst(); return guild.flatMapMany(Guild::getRoles).filter(r -> r.getName().equals(name)).last();
}, r -> r.getId().asLong()); }, r -> defName);
} }
/** /**

View file

@ -16,6 +16,7 @@ import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.architecture.ButtonPlugin; import buttondevteam.lib.architecture.ButtonPlugin;
import buttondevteam.lib.architecture.Component; import buttondevteam.lib.architecture.Component;
import buttondevteam.lib.architecture.ConfigData; import buttondevteam.lib.architecture.ConfigData;
import buttondevteam.lib.architecture.IHaveConfig;
import buttondevteam.lib.player.ChromaGamerBase; import buttondevteam.lib.player.ChromaGamerBase;
import com.google.common.io.Files; import com.google.common.io.Files;
import discord4j.core.DiscordClient; import discord4j.core.DiscordClient;
@ -36,16 +37,17 @@ import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.RegisteredServiceProvider;
import reactor.core.publisher.Mono;
import java.awt.*; import java.awt.*;
import java.io.File; import java.io.File;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ButtonPlugin.ConfigOpts(disableConfigGen = true)
public class DiscordPlugin extends ButtonPlugin { public class DiscordPlugin extends ButtonPlugin {
public static DiscordClient dc; public static DiscordClient dc;
public static DiscordPlugin plugin; public static DiscordPlugin plugin;
@ -53,7 +55,7 @@ public class DiscordPlugin extends ButtonPlugin {
@Getter @Getter
private Command2DC manager; private Command2DC manager;
public ConfigData<Character> Prefix() { private ConfigData<Character> Prefix() {
return getIConfig().getData("prefix", '/', str -> ((String) str).charAt(0), Object::toString); return getIConfig().getData("prefix", '/', str -> ((String) str).charAt(0), Object::toString);
} }
@ -62,7 +64,7 @@ public class DiscordPlugin extends ButtonPlugin {
return plugin.Prefix().get(); return plugin.Prefix().get();
} }
public ConfigData<Guild> MainServer() { private ConfigData<Guild> MainServer() {
return getIConfig().getDataPrimDef("mainServer", 219529124321034241L, id -> dc.getGuildById(Snowflake.of((long) id)).block(), g -> g.getId().asLong()); return getIConfig().getDataPrimDef("mainServer", 219529124321034241L, id -> dc.getGuildById(Snowflake.of((long) id)).block(), g -> g.getId().asLong());
} }
@ -70,7 +72,7 @@ public class DiscordPlugin extends ButtonPlugin {
return DPUtils.channelData(getIConfig(), "commandChannel", 239519012529111040L); return DPUtils.channelData(getIConfig(), "commandChannel", 239519012529111040L);
} }
public ConfigData<Role> ModRole() { public ConfigData<Mono<Role>> ModRole() {
return DPUtils.roleData(getIConfig(), "modRole", "Moderator"); return DPUtils.roleData(getIConfig(), "modRole", "Moderator");
} }
@ -193,6 +195,8 @@ public class DiscordPlugin extends ButtonPlugin {
ChromaGamerBase.addConverter(sender -> Optional.ofNullable(sender instanceof DiscordSenderBase ChromaGamerBase.addConverter(sender -> Optional.ofNullable(sender instanceof DiscordSenderBase
? ((DiscordSenderBase) sender).getChromaUser() : null)); ? ((DiscordSenderBase) sender).getChromaUser() : null));
setupProviders(); setupProviders();
IHaveConfig.pregenConfig(this, null);
} catch (Exception e) { } catch (Exception e) {
TBMCCoreAPI.SendException("An error occured while enabling DiscordPlugin!", e); TBMCCoreAPI.SendException("An error occured while enabling DiscordPlugin!", e);
} }
@ -220,7 +224,7 @@ public class DiscordPlugin extends ButtonPlugin {
+ (Bukkit.getOnlinePlayers().size() == 1 ? " was " : " were ") + (Bukkit.getOnlinePlayers().size() == 1 ? " was " : " were ")
+ "kicked the hell out.") //TODO: Make configurable + "kicked the hell out.") //TODO: Make configurable
: ""); //If 'restart' is disabled then this isn't shown even if joinleave is enabled : ""); //If 'restart' is disabled then this isn't shown even if joinleave is enabled
}).block(Duration.ofSeconds(5)), ChannelconBroadcast.RESTART, false); }).block(), ChannelconBroadcast.RESTART, false);
ChromaBot.getInstance().updatePlayerList(); ChromaBot.getInstance().updatePlayerList();
} }

View file

@ -3,6 +3,8 @@ package buttondevteam.discordplugin.commands;
import buttondevteam.discordplugin.DiscordPlugin; import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.lib.chat.Command2; import buttondevteam.lib.chat.Command2;
import java.lang.reflect.Method;
public class Command2DC extends Command2<ICommand2DC, Command2DCSender> { public class Command2DC extends Command2<ICommand2DC, Command2DCSender> {
@Override @Override
public void registerCommand(ICommand2DC command) { public void registerCommand(ICommand2DC command) {
@ -10,7 +12,7 @@ public class Command2DC extends Command2<ICommand2DC, Command2DCSender> {
} }
@Override @Override
public boolean hasPermission(Command2DCSender sender, ICommand2DC command) { public boolean hasPermission(Command2DCSender sender, ICommand2DC command, Method method) {
//return !command.isModOnly() || sender.getMessage().getAuthor().hasRole(DiscordPlugin.plugin.ModRole().get()); //TODO: ModRole may be null; more customisable way? //return !command.isModOnly() || sender.getMessage().getAuthor().hasRole(DiscordPlugin.plugin.ModRole().get()); //TODO: ModRole may be null; more customisable way?
return true; return true;
} }

View file

@ -12,7 +12,8 @@ public class DebugCommand extends ICommand2DC {
@Command2.Subcommand @Command2.Subcommand
public boolean def(Command2DCSender sender, String args) { public boolean def(Command2DCSender sender, String args) {
sender.getMessage().getAuthorAsMember() sender.getMessage().getAuthorAsMember()
.map(m -> m.getRoleIds().stream().anyMatch(r -> r.equals(DiscordPlugin.plugin.ModRole().get().getId()))) .flatMap(m -> DiscordPlugin.plugin.ModRole().get()
.map(mr -> m.getRoleIds().stream().anyMatch(r -> r.equals(mr.getId()))))
.subscribe(success -> { .subscribe(success -> {
if (success) if (success)
sender.sendMessage("debug " + (CommonListeners.debug() ? "enabled" : "disabled")); sender.sendMessage("debug " + (CommonListeners.debug() ? "enabled" : "disabled"));

View file

@ -15,6 +15,7 @@ import org.apache.commons.lang.exception.ExceptionUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import reactor.core.publisher.Mono;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -22,51 +23,58 @@ import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class ExceptionListenerModule extends Component<DiscordPlugin> implements Listener { public class ExceptionListenerModule extends Component<DiscordPlugin> implements Listener {
private List<Throwable> lastthrown = new ArrayList<>(); private List<Throwable> lastthrown = new ArrayList<>();
private List<String> lastsourcemsg = new ArrayList<>(); private List<String> lastsourcemsg = new ArrayList<>();
@EventHandler @EventHandler
public void onException(TBMCExceptionEvent e) { public void onException(TBMCExceptionEvent e) {
if (DiscordPlugin.SafeMode || !ComponentManager.isEnabled(getClass())) if (DiscordPlugin.SafeMode || !ComponentManager.isEnabled(getClass()))
return; return;
if (lastthrown.stream() if (lastthrown.stream()
.anyMatch(ex -> Arrays.equals(e.getException().getStackTrace(), ex.getStackTrace()) .anyMatch(ex -> Arrays.equals(e.getException().getStackTrace(), ex.getStackTrace())
&& (e.getException().getMessage() == null ? ex.getMessage() == null && (e.getException().getMessage() == null ? ex.getMessage() == null
: e.getException().getMessage().equals(ex.getMessage()))) // e.Exception.Message==ex.Message : e.getException().getMessage().equals(ex.getMessage()))) // e.Exception.Message==ex.Message
&& lastsourcemsg.contains(e.getSourceMessage())) && lastsourcemsg.contains(e.getSourceMessage()))
return; return;
SendException(e.getException(), e.getSourceMessage()); SendException(e.getException(), e.getSourceMessage());
if (lastthrown.size() >= 10) if (lastthrown.size() >= 10)
lastthrown.remove(0); lastthrown.remove(0);
if (lastsourcemsg.size() >= 10) if (lastsourcemsg.size() >= 10)
lastsourcemsg.remove(0); lastsourcemsg.remove(0);
lastthrown.add(e.getException()); lastthrown.add(e.getException());
lastsourcemsg.add(e.getSourceMessage()); lastsourcemsg.add(e.getSourceMessage());
e.setHandled(); e.setHandled();
} }
private static void SendException(Throwable e, String sourcemessage) { private static void SendException(Throwable e, String sourcemessage) {
if (instance == null) return; if (instance == null) return;
try { try {
MessageChannel channel = getChannel(); MessageChannel channel = getChannel();
assert channel != null; assert channel != null;
Role coderRole = instance.pingRole(((GuildChannel) channel).getGuild().block()).get(); Mono<Role> coderRole;
StringBuilder sb = TBMCCoreAPI.IsTestServer() ? new StringBuilder() if (channel instanceof GuildChannel)
: new StringBuilder(coderRole == null ? "" : coderRole.getMention()).append("\n"); coderRole = instance.pingRole(((GuildChannel) channel).getGuild()).get();
sb.append(sourcemessage).append("\n"); else
sb.append("```").append("\n"); coderRole = Mono.empty();
String stackTrace = Arrays.stream(ExceptionUtils.getStackTrace(e).split("\\n")) coderRole.map(role -> TBMCCoreAPI.IsTestServer() ? new StringBuilder()
.filter(s -> !s.contains("\tat ") || s.contains("\tat buttondevteam.")) : new StringBuilder(role.getMention()).append("\n"))
.collect(Collectors.joining("\n")); .defaultIfEmpty(new StringBuilder())
if (sb.length() + stackTrace.length() >= 2000) .flatMap(sb -> {
stackTrace = stackTrace.substring(0, 1999 - sb.length()); sb.append(sourcemessage).append("\n");
sb.append(stackTrace).append("\n"); sb.append("```").append("\n");
sb.append("```"); String stackTrace = Arrays.stream(ExceptionUtils.getStackTrace(e).split("\\n"))
channel.createMessage(sb.toString()).subscribe(); .filter(s -> !s.contains("\tat ") || s.contains("\tat buttondevteam."))
} catch (Exception ex) { .collect(Collectors.joining("\n"));
ex.printStackTrace(); if (sb.length() + stackTrace.length() >= 2000)
} stackTrace = stackTrace.substring(0, 1999 - sb.length());
} sb.append(stackTrace).append("\n");
sb.append("```");
return channel.createMessage(sb.toString());
}).subscribe();
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static ExceptionListenerModule instance; private static ExceptionListenerModule instance;
@ -79,7 +87,7 @@ public class ExceptionListenerModule extends Component<DiscordPlugin> implements
return DPUtils.channelData(getConfig(), "channel", 239519012529111040L); return DPUtils.channelData(getConfig(), "channel", 239519012529111040L);
} }
private ConfigData<Role> pingRole(Guild guild) { private ConfigData<Mono<Role>> pingRole(Mono<Guild> guild) {
return DPUtils.roleData(getConfig(), "pingRole", "Coder", guild); return DPUtils.roleData(getConfig(), "pingRole", "Coder", guild);
} }

View file

@ -15,6 +15,7 @@ import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerJoinEvent;
import reactor.core.publisher.Mono;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -114,7 +115,7 @@ public class FunModule extends Component<DiscordPlugin> implements Listener {
ListC = 0; ListC = 0;
} }
private ConfigData<Role> fullHouseDevRole(Guild guild) { private ConfigData<Mono<Role>> fullHouseDevRole(Mono<Guild> guild) {
return DPUtils.roleData(getConfig(), "fullHouseDevRole", "Developer", guild); return DPUtils.roleData(getConfig(), "fullHouseDevRole", "Developer", guild);
} }
@ -131,20 +132,21 @@ public class FunModule extends Component<DiscordPlugin> implements Listener {
if (fm == null) return; if (fm == null) return;
val channel = fm.fullHouseChannel().get(); val channel = fm.fullHouseChannel().get();
if (channel == null) return; if (channel == null) return;
val devrole = fm.fullHouseDevRole(((GuildChannel) channel).getGuild().block()).get(); if (!(channel instanceof GuildChannel)) return;
val devrole = fm.fullHouseDevRole(((GuildChannel) channel).getGuild()).get();
if (devrole == null) return; if (devrole == null) return;
if (event.getOld().map(p -> p.getStatus().equals(Status.OFFLINE)).orElse(false) if (event.getOld().map(p -> p.getStatus().equals(Status.OFFLINE)).orElse(false)
&& !event.getCurrent().getStatus().equals(Status.OFFLINE) && !event.getCurrent().getStatus().equals(Status.OFFLINE)
&& event.getMember().flatMap(m -> m.getRoles() && event.getMember().flatMap(m -> devrole.flatMap(dr -> m.getRoles()
.any(r -> r.getId().asLong() == devrole.getId().asLong())).block() .any(r -> r.getId().asLong() == dr.getId().asLong()))).block()
&& event.getGuild().flatMap(g -> g.getMembers().filter(m -> m.getRoleIds().stream().anyMatch(s -> s.equals(devrole.getId()))) && event.getGuild().flatMap(g -> devrole.flatMapMany(dr -> g.getMembers().filter(m -> m.getRoleIds().stream().anyMatch(s -> s.equals(dr.getId()))))
.flatMap(Member::getPresence).all(pr -> !pr.getStatus().equals(Status.OFFLINE))).block() .flatMap(Member::getPresence).all(pr -> !pr.getStatus().equals(Status.OFFLINE))).block()
&& lasttime + 10 < TimeUnit.NANOSECONDS.toHours(System.nanoTime()) && lasttime + 10 < TimeUnit.NANOSECONDS.toHours(System.nanoTime())
&& Calendar.getInstance().get(Calendar.DAY_OF_MONTH) % 5 == 0) { && Calendar.getInstance().get(Calendar.DAY_OF_MONTH) % 5 == 0) {
channel.createMessage(mcs -> mcs.setContent("Full house!").setEmbed(ecs -> channel.createMessage(mcs -> mcs.setContent("Full house!").setEmbed(ecs ->
ecs.setImage( ecs.setImage(
"https://cdn.discordapp.com/attachments/249295547263877121/249687682618359808/poker-hand-full-house-aces-kings-playing-cards-15553791.png") "https://cdn.discordapp.com/attachments/249295547263877121/249687682618359808/poker-hand-full-house-aces-kings-playing-cards-15553791.png")
)); )).subscribe();
lasttime = TimeUnit.NANOSECONDS.toHours(System.nanoTime()); lasttime = TimeUnit.NANOSECONDS.toHours(System.nanoTime());
} }
} }

View file

@ -1,13 +1,11 @@
package buttondevteam.discordplugin.mcchat; package buttondevteam.discordplugin.mcchat;
import buttondevteam.discordplugin.*; import buttondevteam.discordplugin.*;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.TBMCSystemChatEvent; import buttondevteam.lib.TBMCSystemChatEvent;
import buttondevteam.lib.architecture.ConfigData; import buttondevteam.lib.architecture.ConfigData;
import buttondevteam.lib.player.*; import buttondevteam.lib.player.*;
import com.earth2me.essentials.CommandSource; import com.earth2me.essentials.CommandSource;
import discord4j.core.object.entity.Role; import discord4j.core.object.entity.Role;
import discord4j.core.object.entity.User;
import discord4j.core.object.util.Snowflake; import discord4j.core.object.util.Snowflake;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.val; import lombok.val;
@ -101,13 +99,13 @@ class MCListener implements Listener {
MCChatUtils.forAllowedCustomAndAllMCChat(MCChatUtils.send(msg), base, ChannelconBroadcast.AFK, false); MCChatUtils.forAllowedCustomAndAllMCChat(MCChatUtils.send(msg), base, ChannelconBroadcast.AFK, false);
} }
private ConfigData<Role> muteRole() { private ConfigData<Mono<Role>> muteRole() {
return DPUtils.roleData(module.getConfig(), "muteRole", "Muted"); return DPUtils.roleData(module.getConfig(), "muteRole", "Muted");
} }
@EventHandler @EventHandler
public void onPlayerMute(MuteStatusChangeEvent e) { public void onPlayerMute(MuteStatusChangeEvent e) {
final Role role = muteRole().get(); final Mono<Role> role = muteRole().get();
if (role == null) return; if (role == null) return;
final CommandSource source = e.getAffected().getSource(); final CommandSource source = e.getAffected().getSource();
if (!source.isPlayer()) if (!source.isPlayer())
@ -117,18 +115,18 @@ class MCListener implements Listener {
if (p == null) return; if (p == null) return;
DiscordPlugin.dc.getUserById(Snowflake.of(p.getDiscordID())) DiscordPlugin.dc.getUserById(Snowflake.of(p.getDiscordID()))
.flatMap(user -> user.asMember(DiscordPlugin.mainServer.getId())) .flatMap(user -> user.asMember(DiscordPlugin.mainServer.getId()))
.flatMap(user -> { .flatMap(user -> role.flatMap(r -> {
if (e.getValue()) if (e.getValue())
user.addRole(role.getId()); user.addRole(r.getId());
else else
user.removeRole(role.getId()); user.removeRole(r.getId());
val modlog = module.modlogChannel().get(); val modlog = module.modlogChannel().get();
String msg = (e.getValue() ? "M" : "Unm") + "uted user: " + user.getUsername() + "#" + user.getDiscriminator(); String msg = (e.getValue() ? "M" : "Unm") + "uted user: " + user.getUsername() + "#" + user.getDiscriminator();
DPUtils.getLogger().info(msg); DPUtils.getLogger().info(msg);
if (modlog != null) if (modlog != null)
return modlog.createMessage(msg); return modlog.createMessage(msg);
return Mono.empty(); return Mono.empty();
}).subscribe(); })).subscribe();
} }
@EventHandler @EventHandler