use of fredboat.messaging.internal.Context 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.messaging.internal.Context in project FredBoat by Frederikam.
the class MusicHelpCommand method getSortedMusicComms.
private static List<String> getSortedMusicComms(Context context) {
List<Command> musicCommands = CommandRegistry.getCommandModule(Module.MUSIC).getDeduplicatedCommands();
// dont explicitly show the youtube and soundcloud commands in this list, since they are just castrated versions
// of the play command, which is "good enough" for this list
musicCommands = musicCommands.stream().filter(command -> !(command instanceof PlayCommand && (command.name.equals(CommandInitializer.YOUTUBE_COMM_NAME) || command.name.equals(CommandInitializer.SOUNDCLOUD_COMM_NAME)))).filter(command -> !(command instanceof DestroyCommand)).collect(Collectors.toList());
musicCommands.sort(new MusicCommandsComparator());
List<String> musicComms = new ArrayList<>();
for (Command command : musicCommands) {
String formattedHelp = HelpCommand.getFormattedCommandHelp(context, command, command.name);
musicComms.add(formattedHelp);
}
return musicComms;
}
use of fredboat.messaging.internal.Context 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.messaging.internal.Context 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.messaging.internal.Context 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);
}
Aggregations