Home > Net >  How two concurrent discord4j applications receive events from Discord server
How two concurrent discord4j applications receive events from Discord server

Time:01-28

I'm using discord4j in a Spring Boot Application for bot operation (it posts some data to channels using channel.createMessage and replies to some messages using MessageCreateEvent handling).

Core code looks like that:

@Bean
public GatewayDiscordClient gatewayDiscordClient() {
    GatewayDiscordClient client = DiscordClientBuilder.create(token)
        .build()
        .login()
        .block();

    client.on(MessageCreateEvent.class, event -> processMessage(event)).subscribe();

    return client;
}

The production version of application is runned on a cloud server, and for debugging purposes I launch testing instance of it on my personal laptop. While I'm debugging the application, I actually have two simultaneous GatewayDiscordClient configurations (one on a production server, second on my testing laptop). Both of them use identical bot token.

How Discord and discord4j handles it? If Discord server produces an event (for example, somebody writes to my bot's DM or mentions it in a channel where bot is participating), should I expect to receive notifications on both instances, or only one will operate (and which)?

CodePudding user response:

You're using the same token, so you're using the production account for testing which is really bad practice. But both gateways will receive the same events because its the same token. It's no different than being logged into your discord account on your phone and desktop at the same time, just a bot instead of a user.

You should create a second bot application solely for development testing and keep your production environment isolated. Using webhook interactions will not save you here as yes, the interactions will only be sent to one place, but if you continue to use the same account for prod/development you're going to get prod and dev env data mixing together.

CodePudding user response:

According to discord documentation. Gateway is a WebSocket, so you should receive events on both instances of the application. Alternatively you could use webhooks, so you could set only one app that will receive the interaction. If you decide on the second approach you could use a tool such as ngrok to receive requests on your local machine.

  • Related