Search in sources :

Example 1 with ICommand

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

the class CommandManager method handle.

public void handle(GuildMessageReceivedEvent event) {
    String prefix = configService.getConfigByName(ConfigDto.Configurations.PREFIX.getConfigValue(), event.getGuild().getId()).getValue();
    Integer deleteDelay = Integer.parseInt(configService.getConfigByName(ConfigDto.Configurations.DELETE_DELAY.getConfigValue(), event.getGuild().getId()).getValue());
    String[] split = event.getMessage().getContentRaw().replaceFirst("(?i)" + Pattern.quote(prefix), "").split("\\s+");
    String volumePropertyName = ConfigDto.Configurations.VOLUME.getConfigValue();
    String defaultVolume = configService.getConfigByName(volumePropertyName, event.getGuild().getId()).getValue();
    int introVolume = Integer.parseInt(defaultVolume);
    UserDto requestingUserDto = calculateUserDto(event.getGuild().getIdLong(), event.getAuthor().getIdLong(), Objects.requireNonNull(event.getMember()).isOwner(), userService, introVolume);
    String invoke = split[0].toLowerCase();
    ICommand cmd = this.getCommand(invoke);
    if (cmd != null) {
        event.getChannel().sendTyping().queue();
        List<String> args = Arrays.asList(split).subList(1, split.length);
        CommandContext ctx = new CommandContext(event, args);
        cmd.handle(ctx, prefix, requestingUserDto, deleteDelay);
    } else {
        getCommand("help");
    }
}
Also used : CommandContext(toby.command.CommandContext) ICommand(toby.command.ICommand) UserDto(toby.jpa.dto.UserDto) UserDtoHelper.calculateUserDto(toby.helpers.UserDtoHelper.calculateUserDto)

Example 2 with ICommand

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

the class CommandManagerTest method testCommandManagerFindsAllCommands.

@Test
public void testCommandManagerFindsAllCommands() {
    CommandManager commandManager = new CommandManager(configService, brotherService, userService, musicFileService, excuseService, waiter);
    List<Class<? extends ICommand>> availableCommands = Arrays.asList(HelpCommand.class, SetConfigCommand.class, KickCommand.class, MoveCommand.class, RollCommand.class, MemeCommand.class, HelloThereCommand.class, BrotherCommand.class, ChCommand.class, ShhCommand.class, TalkCommand.class, PollCommand.class, JoinCommand.class, LeaveCommand.class, PlayCommand.class, NowDigOnThisCommand.class, SetVolumeCommand.class, PauseCommand.class, ResumeCommand.class, LoopCommand.class, StopCommand.class, SkipCommand.class, NowPlayingCommand.class, QueueCommand.class, ShuffleCommand.class, AdjustUserCommand.class, IntroSongCommand.class, EventWaiterCommand.class, UserInfoCommand.class, RandomCommand.class, Kf2RandomMapCommand.class, DbdRandomKillerCommand.class, ExcuseCommand.class, SocialCreditCommand.class);
    assertTrue(availableCommands.containsAll(commandManager.getAllCommands().stream().map(ICommand::getClass).collect(Collectors.toList())));
    assertEquals(34, commandManager.getAllCommands().size());
}
Also used : CommandManager(toby.managers.CommandManager) ICommand(toby.command.ICommand) Test(org.junit.jupiter.api.Test)

Example 3 with ICommand

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

the class HelpCommand 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();
    if (args.isEmpty()) {
        StringBuilder builder = new StringBuilder();
        Consumer<ICommand> commandConsumer = (command) -> builder.append('`').append(prefix).append(command.getName()).append('`').append(String.format(" Aliases are: '%s'", String.join(",", command.getAliases()))).append("\n");
        builder.append(String.format("List of all current commands below. If you want to find out how to use one of the commands try doing `%shelp commandName`\n", prefix));
        builder.append("**Music Commands**:\n");
        manager.getMusicCommands().forEach(commandConsumer);
        builder.append("**Miscellaneous Commands**:\n");
        manager.getMiscCommands().forEach(commandConsumer);
        builder.append("**Moderation Commands**:\n");
        manager.getModerationCommands().forEach(commandConsumer);
        builder.append("**Fetch Commands**:\n");
        manager.getFetchCommands().forEach(commandConsumer);
        channel.sendMessage(builder.toString()).queue(message -> ICommand.deleteAfter(message, deleteDelay));
        return;
    }
    String search = args.get(0);
    ICommand command = manager.getCommand(search);
    if (command == null) {
        channel.sendMessage("Nothing found for " + search).queue(message -> ICommand.deleteAfter(message, deleteDelay));
        return;
    }
    channel.sendMessage(command.getHelp(prefix)).queue(message -> ICommand.deleteAfter(message, deleteDelay));
}
Also used : CommandContext(toby.command.CommandContext) Consumer(java.util.function.Consumer) Arrays(java.util.Arrays) List(java.util.List) UserDto(toby.jpa.dto.UserDto) ICommand(toby.command.ICommand) CommandManager(toby.managers.CommandManager) TextChannel(net.dv8tion.jda.api.entities.TextChannel) TextChannel(net.dv8tion.jda.api.entities.TextChannel) ICommand(toby.command.ICommand)

Aggregations

ICommand (toby.command.ICommand)3 CommandContext (toby.command.CommandContext)2 UserDto (toby.jpa.dto.UserDto)2 CommandManager (toby.managers.CommandManager)2 Arrays (java.util.Arrays)1 List (java.util.List)1 Consumer (java.util.function.Consumer)1 TextChannel (net.dv8tion.jda.api.entities.TextChannel)1 Test (org.junit.jupiter.api.Test)1 UserDtoHelper.calculateUserDto (toby.helpers.UserDtoHelper.calculateUserDto)1