use of ml.duncte123.skybot.objects.command.CommandContext in project SkyBot by duncte123.
the class ShardInfoCommand method embedTable.
private void embedTable(CommandContext ctx) {
final EmbedBuilder embedBuilder = new EmbedBuilder();
final int currentShard = ctx.getJDA().getShardInfo().getShardId();
final ShardManager shardManager = ctx.getJDA().getShardManager();
final ShardCacheView shardCache = shardManager.getShardCache();
final List<JDA> shards = new ArrayList<>(shardCache.asList());
Collections.reverse(shards);
for (final JDA shard : shards) {
final StringBuilder valueBuilder = new StringBuilder();
final LongLongPair channelStats = getConnectedVoiceChannels(shard);
valueBuilder.append("**Status:** ").append(getShardStatus(shard)).append("\n**Ping:** ").append(shard.getGatewayPing()).append("\n**Guilds:** ").append(shard.getGuildCache().size()).append("\n**VCs:** ").append(channelStats.getFirst()).append(" / ").append(channelStats.getSecond());
final int shardId = shard.getShardInfo().getShardId();
embedBuilder.addField(String.format("Shard #%s%s", shardId, shardId == currentShard ? " (current)" : ""), valueBuilder.toString(), true);
}
final long connectedShards = shardCache.applyStream((s) -> s.filter((shard) -> shard.getStatus() == JDA.Status.CONNECTED).count());
final String avgPing = new DecimalFormat("###").format(shardManager.getAverageGatewayPing());
final long guilds = shardManager.getGuildCache().size();
final LongLongPair channelStats = getConnectedVoiceChannels(shardManager);
embedBuilder.addField("Total/Average", String.format("**Connected:** %s\n**Ping:** %s\n**Guilds:** %s\n**VCs:** %s / %s", connectedShards, avgPing, guilds, channelStats.getFirst(), channelStats.getSecond()), false);
sendEmbed(ctx, embedBuilder);
}
use of ml.duncte123.skybot.objects.command.CommandContext in project SkyBot by duncte123.
the class ShardInfoCommand method asciiInfo.
private void asciiInfo(CommandContext ctx) {
final List<String> headers = new ArrayList<>();
headers.add("ID");
headers.add("Status");
headers.add("Ping");
headers.add("Guilds");
headers.add("VCs");
List<List<String>> table = new ArrayList<>();
final int currentShard = ctx.getJDA().getShardInfo().getShardId();
final ShardManager shardManager = ctx.getJDA().getShardManager();
final List<JDA> shards = new ArrayList<>(shardManager.getShards());
Collections.reverse(shards);
for (final JDA shard : shards) {
final List<String> row = new ArrayList<>();
final int shardId = shard.getShardInfo().getShardId();
row.add(shardId + (currentShard == shardId ? " (current)" : ""));
row.add(getShardStatus(shard));
row.add(String.valueOf(shard.getGatewayPing()));
row.add(String.valueOf(shard.getGuildCache().size()));
final LongLongPair channelStats = getConnectedVoiceChannels(shard);
row.add(channelStats.getFirst() + " / " + channelStats.getSecond());
table.add(row);
if (table.size() == 20) {
MessageUtils.sendMsg(ctx, makeAsciiTable(headers, table, shardManager));
table = new ArrayList<>();
}
}
if (!table.isEmpty()) {
MessageUtils.sendMsg(ctx, makeAsciiTable(headers, table, shardManager));
}
}
use of ml.duncte123.skybot.objects.command.CommandContext in project SkyBot by duncte123.
the class MessageListener method handleMessageEventChecked.
private void handleMessageEventChecked(String raw, Guild guild, GuildMessageReceivedEvent event) {
final GuildSetting settings = GuildSettingsUtils.getGuild(guild.getIdLong(), this.variables);
final String customPrefix = settings.getCustomPrefix();
final Message message = event.getMessage();
if (settings.isMessageLogging()) {
final MessageData data = MessageData.from(message);
this.redis.storeMessage(data, isGuildPatron(guild));
}
if (!commandManager.isCommand(customPrefix, raw) && doAutoModChecks(event, settings, raw)) {
return;
}
final User selfUser = event.getJDA().getSelfUser();
final long selfId = selfUser.getIdLong();
final String selfRegex = "<@!?" + selfId + '>';
if (raw.matches(selfRegex)) {
sendMsg(new MessageConfig.Builder().setChannel(event.getChannel()).setMessageFormat("Hey %s, try `%shelp` for a list of commands. If it doesn't work scream at _duncte123#1245_", event.getAuthor(), customPrefix).build());
return;
}
final String[] split = raw.replaceFirst(Pattern.quote(Settings.PREFIX), "").split("\\s+");
final List<CustomCommand> autoResponses = commandManager.getAutoResponses(guild.getIdLong());
if (!autoResponses.isEmpty() && invokeAutoResponse(autoResponses, split, event)) {
return;
}
if (doesNotStartWithPrefix(selfId, raw, customPrefix) || !canRunCommands(raw, customPrefix, event)) {
return;
}
if (raw.matches(selfRegex + "(.*)")) {
// Handle the chat command
Objects.requireNonNull(commandManager.getCommand("chat")).executeCommand(new CommandContext("chat", Arrays.asList(split).subList(1, split.length), event, variables));
} else {
// Handle the command
commandManager.runCommand(event, customPrefix);
}
}
use of ml.duncte123.skybot.objects.command.CommandContext in project SkyBot by duncte123.
the class PurgeUserCommand method execute.
@Override
public void execute(@Nonnull CommandContext ctx) {
final List<Member> mentionedMembers = ctx.getMentionedArg(0);
if (mentionedMembers.isEmpty()) {
sendMsg(ctx, "I could not find any members with that name");
return;
}
final Member targetMember = mentionedMembers.get(0);
final User targetUser = targetMember.getUser();
final TextChannel channel = ctx.getChannel();
final Message message = ctx.getMessage();
channel.getIterableHistory().takeAsync(DEL_COUNT).thenApplyAsync((msgs) -> msgs.stream().filter((msg) -> msg.getAuthor().equals(targetUser)).collect(Collectors.toList())).thenApplyAsync((msgs) -> {
final List<CompletableFuture<Void>> futures = channel.purgeMessages(msgs);
sendSuccess(message);
try {
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).get();
} catch (InterruptedException | ExecutionException e) {
Sentry.captureException(e);
return 0;
}
return msgs.size();
}).exceptionally((thr) -> {
String cause = "";
if (thr.getCause() != null) {
cause = " caused by: " + thr.getCause().getMessage();
}
sendMsg(ctx, "ERROR: " + thr.getMessage() + cause);
sendError(message);
return 0;
}).whenCompleteAsync((count, thr) -> {
sendMsg(DELETE_MESSAGE_AFTER_SECONDS.apply(5L).setChannel(ctx.getChannel()).setMessageFormat("Removed %d messages! (this message will auto delete in 5 seconds)", count).build());
channel.deleteMessageById(message.getIdLong()).queue(null, ignore(UNKNOWN_MESSAGE));
modLog(String.format("%d messages by %#s removed in %s by %#s", count, targetUser, channel, ctx.getAuthor()), ctx.getGuild());
});
}
use of ml.duncte123.skybot.objects.command.CommandContext in project SkyBot by duncte123.
the class LockEmoteCommand method execute.
@Override
public void execute(@Nonnull CommandContext ctx) {
if (ctx.getArgs().isEmpty()) {
this.sendUsageInstructions(ctx);
return;
}
final Message message = ctx.getMessage();
final List<Emote> mentionedEmotes = message.getEmotes();
final List<Role> mentionedRoles = new ArrayList<>(message.getMentionedRoles());
if (mentionedRoles.isEmpty()) {
// Loop over the args and check if there are roles found in text
ctx.getArgs().forEach((arg) -> mentionedRoles.addAll(FinderUtil.findRoles(arg, ctx.getGuild())));
}
if (mentionedEmotes.isEmpty() || mentionedRoles.isEmpty()) {
this.sendUsageInstructions(ctx);
return;
}
final Emote emote = mentionedEmotes.get(0);
if (cannotInteractWithEmote(ctx, emote)) {
return;
}
emote.getManager().setRoles(new HashSet<>(mentionedRoles)).queue();
sendSuccess(message);
final List<String> roleNames = mentionedRoles.stream().map(Role::getName).collect(Collectors.toList());
sendMsg(ctx, "The emote " + emote.getAsMention() + " has been locked to users that have the " + "following roles: `" + String.join("`, `", roleNames) + "`");
}
Aggregations