Guild and Role Middleware

The guilds and guild_roles options are checked once, when the user logs in. The discord.guild and discord.role middleware check them on every request, so access ends when the user leaves a guild or loses a role.

Guilds

The user must be a member of at least one of the given guilds.

routes/web.php
Route::middleware(['auth', 'discord.guild:111111111111111111'])->group(function () {
    // ...
});
 
// Member of either guild.
Route::middleware(['auth', 'discord.guild:111111111111111111,222222222222222222'])->get('/community', ...);

To require several guilds, add the middleware once for each guild.

Without arguments, the guilds and guilds_strict options are used.

Requires the guilds scope.

Roles

The first argument is the guild. The user must have at least one of the roles that follow it.

routes/web.php
// Role 333 or 444 in guild 111.
Route::middleware(['auth', 'discord.role:111111111111111111,333333333333333333,444444444444444444'])->get('/staff', ...);

Without arguments, the guild_roles option is used: the user needs at least one of the listed roles in every listed guild.

Requires the guilds and guilds.members.read scopes.

Responses

SituationResponse
The user is not logged inRedirected to the login route, or 401 for JSON requests
The user has no linked Discord account403 with the missing_discord_account message
Not a member of the guild403 with the not_member_guild_only message
Missing the role403 with the missing_role message
Discord returned an error403 with the authorization_failed_guilds or authorization_failed_roles message

With APP_DEBUG enabled, the reason for a Discord error is added to the message.

Caching

Each request would otherwise call the Discord API, so the user's guilds and roles are cached for 5 minutes. A user who leaves a guild keeps access until the cache expires. Logging in again or unlinking the account clears the cache.

config/larascord.php
'access_cache' => [
    'ttl' => 300,
    'store' => env('LARASCORD_CACHE_STORE'),
],

Errors from Discord are not cached.

In code

The InteractsWithDiscord trait uses the same cached checks, which is useful in gates and policies.

app/Providers/AppServiceProvider.php
Gate::define('moderate', fn (User $user) => $user->hasDiscordRole('111111111111111111', '333333333333333333'));
MethodDescription
inDiscordGuild(string ...$guildIds): boolWhether the user is a member of at least one of the guilds.
hasDiscordRole(string $guildId, string ...$roleIds): boolWhether the user has at least one of the roles in the guild.

Both throw an exception when the user has no linked Discord account or Discord returns an error.