use of fredboat.commandmeta.abs.CommandContext in project FredBoat by Frederikam.
the class AnnounceCommand method onInvoke.
@Override
public void onInvoke(@Nonnull CommandContext context) {
List<GuildPlayer> players = Launcher.getBotController().getPlayerRegistry().getPlayingPlayers();
if (players.isEmpty()) {
context.reply("No currently playing players.");
return;
}
if (!context.hasArguments()) {
HelpCommand.sendFormattedCommandHelp(context);
return;
}
String msg = HEAD + context.rawArgs;
context.reply(String.format("[0/%d]", players.size()), // success handler
status -> new Thread(() -> {
Phaser phaser = new Phaser(players.size());
for (GuildPlayer player : players) {
TextChannel activeTextChannel = player.getActiveTextChannel();
if (activeTextChannel != null) {
CentralMessaging.message(activeTextChannel, msg).success(__ -> phaser.arrive()).failure(__ -> phaser.arriveAndDeregister()).send(// this message was not triggered by a user
null);
} else {
phaser.arriveAndDeregister();
}
}
new Thread(() -> {
try {
do {
try {
phaser.awaitAdvanceInterruptibly(0, 5, TimeUnit.SECONDS);
// Now all the parties have arrived, we can break out of the loop
break;
} catch (TimeoutException ex) {
// This is fine, this means that the required parties haven't arrived
}
printProgress(status, phaser.getArrivedParties(), players.size(), players.size() - phaser.getRegisteredParties());
} while (true);
printDone(status, // phaser wraps back to 0 on phase increment
phaser.getRegisteredParties(), players.size() - phaser.getRegisteredParties());
} catch (InterruptedException ex) {
// restore interrupt flag
Thread.currentThread().interrupt();
log.error("interrupted", ex);
throw new RuntimeException(ex);
}
}).start();
}).start(), // failure handler
throwable -> {
log.error("Announcement failed!", throwable);
TextUtils.handleException(throwable, context);
throw new RuntimeException(throwable);
});
}
use of fredboat.commandmeta.abs.CommandContext in project FredBoat by Frederikam.
the class PlayerDebugCommand method onInvoke.
@Override
public void onInvoke(@Nonnull CommandContext context) {
JSONArray a = new JSONArray();
for (GuildPlayer gp : Launcher.getBotController().getPlayerRegistry().getRegistry().values()) {
JSONObject data = new JSONObject();
data.put("name", gp.getGuild().getName());
data.put("id", gp.getGuild().getId());
data.put("users", gp.getCurrentVoiceChannel().getMembers().toString());
data.put("isPlaying", gp.isPlaying());
data.put("isPaused", gp.isPaused());
data.put("songCount", gp.getTrackCount());
a.put(data);
}
TextUtils.postToPasteService(a.toString()).thenApply(pasteUrl -> pasteUrl.orElse("Failed to upload to any pasteservice.")).thenAccept(context::reply);
}
use of fredboat.commandmeta.abs.CommandContext in project FredBoat by Frederikam.
the class ExportCommand method onInvoke.
@Override
public void onInvoke(@Nonnull CommandContext context) {
GuildPlayer player = Launcher.getBotController().getPlayerRegistry().getExisting(context.guild);
if (player == null || player.isQueueEmpty()) {
throw new MessagingException(context.i18n("exportEmpty"));
}
String out = player.getRemainingTracks().stream().map(atc -> atc.getTrack().getInfo().uri).collect(Collectors.joining("\n"));
TextUtils.postToPasteService(out).thenApply(pasteUrl -> {
if (pasteUrl.isPresent()) {
String url = pasteUrl.get() + ".fredboat";
return context.i18nFormat("exportPlaylistResulted", url);
} else {
return context.i18n("exportPlaylistFail") + "\n" + context.i18n("tryLater");
}
}).thenAccept(context::reply);
}
use of fredboat.commandmeta.abs.CommandContext in project FredBoat by Frederikam.
the class HardbanCommand method onInvoke.
@Override
public void onInvoke(@Nonnull CommandContext context) {
Guild guild = context.guild;
// Ensure we have a search term
if (!context.hasArguments()) {
HelpCommand.sendFormattedCommandHelp(context);
return;
}
// was there a target provided?
Member target = ArgumentUtil.checkSingleFuzzyMemberSearchResult(context, context.args[0]);
if (target == null)
return;
// are we allowed to do that?
if (!checkHardBanAuthorization(context, target))
return;
// putting together a reason
String plainReason = DiscordUtil.getReasonForModAction(context);
String auditLogReason = DiscordUtil.formatReasonForAuditLog(plainReason, context.invoker);
// putting together the action
RestAction<Void> modAction = guild.getController().ban(target, 7, auditLogReason);
// on success
String successOutput = context.i18nFormat("hardbanSuccess", TextUtils.escapeAndDefuse(target.getUser().getName()), target.getUser().getDiscriminator(), target.getUser().getId()) + "\n" + plainReason;
Consumer<Void> onSuccess = aVoid -> {
Metrics.successfulRestActions.labels("ban").inc();
context.replyWithName(successOutput);
};
// on fail
String failOutput = context.i18nFormat("modBanFail", target.getUser());
Consumer<Throwable> onFail = t -> {
CentralMessaging.getJdaRestActionFailureHandler(String.format("Failed to ban user %s in guild %s", target.getUser().getId(), guild.getId())).accept(t);
context.replyWithName(failOutput);
};
// issue the mod action
modAction.queue(onSuccess, onFail);
}
use of fredboat.commandmeta.abs.CommandContext in project FredBoat by Frederikam.
the class KickCommand method onInvoke.
@Override
public void onInvoke(@Nonnull CommandContext context) {
Guild guild = context.guild;
// Ensure we have a search term
if (!context.hasArguments()) {
HelpCommand.sendFormattedCommandHelp(context);
return;
}
// was there a target provided?
Member target = ArgumentUtil.checkSingleFuzzyMemberSearchResult(context, context.args[0]);
if (target == null)
return;
// are we allowed to do that?
if (!checkKickAuthorization(context, target))
return;
// putting together a reason
String plainReason = DiscordUtil.getReasonForModAction(context);
String auditLogReason = DiscordUtil.formatReasonForAuditLog(plainReason, context.invoker);
// putting together the action
RestAction<Void> modAction = guild.getController().kick(target, auditLogReason);
// on success
String successOutput = context.i18nFormat("kickSuccess", TextUtils.escapeAndDefuse(target.getUser().getName()), target.getUser().getDiscriminator(), target.getUser().getId()) + "\n" + plainReason;
Consumer<Void> onSuccess = aVoid -> {
Metrics.successfulRestActions.labels("kick").inc();
context.replyWithName(successOutput);
};
// on fail
String failOutput = context.i18nFormat("kickFail", target.getUser());
Consumer<Throwable> onFail = t -> {
CentralMessaging.getJdaRestActionFailureHandler(String.format("Failed to kick user %s in guild %s", target.getUser().getId(), guild.getId())).accept(t);
context.replyWithName(failOutput);
};
// issue the mod action
modAction.queue(onSuccess, onFail);
}
Aggregations