use of ml.duncte123.skybot.objects.command.CommandContext in project SkyBot by DuncteBot.
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 DuncteBot.
the class TokenCommand method execute.
@Override
public void execute(@Nonnull CommandContext ctx) {
final List<String> args = ctx.getArgs();
final Matcher matcher = TOKEN_REGEX.matcher(args.get(0));
if (!matcher.matches()) {
sendMsg(ctx, "Your input `" + args.get(0) + "` has the wrong token format.");
return;
}
final DuncteApis apis = ctx.getVariables().getApis();
final JsonNode json = apis.decodeToken(args.get(0));
if (json.get("success").asBoolean()) {
handleSuccess(args.get(0), json.get("data"), ctx);
return;
}
final JsonNode error = json.get("error");
final String errorType = error.get("type").asText();
final String errorMessage = error.get("message").asText();
sendMsg(ctx, String.format("Invalid token: (%s) %s", errorType, errorMessage));
}
use of ml.duncte123.skybot.objects.command.CommandContext in project SkyBot by DuncteBot.
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 DuncteBot.
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 DuncteBot.
the class PronounsCheckCommand method execute.
@Override
public void execute(@Nonnull CommandContext ctx) {
final User target = getMentionedUser(ctx);
final long userId = target.getIdLong();
final JsonNode json = ctx.getApis().getPronouns(userId);
final boolean isSelf = userId == ctx.getAuthor().getIdLong();
if (json == null) {
sendMsg(ctx, (isSelf ? "You do" : target.getName() + " does") + " not have any pronouns set");
return;
}
final String pronouns = json.get("pronouns").asText();
final String singular = json.get("singular").asBoolean() ? "Singular" : "Plural";
final String userName = isSelf ? "Your" : target.getName() + "'s";
final String format = "%s current pronouns are:%n**%s** (%s)";
sendMsg(ctx, String.format(format, userName, pronouns, singular));
}
Aggregations