Skip to content

Commit

Permalink
fixes for multibot manager and event handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jagrosh committed Nov 7, 2020
1 parent c090ce5 commit b1d0750
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
6 changes: 2 additions & 4 deletions src/main/java/com/jagrosh/vortex/commands/owner/DebugCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@
import com.jagrosh.vortex.Vortex;
import com.jagrosh.vortex.utils.FormatUtil;
import java.time.temporal.ChronoUnit;
import java.util.stream.Stream;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.sharding.ShardManager;

/**
*
Expand All @@ -52,12 +50,12 @@ protected void execute(CommandEvent event)
long usedMb = (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())/(1024*1024);
StringBuilder sb = new StringBuilder("**"+event.getSelfUser().getName()+"** statistics:"
+ "\nLast Startup: "+FormatUtil.secondsToTime(Constants.STARTUP.until(OffsetDateTime.now(), ChronoUnit.SECONDS))+" ago"
+ "\nMemory: **"+usedMb+"**Mb / **"+totalMb+"**Mb");
+ "\nMemory: **"+usedMb+"**Mb / **"+totalMb+"**Mb\n");
vortex.getShardManager().getShardManagers().forEach(bot ->
//Stream.of(vortex.getShardManager()).forEach(bot ->
{
User self = bot.getShards().get(0).getSelfUser();
sb.append("\n\n__**").append(self.getName()).append("** (").append(self.getId()).append(")__")
sb.append("\n__**").append(self.getName()).append("** (").append(self.getId()).append(")__")
.append("\nGuilds: **").append(bot.getGuildCache().size()).append("**")
.append("\nAverage Ping: **").append(bot.getAverageGatewayPing()).append("**ms")
.append("\nShard Total: **").append(bot.getShardsTotal()).append("**")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
*/
package com.jagrosh.vortex.utils;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.events.GenericEvent;
import net.dv8tion.jda.api.events.guild.GenericGuildEvent;
import net.dv8tion.jda.api.hooks.InterfacedEventManager;
import net.dv8tion.jda.api.sharding.ShardManager;

Expand All @@ -33,17 +34,43 @@ public abstract class ConditionalEventManager extends InterfacedEventManager
public void handle(GenericEvent ge)
{
// check if this guild is already loaded by some other shard
if(ge instanceof GenericGuildEvent)
long selfId;
try
{
selfId = ge.getJDA().getSelfUser().getIdLong();
}
catch ( IllegalStateException ex) // selfid not ready yet
{
return;
}
Guild guild;
try
{
guild = (Guild) ge.getClass().getMethod("getGuild").invoke(ge);
}
catch (NoSuchMethodException | InvocationTargetException ex) // no getGuild method or not in guild
{
guild = null;
}
catch (IllegalAccessException ex) // something actually went wrong
{
guild = null;
ex.printStackTrace();
}
long guildId = guild == null ? 0 : guild.getIdLong();

if(guildId != 0)
{
long selfId = ge.getJDA().getSelfUser().getIdLong();
long gid = ((GenericGuildEvent) ge).getGuild().getIdLong();

for(ShardManager bot: getOrderedShardManagers())
{
if(bot.getShards().get(0).getSelfUser().getIdLong() == selfId)
{
break;
if(bot.getGuildById(gid) != null)
}
if(bot.getGuildById(guildId) != null)
{
return;
}
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/jagrosh/vortex/utils/MultiBotManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,14 @@
*/
public class MultiBotManager
{
private final MultiBotEventManager mbem;
private final List<ShardManager> bots;

protected MultiBotManager(List<DefaultShardManagerBuilder> builders) throws LoginException, IllegalArgumentException
{
this.mbem = new MultiBotEventManager();
this.bots = new ArrayList<>();
for(DefaultShardManagerBuilder b: builders)
{
b.setEventManagerProvider(i -> mbem);
b.setEventManagerProvider(i -> new MultiBotEventManager());
b.setBulkDeleteSplittingEnabled(false);
b.setRequestTimeoutRetry(true);
bots.add(b.build());
Expand Down

0 comments on commit b1d0750

Please sign in to comment.