Updated to Discord4J v3, permission injection, improvements #99
6 changed files with 56 additions and 11 deletions
10
pom.xml
10
pom.xml
|
@ -149,6 +149,10 @@
|
|||
<id>pex-repo</id>
|
||||
<url>http://pex-repo.aoeu.xyz</url>
|
||||
</repository> -->
|
||||
<!-- <repository>
|
||||
<id>Reactor-Tools</id>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
</repository> -->
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
|
@ -231,6 +235,12 @@
|
|||
<artifactId>emoji-java</artifactId>
|
||||
<version>4.0.0</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/io.projectreactor.tools/blockhound -->
|
||||
<!-- <dependency>
|
||||
<groupId>io.projectreactor.tools</groupId>
|
||||
<artifactId>blockhound</artifactId>
|
||||
<version>1.0.0.M3</version>
|
||||
</dependency> -->
|
||||
</dependencies>
|
||||
|
||||
<profiles>
|
||||
|
|
|
@ -74,7 +74,7 @@ public final class DPUtils {
|
|||
public static ReadOnlyConfigData<Mono<Role>> roleData(IHaveConfig config, String key, String defName, Mono<Guild> guild) {
|
||||
return config.getReadOnlyDataPrimDef(key, defName, name -> {
|
||||
if (!(name instanceof String)) return Mono.empty();
|
||||
return guild.flatMapMany(Guild::getRoles).filter(r -> r.getName().equals(name)).last();
|
||||
return guild.flatMapMany(Guild::getRoles).filter(r -> r.getName().equals(name)).next();
|
||||
}, r -> defName);
|
||||
}
|
||||
|
||||
|
|
|
@ -160,18 +160,18 @@ public class DiscordPlugin extends ButtonPlugin {
|
|||
getManager().registerCommand(new DebugCommand());
|
||||
getManager().registerCommand(new ConnectCommand());
|
||||
if (DiscordMCCommand.resetting) //These will only execute if the chat is enabled
|
||||
ChromaBot.getInstance().sendMessageCustomAsWell(ch -> ch.createEmbed(ecs -> ecs.setColor(Color.CYAN)
|
||||
.setTitle("Discord plugin restarted - chat connected.")), ChannelconBroadcast.RESTART); //Really important to note the chat, hmm
|
||||
ChromaBot.getInstance().sendMessageCustomAsWell(chan -> chan.flatMap(ch -> ch.createEmbed(ecs -> ecs.setColor(Color.CYAN)
|
||||
.setTitle("Discord plugin restarted - chat connected."))), ChannelconBroadcast.RESTART); //Really important to note the chat, hmm
|
||||
else if (getConfig().getBoolean("serverup", false)) {
|
||||
ChromaBot.getInstance().sendMessageCustomAsWell(ch -> ch.createEmbed(ecs -> ecs.setColor(Color.YELLOW)
|
||||
.setTitle("Server recovered from a crash - chat connected.")), ChannelconBroadcast.RESTART);
|
||||
ChromaBot.getInstance().sendMessageCustomAsWell(chan -> chan.flatMap(ch -> ch.createEmbed(ecs -> ecs.setColor(Color.YELLOW)
|
||||
.setTitle("Server recovered from a crash - chat connected."))), ChannelconBroadcast.RESTART);
|
||||
val thr = new Throwable(
|
||||
"The server shut down unexpectedly. See the log of the previous run for more details.");
|
||||
thr.setStackTrace(new StackTraceElement[0]);
|
||||
TBMCCoreAPI.SendException("The server crashed!", thr);
|
||||
} else
|
||||
ChromaBot.getInstance().sendMessageCustomAsWell(ch -> ch.createEmbed(ecs -> ecs.setColor(Color.GREEN)
|
||||
.setTitle("Server started - chat connected.")), ChannelconBroadcast.RESTART);
|
||||
ChromaBot.getInstance().sendMessageCustomAsWell(chan -> chan.flatMap(ch -> ch.createEmbed(ecs -> ecs.setColor(Color.GREEN)
|
||||
.setTitle("Server started - chat connected."))), ChannelconBroadcast.RESTART);
|
||||
|
||||
DiscordMCCommand.resetting = false; //This is the last event handling this flag
|
||||
|
||||
|
@ -181,7 +181,7 @@ 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 it's name.)"));
|
||||
"The plugin refuses to load until you change the token to a testing account. (The account needs to have \"test\" in its name.)"));
|
||||
Bukkit.getPluginManager().disablePlugin(this);
|
||||
}
|
||||
TBMCCoreAPI.SendUnsentExceptions();
|
||||
|
@ -197,7 +197,7 @@ public class DiscordPlugin extends ButtonPlugin {
|
|||
|
||||
IHaveConfig.pregenConfig(this, null);
|
||||
} catch (Exception e) {
|
||||
TBMCCoreAPI.SendException("An error occured while enabling DiscordPlugin!", e);
|
||||
TBMCCoreAPI.SendException("An error occurred while enabling DiscordPlugin!", e);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -210,7 +210,7 @@ public class DiscordPlugin extends ButtonPlugin {
|
|||
public void pluginPreDisable() {
|
||||
if (ChromaBot.getInstance() == null) return; //Failed to load
|
||||
System.out.println("Disable start");
|
||||
MCChatUtils.forCustomAndAllMCChat(ch -> ch.createEmbed(ecs -> {
|
||||
MCChatUtils.forCustomAndAllMCChat(chan -> chan.flatMap(ch -> ch.createEmbed(ecs -> {
|
||||
System.out.println("Sending message to " + ch.getMention());
|
||||
if (DiscordMCCommand.resetting)
|
||||
ecs.setColor(Color.ORANGE).setTitle("Discord plugin restarting");
|
||||
|
@ -225,7 +225,7 @@ public class DiscordPlugin extends ButtonPlugin {
|
|||
+ (Bukkit.getOnlinePlayers().size() == 1 ? " was " : " were ")
|
||||
+ "kicked the hell out.") //TODO: Make configurable
|
||||
: ""); //If 'restart' is disabled then this isn't shown even if joinleave is enabled
|
||||
}).block(), ChannelconBroadcast.RESTART, false);
|
||||
})).subscribe(), ChannelconBroadcast.RESTART, false);
|
||||
System.out.println("Updating player list");
|
||||
ChromaBot.getInstance().updatePlayerList();
|
||||
System.out.println("Done");
|
||||
|
|
|
@ -21,7 +21,9 @@ public class CommandListener {
|
|||
public static boolean runCommand(Message message, boolean mentionedonly) {
|
||||
if (!message.getContent().isPresent())
|
||||
return false; //Pin messages and such, let the mcchat listener deal with it
|
||||
System.out.println("1");
|
||||
final MessageChannel channel = message.getChannel().block();
|
||||
System.out.println("2");
|
||||
val content = message.getContent().get();
|
||||
if (channel == null) return false;
|
||||
if (!mentionedonly) { //mentionedonly conditions are in CommonListeners
|
||||
|
@ -31,28 +33,36 @@ public class CommandListener {
|
|||
return false;
|
||||
channel.type().subscribe(); // Fun
|
||||
}
|
||||
System.out.println("3");
|
||||
final StringBuilder cmdwithargs = new StringBuilder(content);
|
||||
val self = DiscordPlugin.dc.getSelf().block();
|
||||
System.out.println("4");
|
||||
if (self == null) return false;
|
||||
val member = self.asMember(DiscordPlugin.mainServer.getId()).block();
|
||||
System.out.println("5");
|
||||
if (member == null) return false;
|
||||
final String mention = self.getMention();
|
||||
final String mentionNick = member.getNicknameMention();
|
||||
System.out.println("6");
|
||||
boolean gotmention = checkanddeletemention(cmdwithargs, mention, message);
|
||||
gotmention = checkanddeletemention(cmdwithargs, mentionNick, message) || gotmention;
|
||||
System.out.println("7");
|
||||
val mentions = message.getRoleMentions();
|
||||
for (String mentionRole : member.getRoles().filter(r -> mentions.any(rr -> rr.getName().equals(r.getName())).blockOptional().orElse(false)).map(Role::getMention).toIterable())
|
||||
gotmention = checkanddeletemention(cmdwithargs, mentionRole, message) || gotmention; // Delete all mentions
|
||||
if (mentionedonly && !gotmention)
|
||||
return false;
|
||||
System.out.println("8");
|
||||
channel.type().subscribe();
|
||||
String cmdwithargsString = cmdwithargs.toString();
|
||||
System.out.println("9");
|
||||
try {
|
||||
if (!DiscordPlugin.plugin.getManager().handleCommand(new Command2DCSender(message), cmdwithargsString))
|
||||
DPUtils.reply(message, channel, "Unknown command. Do " + DiscordPlugin.getPrefix() + "help for help.\n" + cmdwithargsString).subscribe();
|
||||
} catch (Exception e) {
|
||||
TBMCCoreAPI.SendException("Failed to process Discord command: " + cmdwithargsString, e);
|
||||
}
|
||||
System.out.println("10");
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -35,25 +35,36 @@ public class CommonListeners {
|
|||
return;
|
||||
//System.out.println("Author: "+author.get());
|
||||
//System.out.println("Bot: "+author.get().isBot());
|
||||
System.out.println("A");
|
||||
if (FunModule.executeMemes(event.getMessage()))
|
||||
return;
|
||||
System.out.println("B");
|
||||
try {
|
||||
boolean handled = false;
|
||||
val commandChannel = DiscordPlugin.plugin.CommandChannel().get();
|
||||
System.out.println("C");
|
||||
if ((commandChannel != null && event.getMessage().getChannelId().asLong() == commandChannel.asLong()) //If mentioned, that's higher than chat
|
||||
|| event.getMessage().getContent().orElse("").contains("channelcon")) //Only 'channelcon' is allowed in other channels
|
||||
handled = CommandListener.runCommand(event.getMessage(), true); //#bot is handled here
|
||||
System.out.println("D");
|
||||
if (handled) return;
|
||||
//System.out.println("Message handling");
|
||||
val mcchat = Component.getComponents().get(MinecraftChatModule.class);
|
||||
if (mcchat != null && mcchat.isEnabled()) //ComponentManager.isEnabled() searches the component again
|
||||
handled = ((MinecraftChatModule) mcchat).getListener().handleDiscord(event); //Also runs Discord commands in chat channels
|
||||
System.out.println("E");
|
||||
if (!handled)
|
||||
handled = CommandListener.runCommand(event.getMessage(), false);
|
||||
System.out.println("F");
|
||||
} catch (Exception e) {
|
||||
TBMCCoreAPI.SendException("An error occured while handling a message!", e);
|
||||
}
|
||||
});
|
||||
/*dispatcher.on(MessageCreateEvent.class).doOnNext(x -> System.out.println("Got message"))
|
||||
.flatMap(MessageCreateEvent::getGuild)
|
||||
.flatMap(guild -> DiscordPlugin.dc.getSelf())
|
||||
.flatMap(self -> self.asMember(DiscordPlugin.mainServer.getId()))
|
||||
.flatMap(Member::getRoles).subscribe(roles -> System.out.println("Roles: " + roles));*/
|
||||
dispatcher.on(PresenceUpdateEvent.class).subscribe(event -> {
|
||||
if (DiscordPlugin.SafeMode)
|
||||
return;
|
||||
|
|
|
@ -220,22 +220,28 @@ public class MCChatListener implements Listener {
|
|||
public boolean handleDiscord(MessageCreateEvent ev) {
|
||||
if (!ComponentManager.isEnabled(MinecraftChatModule.class))
|
||||
return false;
|
||||
System.out.println("Chat event");
|
||||
val author = ev.getMessage().getAuthor();
|
||||
final boolean hasCustomChat = MCChatCustom.hasCustomChat(ev.getMessage().getChannelId());
|
||||
System.out.println("C1");
|
||||
val channel = ev.getMessage().getChannel().block();
|
||||
System.out.println("C2");
|
||||
if (ev.getMessage().getChannelId().asLong() != module.chatChannel().get().asLong()
|
||||
&& !(channel instanceof PrivateChannel
|
||||
&& author.map(u -> MCChatPrivate.isMinecraftChatEnabled(u.getId().asString())).orElse(false)
|
||||
&& !hasCustomChat))
|
||||
return false; //Chat isn't enabled on this channel
|
||||
System.out.println("C3");
|
||||
if (channel instanceof PrivateChannel //Only in private chat
|
||||
&& ev.getMessage().getContent().isPresent()
|
||||
&& ev.getMessage().getContent().get().length() < "/mcchat<>".length()
|
||||
&& ev.getMessage().getContent().get().replace("/", "")
|
||||
.equalsIgnoreCase("mcchat")) //Either mcchat or /mcchat
|
||||
return false; //Allow disabling the chat if needed
|
||||
System.out.println("C4");
|
||||
if (CommandListener.runCommand(ev.getMessage(), true))
|
||||
return true; //Allow running commands in chat channels
|
||||
System.out.println("C5");
|
||||
MCChatUtils.resetLastMessage(channel);
|
||||
//System.out.println("Message: "+ev.getMessage().getAuthor().toString());
|
||||
recevents.add(ev);
|
||||
|
@ -259,12 +265,14 @@ 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();
|
||||
|
@ -276,6 +284,7 @@ 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
|
||||
|
@ -289,7 +298,9 @@ 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)
|
||||
|
@ -365,6 +376,7 @@ 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)
|
||||
|
@ -374,11 +386,13 @@ 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) {
|
||||
|
|
Loading…
Reference in a new issue