Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DiscordStartGameWebhook and DiscordPhaseUpdateWebhook to Game #194

Merged
merged 5 commits into from
Jun 13, 2024

Conversation

johnpooch
Copy link
Collaborator

@johnpooch johnpooch commented Jun 12, 2024

Adds DiscordWebhookfield to Game:

These webhooks will be invoked when the game is started, and when the phase is updated.

By using webhooks, the server doesn't need to know what needs to happen when certain updates happen. Instead, the Dipcord NodeJS server will listen for these webhook messages, and carry out any necessary actions.

@zond is this all I need to do to add optional fields to a data model? I want the fields to be either nil or an empty string if not specified. I don't want this to break existing create game endpoint.

@johnpooch johnpooch requested a review from zond June 12, 2024 20:16
@johnpooch
Copy link
Collaborator Author

In fact, I might update it to take { id: string, token: string } for each webhook. This will mean I can invoke the webhook using discordgo library which should be a bit neater.

@zond
Copy link
Owner

zond commented Jun 13, 2024

Do I understand it correctly, that this (and the previous PR) is to push events from the game service to Discord? Or is it from the game service to the bot service?

@johnpooch
Copy link
Collaborator Author

Do I understand it correctly, that this (and the previous PR) is to push events from the game service to Discord? Or is it from the game service to the bot service?

@zond I briefly made a PR to have the Diplicity service integrate with the bot directly to update the server when the game is created or phase starts. But I decided it would be a bit of a headache to continue that way. Mainly because it's hard to test.

In this approach, Diplicity service is set up to simply invoke a webhook when certain events happen. The Dipcord bot service will "hear" the webhook being invoked and will know that an update has happened, so it will then fetch the new state from Diplicity service again.

It's a bit more roundabout, but it's much easier to test (can just invoke webhook to test, don't need to actually create a game and finish phase) and easier to maintain (all bot stuff in the one service and language)

@zond
Copy link
Owner

zond commented Jun 13, 2024

So the webhook is on the Discord servers, and will trigger an event that via WebSocket reaches the bot?

Fair enough :D

@johnpooch
Copy link
Collaborator Author

So the webhook is on the Discord servers, and will trigger an event that via WebSocket reaches the bot?

Fair enough :D

Here is the related code in the NodeJS server:

(client as Client<true>).on(Events.MessageCreate, async (message) => {
  if (message.webhookId) {
    const webhook = await message.fetchWebhook();
    if (webhookHandlers.gameStarted.name === webhook.name)
      webhookHandlers.gameStarted.execute(webhook);
  }
});

So basically it is just listening for any message with a webhookId, and then using the webhook name to determine what to do. The webhook will be created in the first place by the bot when creating the game.

Like I said, it's a bit roundabout, but I think ultimately easier.

@johnpooch johnpooch merged commit b42db4e into master Jun 13, 2024
1 check failed
@johnpooch johnpooch deleted the johnpooch/add-webhooks-to-game branch June 13, 2024 10:11
@zond
Copy link
Owner

zond commented Jun 13, 2024

Cool, but it feels a bit Rube Goldberg-y :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants