added experience tracking and automatic role assigning at given levels

includes: set role command to set a role to a given level (it clears out previous levels)
automatic level config generation on startup
min/max/multiplier configuration
job to persist the xp of the previous minute
added delayed scheduler start, so that events have a discord context in order to function
added role listener to automatically add and mark roles when they are created/deleted
fixed user in a server not being created correctly
added role service to give users role given by id
added server reference to role
This commit is contained in:
Sheldan
2020-04-11 17:21:55 +02:00
parent 87c97d5069
commit 6a31dfde8a
72 changed files with 1165 additions and 80 deletions

View File

@@ -0,0 +1,7 @@
package dev.sheldan.abstracto.core.exception;
public class RoleException extends AbstractoRunTimeException {
public RoleException(String message) {
super(message);
}
}

View File

@@ -3,10 +3,7 @@ package dev.sheldan.abstracto.core.models.database;
import dev.sheldan.abstracto.core.models.SnowFlake;
import lombok.*;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.*;
@Entity
@Table(name="role")
@@ -19,8 +16,14 @@ public class ARole implements SnowFlake {
@Getter @Setter
private Long id;
@Column(unique = true)
@Getter @Setter
private String name;
@ManyToOne(fetch = FetchType.LAZY)
@Getter
@Setter
@JoinColumn(name = "role_server_id")
private AServer server;
@Getter
@Setter
private Boolean deleted;
}

View File

@@ -21,8 +21,12 @@ public class AServer implements SnowFlake {
private String name;
@OneToMany(fetch = FetchType.LAZY)
@OneToMany(
fetch = FetchType.LAZY,
cascade = CascadeType.ALL,
orphanRemoval = true)
@Builder.Default
@JoinColumn(name = "role_server_id")
private List<ARole> roles = new ArrayList<>();
@OneToMany(

View File

@@ -14,7 +14,7 @@ import java.util.Optional;
import java.util.concurrent.CompletableFuture;
@Service
public interface Bot {
public interface BotService {
void login() throws LoginException;
JDA getInstance();
GuildChannelMember getServerChannelUser(Long serverId, Long channelId, Long userId);

View File

@@ -3,5 +3,6 @@ package dev.sheldan.abstracto.core.service;
public interface ConfigService {
Double getDoubleValue(String name, Long serverId);
Double getDoubleValue(String name, Long serverId, Double defaultValue);
void createDoubleValueIfNotExist(String name, Long serverId, Double value);
}

View File

@@ -0,0 +1,12 @@
package dev.sheldan.abstracto.core.service;
import dev.sheldan.abstracto.core.models.database.ARole;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import net.dv8tion.jda.api.entities.Role;
public interface RoleService {
void addRoleToUser(AUserInAServer aUserInAServer, ARole role);
void removeRoleFromUser(AUserInAServer aUserInAServer, ARole role);
void markDeleted(Role role);
void markDeleted(Long id);
}

View File

@@ -1,7 +1,10 @@
package dev.sheldan.abstracto.core.service.management;
import dev.sheldan.abstracto.core.models.database.ARole;
import dev.sheldan.abstracto.core.models.database.AServer;
public interface RoleManagementService {
ARole createRole(Long id);
ARole createRole(Long id, AServer server);
ARole findRole(Long id);
void markDeleted(ARole role);
}

View File

@@ -2,13 +2,12 @@ package dev.sheldan.abstracto.core.utils;
import dev.sheldan.abstracto.core.exception.AbstractoRunTimeException;
import dev.sheldan.abstracto.core.service.management.ChannelManagementService;
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
import dev.sheldan.abstracto.core.service.management.UserManagementService;
import dev.sheldan.abstracto.core.models.cache.CachedMessage;
import dev.sheldan.abstracto.core.models.GuildChannelMember;
import dev.sheldan.abstracto.core.models.context.UserInitiatedServerContext;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.core.service.Bot;
import dev.sheldan.abstracto.core.service.BotService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -27,11 +26,11 @@ public class ContextUtils {
private UserManagementService userManagementService;
@Autowired
private Bot bot;
private BotService botService;
public <T extends UserInitiatedServerContext> UserInitiatedServerContext fromMessage(CachedMessage message, Class<T> clazz) {
Method m = null;
GuildChannelMember guildChannelMember = bot.getServerChannelUser(message.getServerId(), message.getChannelId(), message.getAuthorId());
GuildChannelMember guildChannelMember = botService.getServerChannelUser(message.getServerId(), message.getChannelId(), message.getAuthorId());
try {
m = clazz.getMethod("builder");
UserInitiatedServerContext.UserInitiatedServerContextBuilder<?, ?> builder = (UserInitiatedServerContext.UserInitiatedServerContextBuilder) m.invoke(null, null);

View File

@@ -7,9 +7,8 @@ import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.models.database.AUser;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.core.models.template.commands.PingModel;
import dev.sheldan.abstracto.core.service.Bot;
import dev.sheldan.abstracto.core.service.BotService;
import dev.sheldan.abstracto.core.service.management.ChannelManagementService;
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
import dev.sheldan.abstracto.core.service.management.UserManagementService;
import org.junit.Before;
import org.junit.Test;
@@ -38,12 +37,12 @@ public class ContextUtilsTest {
private UserManagementService userManagementService;
@Mock
private Bot bot;
private BotService botService;
@Before
public void setup() {
GuildChannelMember build = GuildChannelMember.builder().build();
when(bot.getServerChannelUser(eq(SERVER_ID), eq(CHANNEL_ID), eq(AUTHOR_ID))).thenReturn(build);
when(botService.getServerChannelUser(eq(SERVER_ID), eq(CHANNEL_ID), eq(AUTHOR_ID))).thenReturn(build);
AServer server = AServer.builder().id(SERVER_ID).build();
AUserInAServer aUserInAServer = AUserInAServer.builder().userReference(AUser.builder().id(AUTHOR_ID).build()).serverReference(server).build();
when(userManagementService.loadUser(eq(SERVER_ID), eq(AUTHOR_ID))).thenReturn(aUserInAServer);