Search in sources :

Example 1 with CommandContext

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);
    });
}
Also used : Launcher(fredboat.main.Launcher) CentralMessaging(fredboat.messaging.CentralMessaging) Logger(org.slf4j.Logger) TextChannel(net.dv8tion.jda.core.entities.TextChannel) Command(fredboat.commandmeta.abs.Command) ICommandRestricted(fredboat.commandmeta.abs.ICommandRestricted) LoggerFactory(org.slf4j.LoggerFactory) CommandContext(fredboat.commandmeta.abs.CommandContext) PermissionLevel(fredboat.definitions.PermissionLevel) TimeoutException(java.util.concurrent.TimeoutException) TextUtils(fredboat.util.TextUtils) Message(net.dv8tion.jda.core.entities.Message) MessageFormat(java.text.MessageFormat) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) GuildPlayer(fredboat.audio.player.GuildPlayer) Context(fredboat.messaging.internal.Context) Phaser(java.util.concurrent.Phaser) HelpCommand(fredboat.command.info.HelpCommand) Nonnull(javax.annotation.Nonnull) TextChannel(net.dv8tion.jda.core.entities.TextChannel) GuildPlayer(fredboat.audio.player.GuildPlayer) Phaser(java.util.concurrent.Phaser) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with CommandContext

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);
}
Also used : Launcher(fredboat.main.Launcher) JSONObject(org.json.JSONObject) GuildPlayer(fredboat.audio.player.GuildPlayer) Context(fredboat.messaging.internal.Context) Command(fredboat.commandmeta.abs.Command) ICommandRestricted(fredboat.commandmeta.abs.ICommandRestricted) CommandContext(fredboat.commandmeta.abs.CommandContext) PermissionLevel(fredboat.definitions.PermissionLevel) TextUtils(fredboat.util.TextUtils) Nonnull(javax.annotation.Nonnull) JSONArray(org.json.JSONArray) JSONObject(org.json.JSONObject) GuildPlayer(fredboat.audio.player.GuildPlayer) JSONArray(org.json.JSONArray)

Example 3 with CommandContext

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);
}
Also used : IMusicCommand(fredboat.commandmeta.abs.IMusicCommand) Launcher(fredboat.main.Launcher) GuildPlayer(fredboat.audio.player.GuildPlayer) Context(fredboat.messaging.internal.Context) Command(fredboat.commandmeta.abs.Command) MessagingException(fredboat.commandmeta.MessagingException) CommandContext(fredboat.commandmeta.abs.CommandContext) TextUtils(fredboat.util.TextUtils) Nonnull(javax.annotation.Nonnull) Collectors(java.util.stream.Collectors) GuildPlayer(fredboat.audio.player.GuildPlayer) MessagingException(fredboat.commandmeta.MessagingException)

Example 4 with CommandContext

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);
}
Also used : ArgumentUtil(fredboat.util.ArgumentUtil) CentralMessaging(fredboat.messaging.CentralMessaging) Logger(org.slf4j.Logger) Member(net.dv8tion.jda.core.entities.Member) Command(fredboat.commandmeta.abs.Command) LoggerFactory(org.slf4j.LoggerFactory) CommandContext(fredboat.commandmeta.abs.CommandContext) TextUtils(fredboat.util.TextUtils) IModerationCommand(fredboat.commandmeta.abs.IModerationCommand) RestAction(net.dv8tion.jda.core.requests.RestAction) Consumer(java.util.function.Consumer) Guild(net.dv8tion.jda.core.entities.Guild) Permission(net.dv8tion.jda.core.Permission) Context(fredboat.messaging.internal.Context) DiscordUtil(fredboat.util.DiscordUtil) HelpCommand(fredboat.command.info.HelpCommand) Metrics(fredboat.feature.metrics.Metrics) Nonnull(javax.annotation.Nonnull) Guild(net.dv8tion.jda.core.entities.Guild) Member(net.dv8tion.jda.core.entities.Member)

Example 5 with CommandContext

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);
}
Also used : ArgumentUtil(fredboat.util.ArgumentUtil) CentralMessaging(fredboat.messaging.CentralMessaging) Logger(org.slf4j.Logger) Member(net.dv8tion.jda.core.entities.Member) Command(fredboat.commandmeta.abs.Command) LoggerFactory(org.slf4j.LoggerFactory) CommandContext(fredboat.commandmeta.abs.CommandContext) TextUtils(fredboat.util.TextUtils) IModerationCommand(fredboat.commandmeta.abs.IModerationCommand) RestAction(net.dv8tion.jda.core.requests.RestAction) Consumer(java.util.function.Consumer) Guild(net.dv8tion.jda.core.entities.Guild) Permission(net.dv8tion.jda.core.Permission) Context(fredboat.messaging.internal.Context) DiscordUtil(fredboat.util.DiscordUtil) HelpCommand(fredboat.command.info.HelpCommand) Metrics(fredboat.feature.metrics.Metrics) Nonnull(javax.annotation.Nonnull) Guild(net.dv8tion.jda.core.entities.Guild) Member(net.dv8tion.jda.core.entities.Member)

Aggregations

CommandContext (fredboat.commandmeta.abs.CommandContext)12 Command (fredboat.commandmeta.abs.Command)11 Context (fredboat.messaging.internal.Context)10 TextUtils (fredboat.util.TextUtils)10 Nonnull (javax.annotation.Nonnull)10 CentralMessaging (fredboat.messaging.CentralMessaging)7 HelpCommand (fredboat.command.info.HelpCommand)6 ICommandRestricted (fredboat.commandmeta.abs.ICommandRestricted)5 PermissionLevel (fredboat.definitions.PermissionLevel)5 Launcher (fredboat.main.Launcher)4 Collectors (java.util.stream.Collectors)4 Guild (net.dv8tion.jda.core.entities.Guild)4 GuildPlayer (fredboat.audio.player.GuildPlayer)3 IModerationCommand (fredboat.commandmeta.abs.IModerationCommand)3 Module (fredboat.definitions.Module)3 Metrics (fredboat.feature.metrics.Metrics)3 PermsUtil (fredboat.perms.PermsUtil)3 ArgumentUtil (fredboat.util.ArgumentUtil)3 DiscordUtil (fredboat.util.DiscordUtil)3 Consumer (java.util.function.Consumer)3