GithubHelp home page GithubHelp logo

Slash Commands issue about javacord HOT 11 CLOSED

cmclient avatar cmclient commented on June 16, 2024
Slash Commands issue

from javacord.

Comments (11)

felldo avatar felldo commented on June 16, 2024

I cannot reproduce this issue. Are you sure this piece of code causes the high CPU usage? Maybe your hardware is just too weak, or another part of your code causes this issue.
If you are sure that this is causing your issue, please create a simple, minimal working example that reproduces your problem.

from javacord.

cmclient avatar cmclient commented on June 16, 2024

Ryzen 9 5950X is weak?
im sure that creating the slash command is the issue, tried java 11 and 16.
and bot after while creating the command use 2GB of ram.
without slash command everything works fine.
i have log "loading commands..." just before creating commands and issues starts right after this

from javacord.

felldo avatar felldo commented on June 16, 2024

Ryzen 9 5950X is weak?

This was just an assumption, I don't know what hardware you are running your bot on.

The first thing you can try is changing your code to

SlashCommand.with("help", "List of all commands")
.createGlobal(BotApplication.getInstance().getApi())
.exceptionally(ExceptionLogger.get())
.join()

So you actually get exceptions that discord may throw.
After that, you really should provide a complete minimal reproducible example that I can try.

from javacord.

cmclient avatar cmclient commented on June 16, 2024

I created minimal example bot to verify it, and it happen when i have +3 commands.
Here is code to reproduce:

package pl.cmclient;

import org.javacord.api.DiscordApi;
import org.javacord.api.DiscordApiBuilder;
import org.javacord.api.entity.permission.PermissionType;
import org.javacord.api.interaction.SlashCommand;
import org.javacord.api.interaction.SlashCommandOption;
import org.javacord.api.interaction.SlashCommandOptionType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

public class TestApp {

    public static void main(String[] args) {
        Logger logger = LoggerFactory.getLogger(TestApp.class);
        DiscordApi api = new DiscordApiBuilder()
                .setToken("xxx")
                .setAllIntents()
                .login()
                .join();

        logger.info("Loading commands...");
        SlashCommand.with("help", "List of all commands")
                .createGlobal(api)
                .join();
        SlashCommand.with("ping", "Response time from Discord Gateway")
                .createGlobal(api)
                .join();
        SlashCommand.with("userinfo", "Info about player account",
                        Collections.singletonList(
                                SlashCommandOption.create(SlashCommandOptionType.STRING, "name", "Player name", true)))
                .createGlobal(api)
                .join();
        SlashCommand.with("test", "Test command")
                .createGlobal(api)
                .join();
        SlashCommand.with("clear", "Purge the chat",
                        Collections.singletonList(
                                SlashCommandOption.create(SlashCommandOptionType.LONG, "amount", "Amount of messages to purge", true)))
                .setDefaultEnabledForPermissions(PermissionType.MANAGE_MESSAGES)
                .setEnabledInDms(false)
                .createGlobal(api)
                .join();
        SlashCommand.with("ban", "Ban an user",
                        List.of(
                                SlashCommandOption.create(SlashCommandOptionType.USER, "user", "User that you want to ban", true),
                                SlashCommandOption.create(SlashCommandOptionType.STRING, "reason", "Reason of ban"),
                                SlashCommandOption.create(SlashCommandOptionType.LONG, "delete", "Delete messages from x days")))
                .setDefaultEnabledForPermissions(PermissionType.MANAGE_MESSAGES)
                .setEnabledInDms(false)
                .createGlobal(api)
                .join();
        SlashCommand.with("kick", "Kick an user",
                        List.of(
                                SlashCommandOption.create(SlashCommandOptionType.USER, "user", "User that you want to kick", true),
                                SlashCommandOption.create(SlashCommandOptionType.STRING, "reason", "Reason of kick")))
                .setDefaultEnabledForPermissions(PermissionType.MANAGE_MESSAGES)
                .setEnabledInDms(false)
                .createGlobal(api)
                .join();
        SlashCommand.with("embed", "Send message in a embed",
                        List.of(
                                SlashCommandOption.create(SlashCommandOptionType.STRING, "message", "Message (seperate title from description with |, for new line use \\n)", true)))
                .setDefaultEnabledForPermissions(PermissionType.MANAGE_MESSAGES)
                .createGlobal(api)
                .join();
        SlashCommand.with("pool", "Create a yes/no pool",
                        List.of(
                                SlashCommandOption.create(SlashCommandOptionType.STRING, "message", "Message (seperate title from description with |, for new line use \\n)", true)))
                .setDefaultEnabledForPermissions(PermissionType.MANAGE_MESSAGES)
                .createGlobal(api)
                .join();
        logger.info("Complete");
    }
}

from javacord.

felldo avatar felldo commented on June 16, 2024

Please regenerate your token, it is still visible in the edit history.

I tried your code and everything is working as intended, so Javacord does not seem to be an issue. Depending on where you see your RAM usage, it may also be consumed by your IDE. For me, IntelliJ IDEA likes to take up to 12 GB without doing anything.
Another thing you could try is using the snaphsot version which I am currently using. After that, I am out of ideas regarding issues with Javacord.

from javacord.

Saladoc avatar Saladoc commented on June 16, 2024

I also could not reproduce the behavior you described, using Javacord 3.8.0 and Java 11.

Side note: Registration of the Commands did take 15+ seconds when I tried it - using DiscordApi#bulkOverwriteGlobalApplicationCommands instead took less than half a second.

from javacord.

feddevanderlist avatar feddevanderlist commented on June 16, 2024

I created minimal example bot to verify it, and it happen when i have +3 commands. Here is code to reproduce:

edit: removed wall of code as it was unchanged from the replied to comment - Saladoc

I think my recommendation would be to add the slashcommands in 1 go instead of adding 1 each time and joining. This could be a network issue and the .join() makes every creation blocking.

from javacord.

cmclient avatar cmclient commented on June 16, 2024

I also could not reproduce the behavior you described, using Javacord 3.8.0 and Java 11.

Side note: Registration of the Commands did take 15+ seconds when I tried it - using DiscordApi#bulkOverwriteGlobalApplicationCommands instead took less than half a second.

Works better with bulkOverwriteGlobalApplicationCommands. Bot loading is quicker and RAM usage dropped to 600 MB from 2 GB. But its still slower and ram usage is higher than almost same bot in JDA - on JDA its 74 MB RAM usage.

from javacord.

felldo avatar felldo commented on June 16, 2024

How many servers is your bot in and how many members do these servers have?

from javacord.

cmclient avatar cmclient commented on June 16, 2024

How many servers is your bot in and how many members do these servers have?

1 server 150k people
this shouldn't be a issue beacuse it's happen only on bot loading

from javacord.

felldo avatar felldo commented on June 16, 2024

1 server 150k people
this shouldn't be a issue beacuse it's happen only on bot loading

If you also log in the same way as your minimal reproducible example, the RAM usage might be normal. Javacord by default caches all members, and since you also have the presence intent when logging in with all intents, even more data is being cached. Depending on how you log in with JDA (create, createLight, createDefault), there are differences on what is being cached by default.
But using bulkOverwriteGlobalApplicationCommands doesn't make a difference in RAM usage, since it effectively creates the same objects using the same class. The only difference should be the noticeable less amount of time it takes to execute the code since you don't wait for each command separately to be created.

So this will leave us with the high CPU usage, which I still can't believe has anything to do with Javacord in the way you showed us how you use it. Many other users are already on version 3.8.0 and haven't noticed anything, especially since the code for slash commands shouldn't have had any significant changes for quite some time. So I'm pretty confident to say that there is something wrong in your code or PC, that causes high CPU usage, since for @Saladoc and me it works fine with the MRE you provided to us.

from javacord.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.