Skip to content

Using Discord Purchase Roles

Which roles a customer gets

At every sync the extension works out the roles a customer should have:

  1. It takes the customer's services with a qualifying status. That is active, plus suspended while While a service is suspended is set to Keep the Discord role. Pending and cancelled services never qualify.
  2. It takes every enabled mapping whose product is among those services.
  3. Each of those mappings names a role in a server (its own override or the default server).

Then, for each server involved, it reads the customer's member record from Discord and compares.

Giving roles

  • A role the customer should have and does not have is added, and recorded in Discord Role Grants.
  • A role the customer already has is left alone. If the extension did not give it, it is not recorded, so the extension will not remove it later.
  • If the customer has not completed the server's membership screening, no role is added.

Removing roles

Only roles recorded in Discord Role Grants are ever removed. A recorded role that the customer no longer qualifies for is removed when one of these is true:

  • an enabled mapping for that role and server has Remove the role when the service ends switched on,
  • no mapping for that role and server exists any more, because you deleted or changed it.

It stays when mappings for it exist but none of the enabled ones has removal switched on. That covers a mapping with removal off and a disabled mapping.

When the customer is not in the server

If Discord answers that the Discord user is not a member of the server, the ledger entries for that server are dropped, since the roles are gone with the membership. If the customer qualifies for a role there, the sync reports "Please join the Discord server before syncing roles.", unless the server join option adds them. Once they have joined, the next sync gives the roles.

When Discord cannot be read

If the member record cannot be read for any other reason (timeout, missing server, permission problem, unexpected answer), nothing is added or removed in that server and the error is stored on the account.

When syncs run

Trigger What is queued
A service is created or deleted Sync for the service's owner
A service's status, product or owner changes Sync for the owner. On an owner change, also for the previous owner
A customer links a Discord account Sync for that customer
A mapping is created, edited or deleted Sync for every linked account, cleanup for every pending ledger entry
Bot token, default server, suspension setting or server join toggle is changed Same as above
Every 15 minutes (discord:roles:sync through Paymenter's scheduler) Same as above
A customer with a linked account is deleted Cleanup of that account's recorded roles

These syncs run as queued jobs after the database change is committed, so a Discord problem cannot interrupt an order, a payment or an admin edit. A job that ends with an error is retried up to six times in total, with waits of 60, 300, 900, 1800 and 3600 seconds. A sync that ends because Discord is not configured, the customer has no linked account, or the customer is not in the server is not retried; the 15 minute run picks it up again.

One sync works on Discord for about 60 seconds at most. If more is left, the account shows "More Discord changes remain; another sync will continue." and a later sync carries on.

You can queue the full reconciliation yourself:

php artisan discord:roles:sync

The manual actions below (sync, unlink, adopt) and the cleanup of the old account during an account change talk to Discord directly while the page waits, so they can take a few seconds.

Admin area

Discord Accounts

Lists every linked account with Customer (e-mail), Discord (display name), Discord user ID, Linked, Last sync and Last error. The Never synced filter separates accounts that have or have not been synced yet. You cannot create links here; customers link themselves.

Queue all Discord role syncs at the top queues a sync for every linked account and a cleanup for every pending ledger entry, like the 15 minute run.

Each row has three actions, all with a confirmation:

Action What it does
Sync Discord Roles Runs a sync for this customer immediately and reports "Sync finished: n role(s) added, n removed." or "Sync finished with a problem." with the reason.
Unlink Discord Removes every role recorded for this Discord account, then deletes the link. If a removal fails, the result is "Unlink remains pending." and the link stays in the pending state described under Unlinking.
Adopt existing mapped roles Records roles the customer already has as given by the extension. See below.

Adopt existing mapped roles

Use this for roles that were given before the extension kept a ledger (version 1.0.x), or by hand, and that the extension should manage from now on.

The action looks at every enabled mapping, in every server, and records each mapped role the customer currently has in Discord. It does not check whether the customer owns the mapped product. Nothing changes in Discord at that moment, but a sync is queued right after.

Warning

Adopted roles are treated like roles the extension gave. If the customer does not qualify for an adopted role and its mapping has removal switched on, the following sync removes it. Unlinking removes all of them. Check the customer's roles before you adopt.

The action needs a configured extension and a linked account that is not waiting for an unlink. If a server cannot be read, the result is "Adoption incomplete."; repeat the action later.

Discord Role Grants

The ledger of roles the extension has given or adopted. Columns: Customer (or "Deleted customer"), Discord user, Server, Role, State, Cleanup pending and Last error. Entries cannot be created, edited or deleted by hand.

State Meaning
pending_add The role was requested but Discord has not confirmed it, for example after a timeout. The entry is kept because Discord may have applied it.
active The role is in place.
pending_remove A removal was started and is not confirmed yet.

Entries that wait for cleanup, or that belong to a deleted customer, have a Retry action. It queues the cleanup job for that entry. The entry disappears once the role is removed or Discord confirms that the user is no longer a member of the server.

What the customer sees

Customers find Discord in their account menu. The page is at /account/discord and requires a login.

Not linked. The page shows "No Discord account linked yet." and the Link Discord account button. The button sends the customer to Discord's consent screen. After approval Discord sends them back, the page shows "Your Discord account is now linked." and a sync is queued. If the customer presses cancel in Discord, the page shows "Discord account linking was cancelled."

Linked. The page shows the Discord display name (or the user ID), when the account was linked, when it was last synced, and the last error if there is one. Three buttons:

Button What it does
Sync my Discord roles Runs a sync immediately and shows "Discord roles updated: n added, n removed." or the error.
Unlink After a confirmation, removes the roles the extension gave and deletes the link. Shows "Your Discord account has been unlinked. n role(s) removed."
Reconnect / change Discord account After a confirmation, starts the consent screen again. Used to give the server join consent or to switch to another Discord account.

A Discord account can be linked to one customer only, and a customer can have one Discord account. If the Discord account is already linked to another customer, linking is refused.

The extension sends no e-mails and no Discord messages.

Unlinking

Unlinking removes every role recorded in the ledger for that Discord account, in every server, including roles from disabled mappings and from mappings with removal switched off. Roles the extension did not record stay. The customer stays a member of the server.

If a removal fails or Discord cannot be read, the link is kept and marked as pending. The customer page then shows "Unlink pending. Role cleanup will retry automatically; no new roles will be granted." While an unlink is pending, every sync for that customer continues the cleanup instead of giving roles, and the Reconnect / change Discord account button is hidden. The link is deleted once all recorded roles are removed.

Changing the Discord account

When a customer reconnects and picks a different Discord account, the extension first unlinks the old account as described above. Only when that has succeeded is the new account stored and synced. If the cleanup of the old account fails, the old link stays in the pending state; once it has finished, the customer starts linking the new account again.

A Discord account that still has pending cleanup, or ledger entries of another or a deleted customer, cannot be linked until that cleanup is done.

Deleting a customer

When a customer with a linked Discord account is deleted in Paymenter, the link is deleted with them. The ledger entries stay, show "Deleted customer", and cleanup jobs remove the recorded roles in Discord. Entries that fail are retried by the 15 minute run and can be retried by hand in Discord Role Grants.