Compare commits

...

1 Commits

Author SHA1 Message Date
Toutsu 2942da0c35 fix(discord): use GuildInteractionUser.Permissions instead of REST guild lookup
PR Checks / test-and-build (pull_request) Successful in 11m25s
Replace REST GetGuildAsync/GetGuildUserAsync calls with authoritative
member.Permissions from the slash-command interaction payload. Discord
already resolves channel/guild permissions in the interaction JSON, so
we no longer need to fetch the guild via REST (which returns 404 when
the bot is not a REST member of the guild, e.g. user-installed apps).

Keep a best-effort GetGuildAsync call only to obtain OwnerId for the
permission checker fallback, swallowing 404 silently.

Bump version → 3.0.7

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-26 10:44:59 +03:00
7 changed files with 56 additions and 54 deletions
+1 -1
View File
@@ -6,7 +6,7 @@ on:
- main - main
env: env:
VERSION: 3.0.6 VERSION: 3.0.7
jobs: jobs:
# ЧАСТЬ 1: Собираем образы и кладем в Gitea (чтобы делиться с ребятами) # ЧАСТЬ 1: Собираем образы и кладем в Gitea (чтобы делиться с ребятами)
+1 -1
View File
@@ -1,6 +1,6 @@
<Project> <Project>
<PropertyGroup> <PropertyGroup>
<Version>3.0.6</Version> <Version>3.0.7</Version>
<TargetFramework>net10.0</TargetFramework> <TargetFramework>net10.0</TargetFramework>
<LangVersion>preview</LangVersion> <LangVersion>preview</LangVersion>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
+3 -3
View File
@@ -49,7 +49,7 @@ services:
crond -f crond -f
bot: bot:
image: git.codeanddice.ru/toutsu/gmrelay-bot:3.0.6 image: git.codeanddice.ru/toutsu/gmrelay-bot:3.0.7
restart: always restart: always
depends_on: depends_on:
db: db:
@@ -67,7 +67,7 @@ services:
retries: 3 retries: 3
discord: discord:
image: git.codeanddice.ru/toutsu/gmrelay-discord-bot:3.0.6 image: git.codeanddice.ru/toutsu/gmrelay-discord-bot:3.0.7
restart: always restart: always
depends_on: depends_on:
db: db:
@@ -84,7 +84,7 @@ services:
retries: 3 retries: 3
web: web:
image: git.codeanddice.ru/toutsu/gmrelay-web:3.0.6 image: git.codeanddice.ru/toutsu/gmrelay-web:3.0.7
restart: always restart: always
depends_on: depends_on:
db: db:
@@ -1,4 +1,5 @@
using GmRelay.DiscordBot.Rendering; using GmRelay.DiscordBot.Rendering;
using NetCord;
using NetCord.Rest; using NetCord.Rest;
using NetCord.Services.ApplicationCommands; using NetCord.Services.ApplicationCommands;
@@ -24,8 +25,24 @@ public class DiscordNewSessionCommand : ApplicationCommandModule<SlashCommandCon
{ {
var guildId = Context.Interaction.GuildId var guildId = Context.Interaction.GuildId
?? throw new InvalidOperationException("This command can only be used in a guild."); ?? throw new InvalidOperationException("This command can only be used in a guild.");
var guild = await Context.Client.Rest.GetGuildAsync(guildId);
var member = await Context.Client.Rest.GetGuildUserAsync(guildId, Context.User.Id); var member = Context.User as GuildInteractionUser
?? throw new InvalidOperationException("Guild member data not available in interaction.");
var resolvedPermissions = (ulong)member.Permissions;
ulong guildOwnerId = 0;
try
{
var guild = await Context.Client.Rest.GetGuildAsync(guildId);
guildOwnerId = guild.OwnerId;
}
catch (RestException ex) when (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
{
_logger.LogWarning(
ex,
"Bot is not a REST member of guild {GuildId}; using resolved permissions from interaction payload",
guildId);
}
var timeResult = DiscordNewSessionHandler.ParseTimeInput(time); var timeResult = DiscordNewSessionHandler.ParseTimeInput(time);
if (!timeResult.IsSuccess) if (!timeResult.IsSuccess)
@@ -35,17 +52,15 @@ public class DiscordNewSessionCommand : ApplicationCommandModule<SlashCommandCon
return; return;
} }
var resolvedPermissions = GetResolvedPermissions(guild, member);
try try
{ {
var view = await _handler.HandleAsync( var view = await _handler.HandleAsync(
guildId: guild.Id.ToString(), guildId: guildId.ToString(),
channelId: Context.Channel.Id.ToString(), channelId: Context.Channel.Id.ToString(),
userId: Context.User.Id, userId: Context.User.Id,
userDisplayName: Context.User.GlobalName ?? Context.User.Username, userDisplayName: Context.User.GlobalName ?? Context.User.Username,
resolvedPermissions: resolvedPermissions, resolvedPermissions: resolvedPermissions,
guildOwnerId: guild.OwnerId, guildOwnerId: guildOwnerId,
title: title, title: title,
scheduledAt: timeResult.Value, scheduledAt: timeResult.Value,
maxPlayers: seats is null ? null : (int)seats.Value, maxPlayers: seats is null ? null : (int)seats.Value,
@@ -66,23 +81,9 @@ public class DiscordNewSessionCommand : ApplicationCommandModule<SlashCommandCon
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Failed to create session for user {UserId} in guild {GuildId}", Context.User.Id, guild.Id); _logger.LogError(ex, "Failed to create session for user {UserId} in guild {GuildId}", Context.User.Id, guildId);
await Context.Interaction.SendResponseAsync( await Context.Interaction.SendResponseAsync(
InteractionCallback.Message(":boom: An error occurred while creating the session.")); InteractionCallback.Message(":boom: An error occurred while creating the session."));
} }
} }
private static ulong GetResolvedPermissions(NetCord.Rest.RestGuild guild, NetCord.GuildUser member)
{
if (member is null)
return 0;
ulong resolved = 0;
foreach (var roleId in member.RoleIds)
{
if (guild.Roles.TryGetValue(roleId, out var role))
resolved |= (ulong)role.Permissions;
}
return resolved;
}
} }
@@ -1,5 +1,6 @@
namespace GmRelay.DiscordBot.Features.Sessions; namespace GmRelay.DiscordBot.Features.Sessions;
using NetCord;
using NetCord.Rest; using NetCord.Rest;
using NetCord.Services.ApplicationCommands; using NetCord.Services.ApplicationCommands;
@@ -24,8 +25,24 @@ public class DiscordRescheduleCommand : ApplicationCommandModule<SlashCommandCon
{ {
var guildId = Context.Interaction.GuildId var guildId = Context.Interaction.GuildId
?? throw new InvalidOperationException("This command can only be used in a guild."); ?? throw new InvalidOperationException("This command can only be used in a guild.");
var guild = await Context.Client.Rest.GetGuildAsync(guildId);
var member = await Context.Client.Rest.GetGuildUserAsync(guildId, Context.User.Id); var member = Context.User as GuildInteractionUser
?? throw new InvalidOperationException("Guild member data not available in interaction.");
var resolvedPermissions = (ulong)member.Permissions;
ulong guildOwnerId = 0;
try
{
var guild = await Context.Client.Rest.GetGuildAsync(guildId);
guildOwnerId = guild.OwnerId;
}
catch (RestException ex) when (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
{
_logger.LogWarning(
ex,
"Bot is not a REST member of guild {GuildId}; using resolved permissions from interaction payload",
guildId);
}
if (!Guid.TryParse(sessionIdText, out var sessionId)) if (!Guid.TryParse(sessionIdText, out var sessionId))
{ {
@@ -66,17 +83,15 @@ public class DiscordRescheduleCommand : ApplicationCommandModule<SlashCommandCon
return; return;
} }
var resolvedPermissions = GetResolvedPermissions(guild, member);
try try
{ {
var result = await _handler.HandleAsync( var result = await _handler.HandleAsync(
guildId: guild.Id.ToString(), guildId: guildId.ToString(),
channelId: Context.Channel.Id.ToString(), channelId: Context.Channel.Id.ToString(),
userId: Context.User.Id, userId: Context.User.Id,
userDisplayName: Context.User.GlobalName ?? Context.User.Username, userDisplayName: Context.User.GlobalName ?? Context.User.Username,
resolvedPermissions: resolvedPermissions, resolvedPermissions: resolvedPermissions,
guildOwnerId: guild.OwnerId, guildOwnerId: guildOwnerId,
sessionId: sessionId, sessionId: sessionId,
options: parsedOptions, options: parsedOptions,
deadline: deadlineResult.Value, deadline: deadlineResult.Value,
@@ -103,18 +118,4 @@ public class DiscordRescheduleCommand : ApplicationCommandModule<SlashCommandCon
InteractionCallback.Message(":boom: Ошибка при запуске голосования.")); InteractionCallback.Message(":boom: Ошибка при запуске голосования."));
} }
} }
private static ulong GetResolvedPermissions(NetCord.Rest.RestGuild guild, NetCord.GuildUser member)
{
if (member is null)
return 0;
ulong resolved = 0;
foreach (var roleId in member.RoleIds)
{
if (guild.Roles.TryGetValue(roleId, out var role))
resolved |= (ulong)role.Permissions;
}
return resolved;
}
} }
@@ -73,7 +73,7 @@
</button> </button>
</form> </form>
<div class="nav-version">v3.0.6</div> <div class="nav-version">v3.0.7</div>
</div> </div>
</Authorized> </Authorized>
<NotAuthorized> <NotAuthorized>
@@ -61,7 +61,7 @@ public sealed class DiscordProjectStructureTests
var prChecks = File.ReadAllText(Path.Combine(repoRoot, ".gitea", "workflows", "pr-checks.yml")); var prChecks = File.ReadAllText(Path.Combine(repoRoot, ".gitea", "workflows", "pr-checks.yml"));
var deploy = File.ReadAllText(Path.Combine(repoRoot, ".gitea", "workflows", "deploy.yml")); var deploy = File.ReadAllText(Path.Combine(repoRoot, ".gitea", "workflows", "deploy.yml"));
Assert.Contains("gmrelay-discord-bot:3.0.6", compose); Assert.Contains("gmrelay-discord-bot:3.0.7", compose);
Assert.Contains("Discord__Token=${DISCORD_BOT_TOKEN:?Set DISCORD_BOT_TOKEN in .env}", compose); Assert.Contains("Discord__Token=${DISCORD_BOT_TOKEN:?Set DISCORD_BOT_TOKEN in .env}", compose);
Assert.Contains("src/GmRelay.DiscordBot/Dockerfile", deploy); Assert.Contains("src/GmRelay.DiscordBot/Dockerfile", deploy);
Assert.Contains("DISCORD_BOT_TOKEN", deploy); Assert.Contains("DISCORD_BOT_TOKEN", deploy);
@@ -75,13 +75,13 @@ public sealed class DiscordProjectStructureTests
{ {
var repoRoot = GetRepoRoot(); var repoRoot = GetRepoRoot();
Assert.Contains("<Version>3.0.6</Version>", File.ReadAllText(Path.Combine(repoRoot, "Directory.Build.props"))); Assert.Contains("<Version>3.0.7</Version>", File.ReadAllText(Path.Combine(repoRoot, "Directory.Build.props")));
Assert.Contains("VERSION: 3.0.6", File.ReadAllText(Path.Combine(repoRoot, ".gitea", "workflows", "deploy.yml"))); Assert.Contains("VERSION: 3.0.7", File.ReadAllText(Path.Combine(repoRoot, ".gitea", "workflows", "deploy.yml")));
Assert.Contains("gmrelay-bot:3.0.6", File.ReadAllText(Path.Combine(repoRoot, "compose.yaml"))); Assert.Contains("gmrelay-bot:3.0.7", File.ReadAllText(Path.Combine(repoRoot, "compose.yaml")));
Assert.Contains("gmrelay-web:3.0.6", File.ReadAllText(Path.Combine(repoRoot, "compose.yaml"))); Assert.Contains("gmrelay-web:3.0.7", File.ReadAllText(Path.Combine(repoRoot, "compose.yaml")));
Assert.Contains("gmrelay-discord-bot:3.0.6", File.ReadAllText(Path.Combine(repoRoot, "compose.yaml"))); Assert.Contains("gmrelay-discord-bot:3.0.7", File.ReadAllText(Path.Combine(repoRoot, "compose.yaml")));
Assert.Contains( Assert.Contains(
"v3.0.6", "v3.0.7",
File.ReadAllText(Path.Combine(repoRoot, "src", "GmRelay.Web", "Components", "Layout", "NavMenu.razor"))); File.ReadAllText(Path.Combine(repoRoot, "src", "GmRelay.Web", "Components", "Layout", "NavMenu.razor")));
} }