Search in sources :

Example 11 with UserDto

use of toby.jpa.dto.UserDto in project toby-bot by ml404.

the class ExcuseCommand method approvePendingExcuse.

private void approvePendingExcuse(CommandContext ctx, UserDto requestingUserDto, TextChannel channel, String pendingExcuse, Integer deleteDelay) {
    if (requestingUserDto.isSuperUser()) {
        String excuseId = pendingExcuse.split(" ", 3)[2];
        ExcuseDto excuseById = excuseService.getExcuseById(Integer.parseInt(excuseId));
        if (!excuseById.isApproved()) {
            excuseById.setApproved(true);
            excuseService.updateExcuse(excuseById);
            channel.sendMessage(String.format("Approved excuse '%s'.", excuseById.getExcuse())).queue(message -> ICommand.deleteAfter(message, deleteDelay));
        } else
            channel.sendMessage("I've heard that excuse before, keep up.").queue(message -> ICommand.deleteAfter(message, deleteDelay));
    } else
        sendErrorMessage(ctx, channel, deleteDelay);
}
Also used : Message(net.dv8tion.jda.api.entities.Message) CommandContext(toby.command.CommandContext) List(java.util.List) ExcuseDto(toby.jpa.dto.ExcuseDto) UserDto(toby.jpa.dto.UserDto) IExcuseService(toby.jpa.service.IExcuseService) ICommand(toby.command.ICommand) Optional(java.util.Optional) Random(java.util.Random) TextChannel(net.dv8tion.jda.api.entities.TextChannel) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) ExcuseDto(toby.jpa.dto.ExcuseDto)

Example 12 with UserDto

use of toby.jpa.dto.UserDto in project toby-bot by ml404.

the class RollCommand method handle.

@Override
public void handle(CommandContext ctx, String prefix, UserDto requestingUserDto, Integer deleteDelay) {
    ICommand.deleteAfter(ctx.getMessage(), deleteDelay);
    TextChannel channel = ctx.getChannel();
    List<String> args = ctx.getArgs();
    Random rand = ThreadLocalRandom.current();
    String rollValue = (!args.isEmpty()) ? args.get(0) : "";
    int diceRoll = !rollValue.isEmpty() ? Integer.parseInt(rollValue) : 6;
    // This results in 1 - 6 (instead of 0 - 5) for default value
    int roll = rand.nextInt(diceRoll) + 1;
    channel.sendMessage("Your roll: " + roll).flatMap((v) -> roll <= (diceRoll / 2), // Send another message if the roll was bad (less than half top value)
    sentMessage -> channel.sendMessage("...shit be cool\n")).queue(message -> ICommand.deleteAfter(message, deleteDelay));
}
Also used : CommandContext(toby.command.CommandContext) List(java.util.List) UserDto(toby.jpa.dto.UserDto) ICommand(toby.command.ICommand) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Random(java.util.Random) TextChannel(net.dv8tion.jda.api.entities.TextChannel) TextChannel(net.dv8tion.jda.api.entities.TextChannel) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Random(java.util.Random)

Example 13 with UserDto

use of toby.jpa.dto.UserDto in project toby-bot by ml404.

the class AdjustUserCommand 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();
    List<Member> mentionedMembers = channelAndArgumentValidation(prefix, requestingUserDto, args, channel, message, member, deleteDelay);
    if (mentionedMembers == null)
        return;
    mentionedMembers.forEach(targetMember -> {
        UserDto targetUserDto = userService.getUserById(targetMember.getIdLong(), targetMember.getGuild().getIdLong());
        // 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 = userAdjustmentValidation(requestingUserDto, targetUserDto) || member.isOwner();
            if (requesterCanAdjustPermissions && isSameGuild) {
                validateArgumentsAndUpdateUser(channel, targetUserDto, args, member.isOwner(), deleteDelay);
                channel.sendMessageFormat("Updated user %s's permissions", targetMember.getNickname()).queue(message1 -> ICommand.deleteAfter(message1, deleteDelay));
            } else
                channel.sendMessageFormat("User '%s' is not allowed to adjust the permissions of user '%s'.", member.getNickname(), targetMember.getNickname()).queue(message1 -> ICommand.deleteAfter(message1, deleteDelay));
        } else {
            createNewUser(channel, targetMember, deleteDelay);
        }
    });
}
Also used : Message(net.dv8tion.jda.api.entities.Message) CommandContext(toby.command.CommandContext) Arrays(java.util.Arrays) UserDtoHelper.userAdjustmentValidation(toby.helpers.UserDtoHelper.userAdjustmentValidation) UserDto(toby.jpa.dto.UserDto) Member(net.dv8tion.jda.api.entities.Member) TextChannel(net.dv8tion.jda.api.entities.TextChannel) IUserService(toby.jpa.service.IUserService) Collectors(java.util.stream.Collectors) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) ICommand(toby.command.ICommand) Map(java.util.Map) TextChannel(net.dv8tion.jda.api.entities.TextChannel) Message(net.dv8tion.jda.api.entities.Message) UserDto(toby.jpa.dto.UserDto) Member(net.dv8tion.jda.api.entities.Member)

Example 14 with UserDto

use of toby.jpa.dto.UserDto in project toby-bot by ml404.

the class UserPersistenceImpl method getUserById.

@Override
public UserDto getUserById(Long discordId, Long guildId) {
    Query userQuery = em.createNamedQuery("UserDto.getById", UserDto.class);
    userQuery.setParameter("discordId", discordId);
    userQuery.setParameter("guildId", guildId);
    return (UserDto) userQuery.getSingleResult();
}
Also used : Query(javax.persistence.Query) UserDto(toby.jpa.dto.UserDto)

Example 15 with UserDto

use of toby.jpa.dto.UserDto 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)

Aggregations

UserDto (toby.jpa.dto.UserDto)27 CommandContext (toby.command.CommandContext)16 ICommand (toby.command.ICommand)16 List (java.util.List)15 TextChannel (net.dv8tion.jda.api.entities.TextChannel)14 Arrays (java.util.Arrays)10 MusicDto (toby.jpa.dto.MusicDto)9 Collectors (java.util.stream.Collectors)8 Message (net.dv8tion.jda.api.entities.Message)8 Member (net.dv8tion.jda.api.entities.Member)6 IUserService (toby.jpa.service.IUserService)6 IOException (java.io.IOException)4 Test (org.junit.jupiter.api.Test)4 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)4 UserDtoHelper.calculateUserDto (toby.helpers.UserDtoHelper.calculateUserDto)4 ConfigDto (toby.jpa.dto.ConfigDto)4 IConfigService (toby.jpa.service.IConfigService)4 Objects (java.util.Objects)3 Optional (java.util.Optional)3 Random (java.util.Random)3