Search in sources :

Example 1 with MessagingException

use of fredboat.commandmeta.MessagingException 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 2 with MessagingException

use of fredboat.commandmeta.MessagingException in project FredBoat by Frederikam.

the class VolumeCommand method onInvoke.

@Override
public void onInvoke(@Nonnull CommandContext context) {
    if (Launcher.getBotController().getAppConfig().getDistribution().volumeSupported()) {
        GuildPlayer player = Launcher.getBotController().getPlayerRegistry().getOrCreate(context.guild);
        try {
            float volume = Float.parseFloat(context.args[0]) / 100;
            volume = Math.max(0, Math.min(1.5f, volume));
            context.reply(context.i18nFormat("volumeSuccess", Math.floor(player.getVolume() * 100), Math.floor(volume * 100)));
            player.setVolume(volume);
        } catch (NumberFormatException | ArrayIndexOutOfBoundsException ex) {
            throw new MessagingException(context.i18nFormat("volumeSyntax", 100 * PlayerRegistry.DEFAULT_VOLUME, Math.floor(player.getVolume() * 100)));
        }
    } else {
        String out = context.i18n("volumeApology") + "\n<" + BotConstants.DOCS_DONATE_URL + ">";
        context.replyImage("https://fred.moe/1vD.png", out, msg -> CentralMessaging.restService.schedule(() -> CentralMessaging.deleteMessage(msg), 2, TimeUnit.MINUTES));
    }
}
Also used : GuildPlayer(fredboat.audio.player.GuildPlayer) MessagingException(fredboat.commandmeta.MessagingException)

Example 3 with MessagingException

use of fredboat.commandmeta.MessagingException in project FredBoat by Frederikam.

the class SetAvatarCommand method onInvoke.

@Override
public void onInvoke(@Nonnull CommandContext context) {
    URI imageUrl = null;
    try {
        if (!context.msg.getAttachments().isEmpty()) {
            Attachment attachment = context.msg.getAttachments().get(0);
            imageUrl = new URI(attachment.getUrl());
        } else if (context.hasArguments()) {
            imageUrl = AVATARS.containsKey(context.args[0]) ? AVATARS.get(context.args[0]) : new URI(context.args[0]);
        }
    } catch (URISyntaxException e) {
        context.reply("Not a valid link.");
        return;
    }
    if (imageUrl != null && imageUrl.getScheme() != null) {
        if (imageUrl.getScheme().equals("branding")) {
            // for branding:resource.png URLs
            // clear off the 'scheme'
            imageUrl = URI.create(imageUrl.getSchemeSpecificPart());
            if (!checkRelativeToBrandingDir(imageUrl)) {
                throw new MessagingException("url is not relative");
            }
            imageUrl = BRANDING_DIR.resolve(imageUrl);
        }
        Icon avatar;
        switch(imageUrl.getScheme()) {
            case "http":
            case "https":
                avatar = fetchRemote(imageUrl);
                break;
            case "file":
                avatar = fetchFile(imageUrl);
                break;
            default:
                throw new MessagingException("Not a readable image");
        }
        setBotAvatar(context, avatar);
        return;
    }
    // if not handled it's not a proper invocation
    HelpCommand.sendFormattedCommandHelp(context);
}
Also used : MessagingException(fredboat.commandmeta.MessagingException) Attachment(net.dv8tion.jda.core.entities.Message.Attachment) URISyntaxException(java.net.URISyntaxException) Icon(net.dv8tion.jda.core.entities.Icon) URI(java.net.URI)

Example 4 with MessagingException

use of fredboat.commandmeta.MessagingException in project FredBoat by Frederikam.

the class GuildPlayer method joinChannel.

public void joinChannel(VoiceChannel targetChannel) throws MessagingException {
    if (targetChannel == null) {
        throw new MessagingException(I18n.get(getGuild()).getString("playerUserNotInChannel"));
    }
    if (targetChannel.equals(getCurrentVoiceChannel())) {
        // already connected to the channel
        return;
    }
    Guild guild = targetChannel.getGuild();
    if (!guild.getSelfMember().hasPermission(targetChannel, Permission.VOICE_CONNECT) && !targetChannel.getMembers().contains(guild.getSelfMember())) {
        throw new MessagingException(I18n.get(getGuild()).getString("playerJoinConnectDenied"));
    }
    if (!guild.getSelfMember().hasPermission(targetChannel, Permission.VOICE_SPEAK)) {
        throw new MessagingException(I18n.get(getGuild()).getString("playerJoinSpeakDenied"));
    }
    if (targetChannel.getUserLimit() > 0 && targetChannel.getUserLimit() <= targetChannel.getMembers().size() && !guild.getSelfMember().hasPermission(Permission.VOICE_MOVE_OTHERS)) {
        throw new MessagingException(String.format("The channel you want me to join is full!" + // todo i18n
        " Please free up some space, or give me the permission to **%s** to bypass the limit.", Permission.VOICE_MOVE_OTHERS.getName()));
    }
    try {
        audioConnectionFacade.openConnection(targetChannel, this);
        log.info("Connected to voice channel " + targetChannel);
    } catch (Exception e) {
        log.error("Failed to join voice channel {}", targetChannel, e);
    }
}
Also used : MessagingException(fredboat.commandmeta.MessagingException) Guild(net.dv8tion.jda.core.entities.Guild) MessagingException(fredboat.commandmeta.MessagingException)

Aggregations

MessagingException (fredboat.commandmeta.MessagingException)4 GuildPlayer (fredboat.audio.player.GuildPlayer)2 Command (fredboat.commandmeta.abs.Command)1 CommandContext (fredboat.commandmeta.abs.CommandContext)1 IMusicCommand (fredboat.commandmeta.abs.IMusicCommand)1 Launcher (fredboat.main.Launcher)1 Context (fredboat.messaging.internal.Context)1 TextUtils (fredboat.util.TextUtils)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Collectors (java.util.stream.Collectors)1 Nonnull (javax.annotation.Nonnull)1 Guild (net.dv8tion.jda.core.entities.Guild)1 Icon (net.dv8tion.jda.core.entities.Icon)1 Attachment (net.dv8tion.jda.core.entities.Message.Attachment)1