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
10 changed files with 24 additions and 20 deletions
Showing only changes of commit 5472929113 - Show all commits

View file

@ -72,7 +72,7 @@
<relocations>
<relocation>
<pattern>io.netty</pattern>
<shadedPattern>buttondevteam.discordplugin.io.netty</shadedPattern>
<shadedPattern>btndvtm.dp.io.netty</shadedPattern>
<excludes>
</excludes>
</relocation>

View file

@ -4,14 +4,18 @@ import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.discordplugin.listeners.CommonListeners;
import buttondevteam.lib.chat.Command2;
import buttondevteam.lib.chat.CommandClass;
import reactor.core.publisher.Mono;
@CommandClass(helpText = {
"Switches debug mode."
})
public class DebugCommand extends ICommand2DC {
@Command2.Subcommand
public boolean def(Command2DCSender sender, String args) {
public boolean def(Command2DCSender sender) {
sender.getMessage().getAuthorAsMember()
.switchIfEmpty(sender.getMessage().getAuthor() //Support DMs
.map(u -> u.asMember(DiscordPlugin.mainServer.getId()))
.orElse(Mono.empty()))
.flatMap(m -> DiscordPlugin.plugin.modRole().get()
.map(mr -> m.getRoleIds().stream().anyMatch(r -> r.equals(mr.getId()))))
.subscribe(success -> {

View file

@ -66,8 +66,8 @@ public class ExceptionListenerModule extends Component<DiscordPlugin> implements
String stackTrace = Arrays.stream(ExceptionUtils.getStackTrace(e).split("\\n"))
.filter(s -> !s.contains("\tat ") || s.contains("\tat buttondevteam."))
.collect(Collectors.joining("\n"));
if (sb.length() + stackTrace.length() >= 2000)
stackTrace = stackTrace.substring(0, 1999 - sb.length());
if (sb.length() + stackTrace.length() >= 1980)
stackTrace = stackTrace.substring(0, 1980 - sb.length());
sb.append(stackTrace).append("\n");
sb.append("```");
return channel.flatMap(ch -> ch.createMessage(sb.toString()));

View file

@ -25,6 +25,9 @@ import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
/**
* The YEEHAW event uses an emoji named :YEEHAW: if available
*/
public class FunModule extends Component<DiscordPlugin> implements Listener {
private static final String[] serverReadyStrings = new String[]{"In one week from now", // Ali
"Between now and the heat-death of the universe.", // Ghostise

View file

@ -62,12 +62,13 @@ public class CommandListener {
try {
timings.printElapsed("F");
if (!DiscordPlugin.plugin.getManager().handleCommand(new Command2DCSender(message), cmdwithargsString))
return DPUtils.reply(message, channel, "Unknown command. Do " + DiscordPlugin.getPrefix() + "help for help.\n" + cmdwithargsString);
return DPUtils.reply(message, channel, "Unknown command. Do " + DiscordPlugin.getPrefix() + "help for help.\n" + cmdwithargsString)
.map(m -> false);
} catch (Exception e) {
TBMCCoreAPI.SendException("Failed to process Discord command: " + cmdwithargsString, e);
}
return Mono.empty();
}).map(m -> false).defaultIfEmpty(true);
return Mono.just(false); //If the command succeeded or there was an error, return false
}).defaultIfEmpty(true);
});
}

View file

@ -45,7 +45,7 @@ public class CommonListeners {
val commandChannel = DiscordPlugin.plugin.commandChannel().get();
val commandCh = DPUtils.getMessageChannel(DiscordPlugin.plugin.commandChannel());
return commandCh.filterWhen(ch -> event.getMessage().getChannel().map(mch ->
commandChannel != null && event.getMessage().getChannelId().asLong() == commandChannel.asLong() //If mentioned, that's higher than chat
(commandChannel != null && mch.getId().asLong() == commandChannel.asLong()) //If mentioned, that's higher than chat
|| mch instanceof PrivateChannel
|| event.getMessage().getContent().orElse("").contains("channelcon"))) //Only 'channelcon' is allowed in other channels
.filterWhen(ch -> { //Only continue if this doesn't handle the event

View file

@ -269,14 +269,12 @@ public class MCChatListener implements Listener {
rectask.cancel();
return;
}
System.out.println("Processing...");
val sender = event.getMessage().getAuthor().orElse(null);
String dmessage = event.getMessage().getContent().orElse("");
try {
final DiscordSenderBase dsender = MCChatUtils.getSender(event.getMessage().getChannelId(), sender);
val user = dsender.getChromaUser();
System.out.println("Mentions start");
for (User u : event.getMessage().getUserMentions().toIterable()) { //TODO: Role mentions
dmessage = dmessage.replace(u.getMention(), "@" + u.getUsername()); // TODO: IG Formatting
val m = u.asMember(DiscordPlugin.mainServer.getId()).block();
@ -288,7 +286,6 @@ public class MCChatListener implements Listener {
for (GuildChannel ch : event.getGuild().flux().flatMap(Guild::getChannels).toIterable()) {
dmessage = dmessage.replace(ch.getMention(), "#" + ch.getName()); // TODO: IG Formatting
}
System.out.println("Mentions end");
dmessage = EmojiParser.parseToAliases(dmessage, EmojiParser.FitzpatrickAction.PARSE); //Converts emoji to text- TODO: Add option to disable (resource pack?)
dmessage = dmessage.replaceAll(":(\\S+)\\|type_(?:(\\d)|(1)_2):", ":$1::skin-tone-$2:"); //Convert to Discord's format so it still shows up
@ -302,9 +299,7 @@ public class MCChatListener implements Listener {
boolean react = false;
System.out.println("Getting channel...");
val sendChannel = event.getMessage().getChannel().block();
System.out.println("Got channel");
boolean isPrivate = sendChannel instanceof PrivateChannel;
if (dmessage.startsWith("/")) { // Ingame command
if (!isPrivate)
@ -380,7 +375,6 @@ public class MCChatListener implements Listener {
}
}
} else {// Not a command
System.out.println("Not a command");
if (dmessage.length() == 0 && event.getMessage().getAttachments().size() == 0
&& !isPrivate && event.getMessage().getType() == Message.Type.CHANNEL_PINNED_MESSAGE) {
val rtr = clmd != null ? clmd.mcchannel.getRTR(clmd.dcp)
@ -390,13 +384,11 @@ public class MCChatListener implements Listener {
: dsender.getName()) + " pinned a message on Discord.", TBMCSystemChatEvent.BroadcastTarget.ALL);
} else {
val cmb = ChatMessage.builder(dsender, user, getChatMessage.apply(dmessage)).fromCommand(false);
System.out.println("Message created");
if (clmd != null)
TBMCChatAPI.SendChatMessage(cmb.permCheck(clmd.dcp).build(), clmd.mcchannel);
else
TBMCChatAPI.SendChatMessage(cmb.build());
react = true;
System.out.println("Message sent");
}
}
if (react) {

View file

@ -332,6 +332,7 @@ public class MCChatUtils {
}
callEventExcludingSome(new PlayerJoinEvent(dcp, ""));
dcp.setLoggedIn(true);
DPUtils.getLogger().info(dcp.getName() + " (" + dcp.getUniqueId() + ") logged in from Discord");
});
}
@ -347,6 +348,7 @@ public class MCChatUtils {
if (needsSync) callEventSync(event);
else callEventExcludingSome(event);
dcp.setLoggedIn(false);
DPUtils.getLogger().info(dcp.getName() + " (" + dcp.getUniqueId() + ") logged out from Discord");
}
static void callEventSync(Event event) {

View file

@ -25,6 +25,7 @@ import org.bukkit.event.server.BroadcastMessageEvent;
import reactor.core.publisher.Mono;
import java.util.Objects;
import java.util.Optional;
@RequiredArgsConstructor
class MCListener implements Listener {
@ -144,8 +145,9 @@ class MCListener implements Listener {
String name = event.getSender() instanceof Player ? ((Player) event.getSender()).getDisplayName()
: event.getSender().getName();
//Channel channel = ChromaGamerBase.getFromSender(event.getSender()).channel().get(); - TODO
DiscordPlugin.mainServer.getEmojis().filter(e -> "YEEHAW".equals(e.getName())).subscribe(yeehaw ->
MCChatUtils.forAllMCChat(MCChatUtils.send(name + (yeehaw != null ? " <:YEEHAW:" + yeehaw.getId().asString() + ">s" : " YEEHAWs"))));
DiscordPlugin.mainServer.getEmojis().filter(e -> "YEEHAW".equals(e.getName()))
.take(1).singleOrEmpty().map(Optional::of).defaultIfEmpty(Optional.empty()).subscribe(yeehaw ->
MCChatUtils.forAllMCChat(MCChatUtils.send(name + (yeehaw.map(guildEmoji -> " <:YEEHAW:" + guildEmoji.getId().asString() + ">s").orElse(" YEEHAWs")))));
}
@EventHandler

View file

@ -1,6 +1,6 @@
package buttondevteam.discordplugin.util;
import buttondevteam.discordplugin.DPUtils;
import buttondevteam.discordplugin.listeners.CommonListeners;
public class Timings {
private long start;
@ -10,7 +10,7 @@ public class Timings {
}
public void printElapsed(String message) {
DPUtils.getLogger().info(message + " (" + (System.nanoTime() - start) / 1000000L + ")");
CommonListeners.debug(message + " (" + (System.nanoTime() - start) / 1000000L + ")");
start = System.nanoTime();
}
}