Search in sources :

Example 1 with CommandContext

use of toby.command.CommandContext in project toby-bot by ml404.

the class SetVolumeCommand method setNewVolume.

private void setNewVolume(CommandContext ctx, String prefix, TextChannel channel, Member member, Integer deleteDelay) {
    Guild guild = ctx.getGuild();
    boolean validVolumeArg = ctx.getArgs().get(0).matches("\\d+");
    if (validVolumeArg) {
        int volume = Integer.parseInt(ctx.getArgs().get(0));
        if (PlayerManager.getInstance().isCurrentlyStoppable() || member.hasPermission(Permission.KICK_MEMBERS)) {
            AudioPlayer audioPlayer = PlayerManager.getInstance().getMusicManager(guild).getAudioPlayer();
            if (volume < 1 || volume > 100) {
                channel.sendMessage(getHelp(prefix)).queue(message -> ICommand.deleteAfter(message, deleteDelay));
                return;
            }
            int oldVolume = audioPlayer.getVolume();
            if (volume == oldVolume) {
                channel.sendMessageFormat("New volume and old volume are the same value, somebody shoot %s", member.getEffectiveName()).queue(message -> ICommand.deleteAfter(message, deleteDelay));
                return;
            }
            audioPlayer.setVolume(volume);
            channel.sendMessageFormat("Changing volume from '%s' to '%s' \uD83D\uDD0A", oldVolume, volume).queue(message -> ICommand.deleteAfter(message, deleteDelay));
        } else {
            channel.sendMessageFormat("You aren't allowed to change the volume kid %s", Emotes.TOBY).queue(message -> ICommand.deleteAfter(message, deleteDelay));
        }
    } else
        channel.sendMessage(getHelp(prefix)).queue(message -> ICommand.deleteAfter(message, deleteDelay));
}
Also used : AudioPlayer(com.sedmelluq.discord.lavaplayer.player.AudioPlayer) CommandContext(toby.command.CommandContext) Arrays(java.util.Arrays) Emotes(toby.emote.Emotes) UserDto(toby.jpa.dto.UserDto) Permission(net.dv8tion.jda.api.Permission) Member(net.dv8tion.jda.api.entities.Member) TextChannel(net.dv8tion.jda.api.entities.TextChannel) PlayerManager(toby.lavaplayer.PlayerManager) List(java.util.List) Guild(net.dv8tion.jda.api.entities.Guild) GuildVoiceState(net.dv8tion.jda.api.entities.GuildVoiceState) ICommand(toby.command.ICommand) AudioPlayer(com.sedmelluq.discord.lavaplayer.player.AudioPlayer) Guild(net.dv8tion.jda.api.entities.Guild)

Example 2 with CommandContext

use of toby.command.CommandContext in project toby-bot by ml404.

the class IntroSongCommand method setIntroViaDiscordAttachment.

private void setIntroViaDiscordAttachment(CommandContext ctx, UserDto requestingUserDto, Integer deleteDelay, TextChannel channel, List<Message.Attachment> attachments, int introVolume) {
    Message.Attachment attachment = attachments.stream().findFirst().get();
    if (!Objects.equals(attachment.getFileExtension(), "mp3")) {
        channel.sendMessage("Please use mp3 files only").queue(message -> ICommand.deleteAfter(message, deleteDelay));
        return;
    } else if (attachment.getSize() > 300000) {
        channel.sendMessage("Please keep the file size under 300kb").queue(message -> ICommand.deleteAfter(message, deleteDelay));
    }
    List<Member> mentionedMembers = ctx.getMessage().getMentionedMembers();
    InputStream inputStream = null;
    try {
        inputStream = attachment.retrieveInputStream().get();
    } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }
    if (mentionedMembers.size() > 0 && requestingUserDto.isSuperUser()) {
        InputStream finalInputStream = inputStream;
        mentionedMembers.forEach(member -> {
            UserDto userDto = calculateUserDto(member.getGuild().getIdLong(), member.getIdLong(), member.isOwner(), userService, introVolume);
            persistMusicFile(userDto, deleteDelay, channel, attachment.getFileName(), introVolume, finalInputStream, member.getEffectiveName());
        });
    } else
        persistMusicFile(requestingUserDto, deleteDelay, channel, attachment.getFileName(), introVolume, inputStream, ctx.getAuthor().getName());
}
Also used : Message(net.dv8tion.jda.api.entities.Message) CommandContext(toby.command.CommandContext) IConfigService(toby.jpa.service.IConfigService) Arrays(java.util.Arrays) IMusicFileService(toby.jpa.service.IMusicFileService) UserDto(toby.jpa.dto.UserDto) IOException(java.io.IOException) UserDtoHelper.calculateUserDto(toby.helpers.UserDtoHelper.calculateUserDto) Member(net.dv8tion.jda.api.entities.Member) TextChannel(net.dv8tion.jda.api.entities.TextChannel) URLHelper(toby.helpers.URLHelper) IUserService(toby.jpa.service.IUserService) Collectors(java.util.stream.Collectors) MusicDto(toby.jpa.dto.MusicDto) Objects(java.util.Objects) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) ConfigDto(toby.jpa.dto.ConfigDto) ICommand(toby.command.ICommand) FileUtils.readInputStreamToByteArray(toby.helpers.FileUtils.readInputStreamToByteArray) URI(java.net.URI) InputStream(java.io.InputStream) Message(net.dv8tion.jda.api.entities.Message) InputStream(java.io.InputStream) UserDto(toby.jpa.dto.UserDto) UserDtoHelper.calculateUserDto(toby.helpers.UserDtoHelper.calculateUserDto) ExecutionException(java.util.concurrent.ExecutionException) Member(net.dv8tion.jda.api.entities.Member)

Example 3 with CommandContext

use of toby.command.CommandContext in project toby-bot by ml404.

the class KickCommand method handle.

@Override
public void handle(CommandContext ctx, String prefix, UserDto requestingUserDto, Integer deleteDelay) {
    ICommand.deleteAfter(ctx.getMessage(), deleteDelay);
    final TextChannel channel = ctx.getChannel();
    final Message message = ctx.getMessage();
    final Member member = ctx.getMember();
    final List<String> args = ctx.getArgs();
    if (message.getMentionedMembers().isEmpty()) {
        channel.sendMessage("You must mention 1 or more Users to shoot").queue(message1 -> ICommand.deleteAfter(message1, deleteDelay));
        return;
    }
    message.getMentionedMembers().forEach(target -> {
        if (!member.canInteract(target) || !member.hasPermission(Permission.KICK_MEMBERS)) {
            channel.sendMessage(String.format("You can't shoot %s", target)).queue(message1 -> ICommand.deleteAfter(message1, deleteDelay));
            return;
        }
        final Member botMember = ctx.getSelfMember();
        if (!botMember.canInteract(target) || !botMember.hasPermission(Permission.KICK_MEMBERS)) {
            channel.sendMessage(String.format("I'm not allowed to shoot %s", target)).queue(message1 -> ICommand.deleteAfter(message1, deleteDelay));
            return;
        }
        ctx.getGuild().kick(target).reason("because you told me to.").queue((__) -> channel.sendMessage("Shot hit the mark... something about fortnite?").queue(message1 -> ICommand.deleteAfter(message1, deleteDelay)), (error) -> channel.sendMessageFormat("Could not shoot %s", error.getMessage()).queue(message1 -> ICommand.deleteAfter(message1, deleteDelay)));
    });
}
Also used : Message(net.dv8tion.jda.api.entities.Message) CommandContext(toby.command.CommandContext) Arrays(java.util.Arrays) List(java.util.List) UserDto(toby.jpa.dto.UserDto) ICommand(toby.command.ICommand) Permission(net.dv8tion.jda.api.Permission) Member(net.dv8tion.jda.api.entities.Member) TextChannel(net.dv8tion.jda.api.entities.TextChannel) TextChannel(net.dv8tion.jda.api.entities.TextChannel) Message(net.dv8tion.jda.api.entities.Message) Member(net.dv8tion.jda.api.entities.Member)

Example 4 with CommandContext

use of toby.command.CommandContext in project toby-bot by ml404.

the class SocialCreditCommand method handle.

@Override
public void handle(CommandContext ctx, String prefix, UserDto requestingUserDto, Integer deleteDelay) {
    ICommand.deleteAfter(ctx.getMessage(), deleteDelay);
    List<String> args = ctx.getArgs();
    TextChannel channel = ctx.getChannel();
    Message message = ctx.getMessage();
    final Member member = ctx.getMember();
    if (!ctx.getGuild().isLoaded())
        ctx.getGuild().loadMembers();
    if (args.contains("leaderboard")) {
        Map<Long, Long> discordSocialCreditMap = new HashMap<>();
        userService.listGuildUsers(ctx.getGuild().getIdLong()).forEach(userDto -> {
            Long socialCredit = userDto.getSocialCredit() == null ? 0L : userDto.getSocialCredit();
            discordSocialCreditMap.put(userDto.getDiscordId(), socialCredit);
        });
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("**Social Credit Leaderboard**\n");
        stringBuilder.append("**-----------------------------**\n");
        LinkedHashMap<Long, Long> sortedMap = discordSocialCreditMap.entrySet().stream().sorted(comparingByValue(Comparator.reverseOrder())).collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2, LinkedHashMap::new));
        AtomicInteger position = new AtomicInteger();
        sortedMap.forEach((k, v) -> {
            position.getAndIncrement();
            ctx.getGuild().getMembers().stream().filter(member1 -> member1.getIdLong() == k).findFirst().ifPresent(memberById -> stringBuilder.append(String.format("#%s: %s - score: %d\n", position, memberById.getEffectiveName(), v)));
        });
        channel.sendMessageFormat(stringBuilder.toString()).queue(message1 -> ICommand.deleteAfter(message1, deleteDelay));
    } else {
        List<Member> mentionedMembers = message.getMentionedMembers();
        if (mentionedMembers.isEmpty()) {
            listSocialCreditScore(requestingUserDto, member.getEffectiveName(), deleteDelay, channel);
        } else
            mentionedMembers.forEach(targetMember -> {
                UserDto targetUserDto = userService.getUserById(targetMember.getIdLong(), targetMember.getGuild().getIdLong());
                if (args.subList(0, args.size()).stream().allMatch(s -> s.matches(Message.MentionType.USER.getPattern().pattern()))) {
                    listSocialCreditScore(targetUserDto, targetMember.getEffectiveName(), deleteDelay, channel);
                } else {
                    // Check to see if the database contained an entry for the user we have made a request against
                    if (targetUserDto != null) {
                        boolean isSameGuild = requestingUserDto.getGuildId().equals(targetUserDto.getGuildId());
                        boolean requesterCanAdjustPermissions = member.isOwner();
                        if (requesterCanAdjustPermissions && isSameGuild) {
                            String socialCreditString = args.subList(0, args.size()).stream().filter(s -> !s.matches(Message.MentionType.USER.getPattern().pattern())).findFirst().get();
                            UserDto updatedUser = validateArgumentsAndAdjustSocialCredit(ctx, targetUserDto, channel, Long.valueOf(socialCreditString), ctx.getMember().isOwner(), deleteDelay);
                            channel.sendMessageFormat("Updated user %s's social credit by %s. New score is: %d", targetMember.getEffectiveName(), socialCreditString, updatedUser.getSocialCredit()).queue(message1 -> ICommand.deleteAfter(message1, deleteDelay));
                        } else
                            channel.sendMessageFormat("User '%s' is not allowed to adjust the social credit of user '%s'.", member.getNickname(), targetMember.getNickname()).queue(message1 -> ICommand.deleteAfter(message1, deleteDelay));
                    }
                }
            });
    }
}
Also used : Message(net.dv8tion.jda.api.entities.Message) CommandContext(toby.command.CommandContext) java.util(java.util) Collectors.toMap(java.util.stream.Collectors.toMap) UserDto(toby.jpa.dto.UserDto) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ICommand(toby.command.ICommand) Entry.comparingByValue(java.util.Map.Entry.comparingByValue) Member(net.dv8tion.jda.api.entities.Member) TextChannel(net.dv8tion.jda.api.entities.TextChannel) IUserService(toby.jpa.service.IUserService) Message(net.dv8tion.jda.api.entities.Message) UserDto(toby.jpa.dto.UserDto) TextChannel(net.dv8tion.jda.api.entities.TextChannel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Member(net.dv8tion.jda.api.entities.Member) Collectors.toMap(java.util.stream.Collectors.toMap)

Example 5 with CommandContext

use of toby.command.CommandContext in project toby-bot by ml404.

the class DbdRandomKillerCommand method handle.

@Override
@SuppressWarnings("unchecked")
public void handle(CommandContext ctx, String prefix, UserDto requestingUserDto, Integer deleteDelay) {
    ICommand.deleteAfter(ctx.getMessage(), deleteDelay);
    final TextChannel channel = ctx.getChannel();
    try {
        WikiFetcher wikiFetcher = new WikiFetcher(cache);
        List<String> dbdKillers = wikiFetcher.fetchFromWiki(cacheName, dbdWebUrl, className, cssQuery);
        channel.sendMessage(RandomCommand.getRandomElement(dbdKillers)).queue(message -> ICommand.deleteAfter(message, deleteDelay));
    } catch (IOException ignored) {
        channel.sendMessage("Huh, the website I pull data from must have returned something unexpected.").queue(message -> ICommand.deleteAfter(message, deleteDelay));
    }
}
Also used : CommandContext(toby.command.CommandContext) WikiFetcher(toby.helpers.WikiFetcher) Arrays(java.util.Arrays) List(java.util.List) UserDto(toby.jpa.dto.UserDto) ICommand(toby.command.ICommand) Cache(toby.helpers.Cache) IOException(java.io.IOException) TextChannel(net.dv8tion.jda.api.entities.TextChannel) RandomCommand(toby.command.commands.misc.RandomCommand) TextChannel(net.dv8tion.jda.api.entities.TextChannel) WikiFetcher(toby.helpers.WikiFetcher) IOException(java.io.IOException)

Aggregations

CommandContext (toby.command.CommandContext)16 ICommand (toby.command.ICommand)16 UserDto (toby.jpa.dto.UserDto)16 List (java.util.List)14 TextChannel (net.dv8tion.jda.api.entities.TextChannel)14 Arrays (java.util.Arrays)10 Collectors (java.util.stream.Collectors)7 Message (net.dv8tion.jda.api.entities.Message)7 Member (net.dv8tion.jda.api.entities.Member)6 IOException (java.io.IOException)3 Random (java.util.Random)3 Permission (net.dv8tion.jda.api.Permission)3 IConfigService (toby.jpa.service.IConfigService)3 IUserService (toby.jpa.service.IUserService)3 Map (java.util.Map)2 Objects (java.util.Objects)2 Optional (java.util.Optional)2 RandomCommand (toby.command.commands.misc.RandomCommand)2 Cache (toby.helpers.Cache)2 UserDtoHelper.calculateUserDto (toby.helpers.UserDtoHelper.calculateUserDto)2