Added timings and a line that solves everything

cb.setStoreService(new JdkStoreService());
This commit is contained in:
Norbi Peti 2019-05-30 18:45:44 +02:00
parent bf538d9bd0
commit 545b8130e0
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
7 changed files with 58 additions and 39 deletions

View file

@ -187,7 +187,7 @@
<dependency>
<groupId>com.discord4j</groupId>
<artifactId>discord4j-core</artifactId>
<version>3.0.5</version>
<version>3.0.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-jdk14 -->
<dependency>

View file

@ -12,6 +12,7 @@ import buttondevteam.discordplugin.mcchat.MCChatUtils;
import buttondevteam.discordplugin.mcchat.MinecraftChatModule;
import buttondevteam.discordplugin.mccommands.DiscordMCCommand;
import buttondevteam.discordplugin.role.GameRoleModule;
import buttondevteam.discordplugin.util.Timings;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.architecture.ButtonPlugin;
import buttondevteam.lib.architecture.Component;
@ -29,6 +30,7 @@ import discord4j.core.object.presence.Activity;
import discord4j.core.object.presence.Presence;
import discord4j.core.object.reaction.ReactionEmoji;
import discord4j.core.object.util.Snowflake;
import discord4j.store.jdk.JdkStoreService;
import lombok.Getter;
import lombok.val;
import net.milkbowl.vault.permission.Permission;
@ -108,6 +110,7 @@ public class DiscordPlugin extends ButtonPlugin {
}
val cb = new DiscordClientBuilder(token);
cb.setInitialPresence(Presence.doNotDisturb(Activity.playing("booting")));
cb.setStoreService(new JdkStoreService()); //The default doesn't work for some reason - it's waaay faster now
dc = cb.build();
dc.getEventDispatcher().on(ReadyEvent.class) // Listen for ReadyEvent(s)
.map(event -> event.getGuilds().size()) // Get how many guilds the bot is in
@ -138,11 +141,6 @@ public class DiscordPlugin extends ButtonPlugin {
getLogger().warning("Main server set to first one: " + mainServer.getName());
mainServer().set(mainServer); //Save in config
}
if (!TBMCCoreAPI.IsTestServer()) { //Don't change conditions here, see mainServer=devServer=null in onDisable()
dc.updatePresence(Presence.online(Activity.playing("Minecraft"))).subscribe();
} else {
dc.updatePresence(Presence.online(Activity.playing("testing"))).subscribe();
}
SafeMode = false;
DPUtils.disableIfConfigError(null, commandChannel(), modRole()); //Won't disable, just prints the warning here
@ -196,6 +194,11 @@ public class DiscordPlugin extends ButtonPlugin {
setupProviders();
IHaveConfig.pregenConfig(this, null);
if (!TBMCCoreAPI.IsTestServer()) {
dc.updatePresence(Presence.online(Activity.playing("Minecraft"))).subscribe();
} else {
dc.updatePresence(Presence.online(Activity.playing("testing"))).subscribe();
}
} catch (Exception e) {
TBMCCoreAPI.SendException("An error occurred while enabling DiscordPlugin!", e);
}
@ -209,9 +212,10 @@ public class DiscordPlugin extends ButtonPlugin {
@Override
public void pluginPreDisable() {
if (ChromaBot.getInstance() == null) return; //Failed to load
System.out.println("Disable start");
Timings timings = new Timings();
timings.printElapsed("Disable start");
MCChatUtils.forCustomAndAllMCChat(chan -> chan.flatMap(ch -> ch.createEmbed(ecs -> {
System.out.println("Sending message to " + ch.getMention());
timings.printElapsed("Sending message to " + ch.getMention());
if (DiscordMCCommand.resetting)
ecs.setColor(Color.ORANGE).setTitle("Discord plugin restarting");
else
@ -226,16 +230,17 @@ public class DiscordPlugin extends ButtonPlugin {
+ "kicked the hell out.") //TODO: Make configurable
: ""); //If 'restart' is disabled then this isn't shown even if joinleave is enabled
})).subscribe(), ChannelconBroadcast.RESTART, false);
System.out.println("Updating player list");
timings.printElapsed("Updating player list");
ChromaBot.getInstance().updatePlayerList();
System.out.println("Done");
timings.printElapsed("Done");
}
@Override
public void pluginDisable() {
System.out.println("Actual disable start (logout)");
Timings timings = new Timings();
timings.printElapsed("Actual disable start (logout)");
MCChatPrivate.logoutAll();
System.out.println("Config setup");
timings.printElapsed("Config setup");
getConfig().set("serverup", false);
if (ChromaBot.getInstance() == null) return; //Failed to load
@ -243,9 +248,9 @@ public class DiscordPlugin extends ButtonPlugin {
try {
SafeMode = true; // Stop interacting with Discord
ChromaBot.delete();
System.out.println("Updating presence...");
timings.printElapsed("Updating presence...");
dc.updatePresence(Presence.idle(Activity.playing("Chromacraft"))).block(); //No longer using the same account for testing
System.out.println("Logging out...");
timings.printElapsed("Logging out...");
dc.logout().block();
//Configs are emptied so channels and servers are fetched again
} catch (Exception e) {

View file

@ -3,6 +3,7 @@ package buttondevteam.discordplugin.listeners;
import buttondevteam.discordplugin.DPUtils;
import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.discordplugin.commands.Command2DCSender;
import buttondevteam.discordplugin.util.Timings;
import buttondevteam.lib.TBMCCoreAPI;
import discord4j.core.object.entity.Message;
import discord4j.core.object.entity.MessageChannel;
@ -22,44 +23,44 @@ public class CommandListener {
* @return Whether it <b>did not run</b> the command
*/
public static Mono<Boolean> runCommand(Message message, MessageChannel commandChannel, boolean mentionedonly) {
Timings timings = CommonListeners.timings;
Mono<Boolean> ret = Mono.just(true);
if (!message.getContent().isPresent())
return ret; //Pin messages and such, let the mcchat listener deal with it
val content = message.getContent().get();
System.out.println("A");
timings.printElapsed("A");
return message.getChannel().flatMap(channel -> {
Mono<?> tmp = ret;
if (!mentionedonly) { //mentionedonly conditions are in CommonListeners
System.out.println("B");
//System.out.println("Channel type: " + commandChannel.getClass().getSimpleName());
//System.out.println("Guild: " + ((TextChannel) commandChannel).getGuildId());
timings.printElapsed("B");
if (!(channel instanceof PrivateChannel)
&& !(content.charAt(0) == DiscordPlugin.getPrefix()
&& channel.getId().asLong() == commandChannel.getId().asLong())) //
return ret;
System.out.println("C");
timings.printElapsed("C");
tmp = ret.then(channel.type()).thenReturn(true); // Fun (this true is ignored - x)
}
final StringBuilder cmdwithargs = new StringBuilder(content);
val gotmention = new AtomicBoolean();
timings.printElapsed("Before self");
return tmp.flatMapMany(x ->
DiscordPlugin.dc.getSelf().flatMap(self -> self.asMember(DiscordPlugin.mainServer.getId()))
.flatMapMany(self -> {
System.out.println("D");
timings.printElapsed("D");
gotmention.set(checkanddeletemention(cmdwithargs, self.getMention(), message));
gotmention.set(checkanddeletemention(cmdwithargs, self.getNicknameMention(), message) || gotmention.get());
val mentions = message.getRoleMentions();
return self.getRoles().filterWhen(r -> mentions.any(rr -> rr.getName().equals(r.getName())))
.map(Role::getMention);
}).map(mentionRole -> {
System.out.println("E");
timings.printElapsed("E");
gotmention.set(checkanddeletemention(cmdwithargs, mentionRole, message) || gotmention.get()); // Delete all mentions
return !mentionedonly || gotmention.get(); //Stops here if false
}).switchIfEmpty(Mono.fromSupplier(() -> !mentionedonly || gotmention.get())))
.filter(b -> b).last(false).filter(b -> b).doOnNext(b -> channel.type().subscribe()).flatMap(b -> {
String cmdwithargsString = cmdwithargs.toString();
try {
System.out.println("F");
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);
} catch (Exception e) {

View file

@ -5,6 +5,7 @@ import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.discordplugin.fun.FunModule;
import buttondevteam.discordplugin.mcchat.MinecraftChatModule;
import buttondevteam.discordplugin.role.GameRoleModule;
import buttondevteam.discordplugin.util.Timings;
import buttondevteam.lib.TBMCCoreAPI;
import buttondevteam.lib.architecture.Component;
import discord4j.core.event.EventDispatcher;
@ -19,6 +20,8 @@ import reactor.core.publisher.Mono;
public class CommonListeners {
public static final Timings timings = new Timings();
/*
MentionEvent:
- CommandListener (starts with mention, only 'channelcon' and not in #bot)
@ -30,14 +33,13 @@ public class CommonListeners {
*/
public static void register(EventDispatcher dispatcher) {
dispatcher.on(MessageCreateEvent.class).flatMap(event -> {
timings.printElapsed("Message received");
val def = Mono.empty();
if (DiscordPlugin.SafeMode)
return def;
val author = event.getMessage().getAuthor();
if (!author.isPresent() || author.get().isBot())
return def;
//System.out.println("Author: "+author.get());
//System.out.println("Bot: "+author.get().isBot());
if (FunModule.executeMemes(event.getMessage()))
return def;
val commandChannel = DiscordPlugin.plugin.commandChannel().get();
@ -47,25 +49,20 @@ public class CommonListeners {
|| 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
System.out.println("Run command 1");
timings.printElapsed("Run command 1");
return CommandListener.runCommand(event.getMessage(), ch, true); //#bot is handled here
}).filterWhen(ch -> {
System.out.println("mcchat");
timings.printElapsed("mcchat");
val mcchat = Component.getComponents().get(MinecraftChatModule.class);
if (mcchat != null && mcchat.isEnabled()) //ComponentManager.isEnabled() searches the component again
return ((MinecraftChatModule) mcchat).getListener().handleDiscord(event); //Also runs Discord commands in chat channels
return Mono.empty(); //Wasn't handled, continue
}).filterWhen(ch -> {
System.out.println("Run command 2");
timings.printElapsed("Run command 2");
return CommandListener.runCommand(event.getMessage(), ch, false);
});
}).onErrorContinue((err, obj) -> TBMCCoreAPI.SendException("An error occured while handling a message!", err))
.subscribe();
/*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;

View file

@ -8,7 +8,9 @@ import buttondevteam.discordplugin.DiscordPlugin;
import buttondevteam.discordplugin.DiscordSender;
import buttondevteam.discordplugin.DiscordSenderBase;
import buttondevteam.discordplugin.listeners.CommandListener;
import buttondevteam.discordplugin.listeners.CommonListeners;
import buttondevteam.discordplugin.playerfaker.VanillaCommandListener;
import buttondevteam.discordplugin.util.Timings;
import buttondevteam.lib.*;
import buttondevteam.lib.chat.ChatMessage;
import buttondevteam.lib.chat.TBMCChatAPI;
@ -222,17 +224,18 @@ public class MCChatListener implements Listener {
val ret = Mono.just(true);
if (!ComponentManager.isEnabled(MinecraftChatModule.class))
return ret;
System.out.println("Chat event");
Timings timings = CommonListeners.timings;
timings.printElapsed("Chat event");
val author = ev.getMessage().getAuthor();
final boolean hasCustomChat = MCChatCustom.hasCustomChat(ev.getMessage().getChannelId());
return ev.getMessage().getChannel().filter(channel -> {
System.out.println("Filter 1");
timings.printElapsed("Filter 1");
return !(ev.getMessage().getChannelId().asLong() != module.chatChannel().get().asLong()
&& !(channel instanceof PrivateChannel
&& author.map(u -> MCChatPrivate.isMinecraftChatEnabled(u.getId().asString())).orElse(false)
&& !hasCustomChat)); //Chat isn't enabled on this channel
}).filter(channel -> {
System.out.println("Filter 2");
timings.printElapsed("Filter 2");
return !(channel instanceof PrivateChannel //Only in private chat
&& ev.getMessage().getContent().isPresent()
&& ev.getMessage().getContent().get().length() < "/mcchat<>".length()
@ -244,7 +247,7 @@ public class MCChatListener implements Listener {
.filter(channel -> {
MCChatUtils.resetLastMessage(channel);
recevents.add(ev);
System.out.println("Message event added");
timings.printElapsed("Message event added");
if (rectask != null)
return true;
recrun = () -> { //Don't return in a while loop next time

View file

@ -218,17 +218,14 @@ public class MCChatUtils {
*/
public static void resetLastMessage(Channel channel) {
if (notEnabled()) return;
System.out.println("Reset last message");
if (channel.getId().asLong() == module.chatChannel().get().asLong()) {
(lastmsgdata == null ? lastmsgdata = new LastMsgData(module.chatChannelMono().block(), null)
: lastmsgdata).message = null;
System.out.println("Reset done: public chat");
return;
} // Don't set the whole object to null, the player and channel information should be preserved
for (LastMsgData data : channel instanceof PrivateChannel ? MCChatPrivate.lastmsgPerUser : MCChatCustom.lastmsgCustom) {
if (data.channel.getId().asLong() == channel.getId().asLong()) {
data.message = null;
System.out.println("Reset done: private/custom chat");
return;
}
}

View file

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