Search in sources :

Example 1 with Context

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);
    });
}
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 Context

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;
}
Also used : CommandRegistry(fredboat.commandmeta.CommandRegistry) Launcher(fredboat.main.Launcher) Arrays(java.util.Arrays) TextChannel(net.dv8tion.jda.core.entities.TextChannel) SeekCommand(fredboat.command.music.seeking.SeekCommand) Command(fredboat.commandmeta.abs.Command) CommandContext(fredboat.commandmeta.abs.CommandContext) PermissionLevel(fredboat.definitions.PermissionLevel) RestartCommand(fredboat.command.music.seeking.RestartCommand) TextUtils(fredboat.util.TextUtils) ArrayList(java.util.ArrayList) RewindCommand(fredboat.command.music.seeking.RewindCommand) Permission(net.dv8tion.jda.core.Permission) Module(fredboat.definitions.Module) Nonnull(javax.annotation.Nonnull) fredboat.command.music.control(fredboat.command.music.control) CentralMessaging(fredboat.messaging.CentralMessaging) IInfoCommand(fredboat.commandmeta.abs.IInfoCommand) fredboat.command.music.info(fredboat.command.music.info) Collectors(java.util.stream.Collectors) CommandInitializer(fredboat.commandmeta.CommandInitializer) ForwardCommand(fredboat.command.music.seeking.ForwardCommand) List(java.util.List) Context(fredboat.messaging.internal.Context) Emojis(fredboat.util.Emojis) Comparator(java.util.Comparator) PermsUtil(fredboat.perms.PermsUtil) SeekCommand(fredboat.command.music.seeking.SeekCommand) Command(fredboat.commandmeta.abs.Command) RestartCommand(fredboat.command.music.seeking.RestartCommand) RewindCommand(fredboat.command.music.seeking.RewindCommand) IInfoCommand(fredboat.commandmeta.abs.IInfoCommand) ForwardCommand(fredboat.command.music.seeking.ForwardCommand) ArrayList(java.util.ArrayList)

Example 3 with Context

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);
}
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 4 with Context

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);
}
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 5 with Context

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);
}
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

Command (fredboat.commandmeta.abs.Command)11 CommandContext (fredboat.commandmeta.abs.CommandContext)11 Context (fredboat.messaging.internal.Context)11 TextUtils (fredboat.util.TextUtils)11 Nonnull (javax.annotation.Nonnull)11 CentralMessaging (fredboat.messaging.CentralMessaging)8 PermissionLevel (fredboat.definitions.PermissionLevel)6 HelpCommand (fredboat.command.info.HelpCommand)5 ICommandRestricted (fredboat.commandmeta.abs.ICommandRestricted)5 Launcher (fredboat.main.Launcher)5 Collectors (java.util.stream.Collectors)5 PermsUtil (fredboat.perms.PermsUtil)4 Permission (net.dv8tion.jda.core.Permission)4 Guild (net.dv8tion.jda.core.entities.Guild)4 GuildPlayer (fredboat.audio.player.GuildPlayer)3 CommandInitializer (fredboat.commandmeta.CommandInitializer)3 CommandRegistry (fredboat.commandmeta.CommandRegistry)3 IInfoCommand (fredboat.commandmeta.abs.IInfoCommand)3 IModerationCommand (fredboat.commandmeta.abs.IModerationCommand)3 Module (fredboat.definitions.Module)3