diff --git a/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx b/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx index 1a90fc7f..8e91eb72 100644 --- a/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx +++ b/src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx @@ -824,6 +824,9 @@ 参量质变仪已准备完成 + + 权限不足,将无法为您设置 Discord Activity 状态 + 正在提瓦特大陆中探索 diff --git a/src/Snap.Hutao/Snap.Hutao/Service/Discord/DiscordController.cs b/src/Snap.Hutao/Snap.Hutao/Service/Discord/DiscordController.cs index 660b9ec8..6166c562 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/Discord/DiscordController.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/Discord/DiscordController.cs @@ -132,13 +132,6 @@ internal static class DiscordController return; } - // Actually requires a discord client to be running on Windows platform. - // If not, the following creation code will throw. - if (System.Diagnostics.Process.GetProcessesByName("Discord").Length <= 0) - { - return; - } - lock (SyncRoot) { DiscordCreateParams @params = default; diff --git a/src/Snap.Hutao/Snap.Hutao/Service/Discord/DiscordService.cs b/src/Snap.Hutao/Snap.Hutao/Service/Discord/DiscordService.cs index 91b62bc5..c977e243 100644 --- a/src/Snap.Hutao/Snap.Hutao/Service/Discord/DiscordService.cs +++ b/src/Snap.Hutao/Snap.Hutao/Service/Discord/DiscordService.cs @@ -2,6 +2,8 @@ // Licensed under the MIT license. using Snap.Hutao.Core; +using Snap.Hutao.Service.Notification; +using System.Diagnostics; namespace Snap.Hutao.Service.Discord; @@ -9,22 +11,69 @@ namespace Snap.Hutao.Service.Discord; [Injection(InjectAs.Singleton, typeof(IDiscordService))] internal sealed partial class DiscordService : IDiscordService, IDisposable { + private readonly IInfoBarService infoBarService; private readonly RuntimeOptions runtimeOptions; + private bool isInitialized; + public async ValueTask SetPlayingActivityAsync(bool isOversea) { - _ = isOversea - ? await DiscordController.SetPlayingGenshinImpactAsync().ConfigureAwait(false) - : await DiscordController.SetPlayingYuanShenAsync().ConfigureAwait(false); + if (IsSupported()) + { + _ = isOversea + ? await DiscordController.SetPlayingGenshinImpactAsync().ConfigureAwait(false) + : await DiscordController.SetPlayingYuanShenAsync().ConfigureAwait(false); + } } public async ValueTask SetNormalActivityAsync() { - _ = await DiscordController.SetDefaultActivityAsync(runtimeOptions.AppLaunchTime).ConfigureAwait(false); + if (IsSupported()) + { + _ = await DiscordController.SetDefaultActivityAsync(runtimeOptions.AppLaunchTime).ConfigureAwait(false); + } } public void Dispose() { DiscordController.Stop(); } + + private bool IsSupported() + { + try + { + // Actually requires a discord client to be running on Windows platform. + // If not, discord core creation code will throw. + Process[] discordProcesses = Process.GetProcessesByName("Discord"); + + if (discordProcesses.Length <= 0) + { + return false; + } + + foreach (Process process in discordProcesses) + { + try + { + _ = process.Handle; + } + catch (Exception) + { + if (!isInitialized) + { + infoBarService.Warning(SH.ServiceDiscordActivityElevationRequiredHint); + } + + return false; + } + } + + return true; + } + finally + { + isInitialized = true; + } + } } \ No newline at end of file