Kaktushose / jda-commands

A declarative, annotation driven command library for JDA
https://github.com/Kaktushose/jda-commands/wiki
Apache License 2.0
67 stars 11 forks source link

Error in discord, but not in console. #130

Closed Scyye closed 8 months ago

Scyye commented 10 months ago

I'm getting this response when running a command: image

however, when doing as it suggests and checking the log console, theres nothing. any clue?

Kaktushose commented 10 months ago

Please share the coresponding code of the command

Scyye commented 10 months ago

it is terribly coded and theres a lot of stuff that doesnt need to be there, but here ya go

@SlashCommand(value = "package search")
    public void onPackageGet(CommandEvent event,
                             @Param(name = "community", value = "The community to retrieve from") String community,
                             @Optional @Param(name = "search", value = "The search") String search) {
        if (!ServerConfig.configs.get(event.getGuild().getId()).get("community").equals(""))
            community = ServerConfig.configs.get(event.getGuild().getId()).get("community", String.class);

        PackageListing[] result;

        if (!TestCommands.checkExecute(event, e ->
                !ServerConfig.configs.get(event.getGuild().getId()).get("disabledChannels", List.class).contains(e.getChannel().getId())
                && !ServerConfig.configs.get(event.getGuild().getId()).get("disabledUsers", List.class).contains(e.getUser().getId())))
            return;

        if (search!=null)
            if (search.length()>100) {
                event.reply("Search query too long.");
                return;
            }

        if (community!=null)
            if (community.length()>100) {
                event.reply("Community name too long.");
                return;
            }

        if (search!=null)
            search = MarkdownSanitizer.sanitize(search).replace(" ", "_");

        if (community!=null) {
            community = community.toLowerCase().replace(" ", "-");
            community = MarkdownSanitizer.sanitize(community);

            boolean success = false;

            for (var c : Bot.bot.tsja.getCommunities()) {
                if (c.getIdentifier().equals(community)) {
                    success=true;
                    break;
                }
            }

            if (!success) {
                event.reply("Invalid Community. Do `/community all` for a list of valid communities.");
                return;
            }
        } else {
            community = "all";
        }

        if (search != null) {
            result = new TSJAUtils().getPackagesByName(Bot.bot.tsja, community, search);
        } else {
            result = Bot.bot.tsja.getPackages(community, null);
        }

        PaginatedMenuHandler.Menu menu;
        List<PaginatedMenuHandler.Page> pages = new ArrayList<>();
        PaginatedMenuHandler.Page currentPage =
                new PaginatedMenuHandler.Page(MessageCreateData.fromContent("# Mods\nSearch: " + search + "\n\n\n"));

        StringBuilder builder = new StringBuilder();

        for (PackageListing p : result) {
            if (builder.length() > 1500) {
                currentPage.append(builder.toString()); // Append builder to the current page
                builder.setLength(0); // Clear the builder
                pages.add(currentPage); // Add the current page to the list of pages
                currentPage = new PaginatedMenuHandler.Page(MessageCreateData.fromContent("")); // Create a new page
            }

            String packageName = p.isDeprecated() ? "~~" + p.getName() + "~~" : p.getName();
            String packageLink = MarkdownUtil.maskedLink(packageName, "<" + p.getPackageUrl() + ">");
            String ownerLink = MarkdownUtil.maskedLink(p.getOwner(), "<" + TSJA.getTeamUrl(community, p.getOwner()) + ">");
            String downloadLink = MarkdownUtil.maskedLink("here", "<" + p.getVersions()[0].getDownloadUrl() + ">");
            String content = String.format("%s by %s, download %s (%s)\n", packageLink, ownerLink, downloadLink, p.getUniqueId());
            builder.append(content);
        }

        currentPage.append(builder.toString());
        pages.add(currentPage);

        event.reply("MENU LOADING...", msg -> {
            PaginatedMenuHandler.addMenu(PaginatedMenuHandler.buildMenu(msg, pages.stream().map(page -> page.content).toList().toArray(
                    MessageCreateData[]::new)));
        });
    }
Scyye commented 10 months ago

If it helps, this has been happening for a while. Its not a new update thing.

Kaktushose commented 10 months ago

Using the latest commit to the development and the following command class and logback config, I get perfectly fine error handling:

@Interaction
public class ExceptionTest {

    @SlashCommand(value = "exception")
    public void onCommand(CommandEvent event) {
        throw new RuntimeException("test");
    }
}
<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>
    <root level="info">
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>

It's virtually impossible for me to reproduce this bug and I'm not even sure if it's related to jda-commands. Please consider the following steps to help me tackle down this bug:

  1. Check and preferably share your logging configuration
  2. Create a test command that directly throws an exception like I did and see if it's logged correctly
  3. Execute your command code outside of a jda-commands environment so we can find the spot where the NPE occurs. This would help me with trying to reproduce the bug
Scyye commented 10 months ago
  1. using the same logback.xml as you, no log, yet theres an error in discord, see below.

  2. image

@SlashCommand(value = "test")
public void onTest(CommandEvent event) {
    throw new RuntimeException("Test");
}

as well as

@SlashCommand(value = "test")
public void onTest(CommandEvent event) throws RuntimeException {
        throw new RuntimeException("Test");
}

No output to console

  1. If this is needed, ill do it. although it doesnt seem like it, as even directly throwing a runtime exception doesnt print to the log

Kaktushose commented 9 months ago

which version of jda-commands are you using?

Scyye commented 9 months ago

4.0.0 beta.1

Kaktushose commented 9 months ago

can you test if exceptions get logged when they are thrown outside of jda-commands?

Scyye commented 9 months ago

logs perfectly.

Kaktushose commented 9 months ago

I'm unable to reproduce the bug. Is your repository public?

Scyye commented 9 months ago

https://github.com/Scyye/ThunderstoreBrowser/blob/main/src/main/java/dev/scyye/thunderstorebot/commands/APICommands.java

Kaktushose commented 9 months ago

looking at your pom.xml it seems like you haven't configured logback correctly. You also need to add the logback dependency:

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.2.8</version>
</dependency>

I've cloned your repo, added logback and exceptions were logged perfectly fine