Search code examples
spring-bootdiscorddiscord4j

How two concurrent discord4j applications receive events from Discord server


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)?


Solution

  • 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.