Skip to content

Commit

Permalink
Merge pull request #29 from Softawii/feature/template
Browse files Browse the repository at this point in the history
Feature/template

Closes #23
  • Loading branch information
FerroEduardo authored Jun 7, 2022
2 parents 1c09942 + 28d403d commit 4d00c54
Show file tree
Hide file tree
Showing 14 changed files with 671 additions and 13 deletions.
1 change: 0 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
name: build
on:
push:
pull_request:

jobs:
build:
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/softawii/capivara/Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.softawii.capivara;

import com.softawii.capivara.core.PackageManager;
import com.softawii.capivara.core.TemplateManager;
import com.softawii.capivara.listeners.PackageGroup;
import com.softawii.capivara.listeners.TemplateGroup;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Activity;
import org.springframework.boot.CommandLineRunner;
Expand All @@ -20,6 +22,7 @@ public static void main(String[] args) {
jda.getPresence().setPresence(Activity.of(Activity.ActivityType.WATCHING,buildProperties.getVersion()), true);
System.out.println(buildProperties.getVersion() + " Bot is ready as " + jda.getSelfUser().getName());
PackageGroup.packageManager = context.getBean(PackageManager.class);
TemplateGroup.templateManager = context.getBean(TemplateManager.class);
}

@Override
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/softawii/capivara/config/SpringConfig.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.softawii.capivara.config;

import com.softawii.capivara.listeners.PackageGroup;
import com.softawii.capivara.listeners.TemplateGroup;
import com.softawii.curupira.core.Curupira;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
Expand Down Expand Up @@ -63,7 +64,10 @@ public JDA jda() {
builder.enableIntents(GatewayIntent.GUILD_EMOJIS);
builder.setMemberCachePolicy(MemberCachePolicy.ALL);
builder.enableCache(CacheFlag.EMOTE, CacheFlag.ROLE_TAGS, CacheFlag.MEMBER_OVERRIDES);
builder.addEventListeners(new PackageGroup.AutoCompleter());
builder.addEventListeners(
new PackageGroup.AutoCompleter(),
new TemplateGroup.AutoCompleter()
);
jda = builder.build();
jda.awaitReady();
} catch (LoginException | InterruptedException e) {
Expand Down
55 changes: 55 additions & 0 deletions src/main/java/com/softawii/capivara/core/TemplateManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.softawii.capivara.core;

import com.softawii.capivara.entity.Template;
import com.softawii.capivara.entity.Template.TemplateKey;
import com.softawii.capivara.exceptions.TemplateAlreadyExistsException;
import com.softawii.capivara.exceptions.TemplateDoesNotExistException;
import com.softawii.capivara.services.TemplateService;
import net.dv8tion.jda.api.interactions.commands.Command;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class TemplateManager {

private final TemplateService templateService;

public TemplateManager(TemplateService templateService) {
this.templateService = templateService;
}

public Template create(Long guildId, String name, String json) throws TemplateAlreadyExistsException {
return templateService.create(new Template(new TemplateKey(guildId, name), json));
}

public Template update(Long guildId, String name, String json) throws TemplateDoesNotExistException {
Template template = templateService.findById(new TemplateKey(guildId, name));
template.setJson(json);
return templateService.update(template);
}

public void destroy(Long guildId, String name) throws TemplateDoesNotExistException {
templateService.destroy(guildId, name);
}

public List<Template> findAllByGuildId(Long guildId) {
return templateService.findAllByGuildId(guildId);
}

public Template findById(Long guildId, String name) throws TemplateDoesNotExistException {
return templateService.findById(new TemplateKey(guildId, name));
}

public boolean existsById(Long guildId, String name) {
return templateService.existsById(new TemplateKey(guildId, name));
}

public List<Command.Choice> autoCompleteTemplateName(Long guildId, String templateName) {
return templateService.findAllByGuildId(guildId).stream()
.map(template -> template.getTemplateKey().getName())
.filter(c -> c.startsWith(templateName))
.map(c -> new Command.Choice(c, c))
.toList();
}
}
88 changes: 88 additions & 0 deletions src/main/java/com/softawii/capivara/entity/Template.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.softawii.capivara.entity;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import java.io.Serializable;
import java.util.Objects;

@Entity
public class Template {

@EmbeddedId
private TemplateKey templateKey;

@Embeddable
public static class TemplateKey implements Serializable {
@Column
private Long guildId;

@Column
private String name;

public TemplateKey() {
}

public TemplateKey(Long guildId, String name) {
this.guildId = guildId;
this.name = name;
}

public Long getGuildId() {
return guildId;
}

public void setGuildId(Long guildId) {
this.guildId = guildId;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TemplateKey that = (TemplateKey) o;
return guildId.equals(that.guildId) && name.equals(that.name);
}

@Override
public int hashCode() {
return Objects.hash(guildId, name);
}
}

@Column
private String json;

public Template() {
}

public Template(TemplateKey templateKey, String json) {
this.templateKey = templateKey;
this.json = json;
}

public TemplateKey getTemplateKey() {
return templateKey;
}

public void setTemplateKey(TemplateKey templateKey) {
this.templateKey = templateKey;
}

public String getJson() {
return json;
}

public void setJson(String json) {
this.json = json;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.softawii.capivara.exceptions;

public class CategoryIsEmptyException extends Exception {
public CategoryIsEmptyException() {
}

public CategoryIsEmptyException(String message) {
super(message);
}

public CategoryIsEmptyException(String message, Throwable cause) {
super(message, cause);
}

public CategoryIsEmptyException(Throwable cause) {
super(cause);
}

public CategoryIsEmptyException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.softawii.capivara.exceptions;

public class TemplateAlreadyExistsException extends Exception {
public TemplateAlreadyExistsException() {
}

public TemplateAlreadyExistsException(String message) {
super(message);
}

public TemplateAlreadyExistsException(String message, Throwable cause) {
super(message, cause);
}

public TemplateAlreadyExistsException(Throwable cause) {
super(cause);
}

public TemplateAlreadyExistsException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.softawii.capivara.exceptions;

public class TemplateDoesNotExistException extends Exception {
public TemplateDoesNotExistException() {
}

public TemplateDoesNotExistException(String message) {
super(message);
}

public TemplateDoesNotExistException(String message, Throwable cause) {
super(message, cause);
}

public TemplateDoesNotExistException(Throwable cause) {
super(cause);
}

public TemplateDoesNotExistException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
13 changes: 4 additions & 9 deletions src/main/java/com/softawii/capivara/listeners/PackageGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import kotlin.Pair;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Emote;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
Expand Down Expand Up @@ -52,8 +51,7 @@ public static void create(SlashCommandInteractionEvent event) {

if(name.contains(":")) {
LOGGER.debug(String.format("create: Package Name contains :, just ignore it (%s)", name));
MessageEmbed embed = Utils.simpleEmbed("Nome muito feio!", "O nome do package não pode conter o caracter ':', foi mal, problemas internos aqui!", Color.RED);
event.replyEmbeds(embed).setEphemeral(true).queue();
event.replyEmbeds(Utils.nameContainsColon("O nome do package")).setEphemeral(true).queue();
return;
}

Expand Down Expand Up @@ -173,8 +171,7 @@ public static void add(SlashCommandInteractionEvent event) {

if(packageName.contains(":")) {
LOGGER.debug(String.format("add: Package Name contains :, just ignore it (%s)", packageName));
MessageEmbed embed = Utils.simpleEmbed("Nome muito feio!", "O nome do package não pode conter o caracter ':', foi mal, problemas internos aqui!", Color.RED);
event.replyEmbeds(embed).setEphemeral(true).queue();
event.replyEmbeds(Utils.nameContainsColon("O nome do package")).setEphemeral(true).queue();
return;
}

Expand All @@ -183,8 +180,7 @@ public static void add(SlashCommandInteractionEvent event) {

if(name.contains(":")) {
LOGGER.debug(String.format("add: Role Name contains :, just ignore it (%s)", name));
MessageEmbed embed = Utils.simpleEmbed("Nome muito feio!", "O nome do cargo não pode conter o caracter ':', foi mal, problemas internos aqui!", Color.RED);
event.replyEmbeds(embed).setEphemeral(true).queue();
event.replyEmbeds(Utils.nameContainsColon("O nome do package")).setEphemeral(true).queue();
return;
}

Expand All @@ -198,8 +194,7 @@ public static void add(SlashCommandInteractionEvent event) {
isUnicode = emoji.getSecond();
} catch (MultipleEmojiMessageException e) {
LOGGER.debug("add: MultipleEmojiMessageException");
MessageEmbed embed = Utils.simpleEmbed("Emoji muito feio!", "O emoji não pode conter mais de um emoji, tu ta doidinho!", Color.RED);
event.replyEmbeds(embed).setEphemeral(true).queue();
event.replyEmbeds(Utils.multipleEmoji()).setEphemeral(true).queue();
return;
}

Expand Down
Loading

0 comments on commit 4d00c54

Please sign in to comment.