Search in sources :

Example 11 with SlashCommandEvent

use of net.dv8tion.jda.api.events.interaction.SlashCommandEvent in project TJ-Bot by Together-Java.

the class TagManageCommandTest method editWithMessageFailsForInvalidMessageId.

@Test
@DisplayName("'/tag-manage edit-with-message' fails if the given message id is in an invalid format")
void editWithMessageFailsForInvalidMessageId() {
    // GIVEN a tag system with the "foo" tag
    system.putTag("foo", "old");
    // WHEN using '/tag-manage edit-with-message id:foo message-id:new'
    SlashCommandEvent event = triggerEditWithMessageCommand("foo", "bar");
    // THEN the command fails and responds accordingly, the tags content was not changed
    verify(event).reply("The given message id 'bar' is invalid, expected a number.");
    assertEquals("old", system.getTag("foo").orElseThrow());
}
Also used : SlashCommandEvent(net.dv8tion.jda.api.events.interaction.SlashCommandEvent) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 12 with SlashCommandEvent

use of net.dv8tion.jda.api.events.interaction.SlashCommandEvent in project TJ-Bot by Together-Java.

the class TagManageCommandTest method commandCanNotBeUsedWithoutRoles.

@Test
@DisplayName("Users without the required role can not use '/tag-manage'")
void commandCanNotBeUsedWithoutRoles() {
    // GIVEN a regular user without roles
    Member regularUser = jdaTester.createMemberSpy(1);
    // WHEN the regular user triggers any '/tag-manage' command
    SlashCommandEvent event = triggerRawCommandWithUser("foo", regularUser);
    // THEN the command can not be used since the user lacks roles
    verify(event).reply("Tags can only be managed by users with a corresponding role.");
}
Also used : Member(net.dv8tion.jda.api.entities.Member) SlashCommandEvent(net.dv8tion.jda.api.events.interaction.SlashCommandEvent) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 13 with SlashCommandEvent

use of net.dv8tion.jda.api.events.interaction.SlashCommandEvent in project TJ-Bot by Together-Java.

the class TagManageCommandTest method editWithMessageUnknownTagFails.

@Test
@DisplayName("'/tag-manage edit-with-message' fails if the tag is unknown")
void editWithMessageUnknownTagFails() {
    // GIVEN a tag system without any tags
    postMessage("bar", "1");
    // WHEN using '/tag-manage edit-with-message id:foo message-id:new'
    SlashCommandEvent event = triggerEditWithMessageCommand("foo", "1");
    // THEN the command fails and responds accordingly, the tag was not created
    verify(event).reply(startsWith("Could not find any tag with id"));
    assertFalse(system.hasTag("foo"));
}
Also used : SlashCommandEvent(net.dv8tion.jda.api.events.interaction.SlashCommandEvent) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 14 with SlashCommandEvent

use of net.dv8tion.jda.api.events.interaction.SlashCommandEvent in project TJ-Bot by Together-Java.

the class TagManageCommandTest method editExistingTagWorks.

@Test
@DisplayName("'/tag-manage edit' works if the tag is known")
void editExistingTagWorks() {
    // GIVEN a tag system with the "foo" tag
    system.putTag("foo", "old");
    // WHEN using '/tag-manage edit id:foo content:new'
    SlashCommandEvent event = triggerEditCommand("foo", "new");
    // THEN the command succeeds and the content of the tag was changed
    assertEquals("Success", getResponse(event).getTitle());
    assertEquals("new", system.getTag("foo").orElseThrow());
}
Also used : SlashCommandEvent(net.dv8tion.jda.api.events.interaction.SlashCommandEvent) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 15 with SlashCommandEvent

use of net.dv8tion.jda.api.events.interaction.SlashCommandEvent in project TJ-Bot by Together-Java.

the class TagsCommand method onSlashCommand.

@Override
public void onSlashCommand(@NotNull SlashCommandEvent event) {
    Collection<String> tagIds = tagSystem.getAllIds();
    if (tagIds.size() > MAX_TAGS_THRESHOLD_WARNING) {
        // TODO Implement the edge case
        logger.warn("The amount of tags is very high and it might soon exceed the maximum character limit. The code should be adjusted to support this edge case soon.");
    }
    String tagListText = tagIds.stream().sorted().map(tag -> "• " + tag).collect(Collectors.joining("\n"));
    event.replyEmbeds(new EmbedBuilder().setTitle("All available tags").setDescription(tagListText).setFooter(event.getUser().getName() + " • used " + event.getCommandString()).setTimestamp(Instant.now()).setColor(TagSystem.AMBIENT_COLOR).build()).addActionRow(TagSystem.createDeleteButton(generateComponentId(event.getUser().getId()))).queue();
}
Also used : SlashCommandVisibility(org.togetherjava.tjbot.commands.SlashCommandVisibility) Logger(org.slf4j.Logger) ButtonClickEvent(net.dv8tion.jda.api.events.interaction.ButtonClickEvent) Permission(net.dv8tion.jda.api.Permission) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) SlashCommandAdapter(org.togetherjava.tjbot.commands.SlashCommandAdapter) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) SlashCommandEvent(net.dv8tion.jda.api.events.interaction.SlashCommandEvent) Objects(java.util.Objects) List(java.util.List) NotNull(org.jetbrains.annotations.NotNull) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder)

Aggregations

SlashCommandEvent (net.dv8tion.jda.api.events.interaction.SlashCommandEvent)35 Test (org.junit.jupiter.api.Test)24 DisplayName (org.junit.jupiter.api.DisplayName)23 NotNull (org.jetbrains.annotations.NotNull)8 Collectors (java.util.stream.Collectors)3 Member (net.dv8tion.jda.api.entities.Member)3 IOException (java.io.IOException)2 List (java.util.List)2 Consumer (java.util.function.Consumer)2 TechDiscordBot (me.TechsCode.TechDiscordBot.TechDiscordBot)2 DefinedQuery (me.TechsCode.TechDiscordBot.objects.DefinedQuery)2 Query (me.TechsCode.TechDiscordBot.objects.Query)2 TechEmbedBuilder (me.TechsCode.TechDiscordBot.util.TechEmbedBuilder)2 Permission (net.dv8tion.jda.api.Permission)2 Guild (net.dv8tion.jda.api.entities.Guild)2 MessageEmbed (net.dv8tion.jda.api.entities.MessageEmbed)2 OptionType (net.dv8tion.jda.api.interactions.commands.OptionType)2 OptionData (net.dv8tion.jda.api.interactions.commands.build.OptionData)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2