コード例 #1
0
 private Task SaveConfigAsync(ITargetConfig config, CancellationToken cancellationToken = default)
 {
     if (config is GroupConfig groupConfig)
     {
         return(_groupConfigStore.SetGroupConfigAsync(groupConfig, cancellationToken));
     }
     else if (config is UserData userData)
     {
         return(_userDataStore.SetUserDataAsync(userData, cancellationToken));
     }
     return(Task.CompletedTask);
 }
コード例 #2
0
        private async Task HandleImageCheckRequestAsync(CommandContext context, string imageUrl, bool isExplicitRequest, CancellationToken cancellationToken = default)
        {
            ITargetConfig config = await GetConfigAsync <ITargetConfig>(context.Message, cancellationToken).ConfigureAwait(false);

            if (!isExplicitRequest && !await CheckShouldCheckSizeAsync(context, config, cancellationToken).ConfigureAwait(false))
            {
                return;
            }

            using IDisposable logScope = _log.BeginScope(new Dictionary <string, object>()
            {
                { "ImageURL", imageUrl },
                { "SenderID", context.Message.SenderID.Value },
                { "RecipientID", context.Message.RecipientID },
                { "GroupName", context.IsGroup ? context.Message.RecipientID.ToString() : null }
            });

            Image img = null;

            try
            {
                img = await DownloadImageAsync(imageUrl, cancellationToken).ConfigureAwait(false);
            }
            catch (Exception ex) when(ex.LogAsError(this._log, "Failed downloading image {ImageURL}", imageUrl))
            {
                await context.ReplyTextAsync($"/alert Failed downloading image: {ex.Message}\r\nImage URL: {imageUrl}", cancellationToken).ConfigureAwait(false);

                return;
            }

            _log.LogTrace("Verifying image size");
            PictureSize size = Verify(img.Size);

            _log.LogTrace("Image size: {ImageSize}", size);

            // build message
            string response = $"Image size: {size} {GetEmoteForExpression(!size.IsTooSmall && !size.IsTooBig)}\r\n" +
                              $"Is square: {GetEmoteForExpression(size.IsSquare)}";

            if (config.PostImageURL)
            {
                response += $"\r\nImage URL: {imageUrl}";
            }

            // send the response
            await context.ReplyTextAsync(response, cancellationToken).ConfigureAwait(false);
        }
コード例 #3
0
        private async Task CmdPostUrlAsync(CommandContext context, Match regexMatch, CancellationToken cancellationToken = default)
        {
            // if is group, ensure user is admin or owner
            if (context.IsGroup)
            {
                WolfGroupMember member = await GetGroupMemberAsync(context, cancellationToken).ConfigureAwait(false);

                if (member == null)
                {
                    await context.Client.SendGroupMembersBugNoticeAsync(context.Message, cancellationToken).ConfigureAwait(false);

                    return;
                }
                if (member?.HasAdminPrivileges != true)
                {
                    await context.ReplyTextAsync("/alert You need at least admin permissions to change group config.", cancellationToken).ConfigureAwait(false);

                    return;
                }
            }

            // check how the user wants to change the setting
            SettingSwitch settingSwitch = ParseSettingSwitch(regexMatch.Groups.Count > 1 ? regexMatch.Groups[1]?.Value : null);

            if (settingSwitch == SettingSwitch.Invalid)
            {
                await context.ReplyTextAsync("/alert Invalid switch.\r\n" +
                                             "Allowed values: on, true, enable, enabled, off, false, disable, disabled, toggle", cancellationToken).ConfigureAwait(false);

                return;
            }

            // get and update config
            ITargetConfig config = await GetConfigAsync <ITargetConfig>(context.Message, cancellationToken).ConfigureAwait(false);

            config.PostImageURL = GetSwitchedValue(config.PostImageURL, settingSwitch);
            await SaveConfigAsync(config, cancellationToken).ConfigureAwait(false);

            await context.ReplyTextAsync($"/me Posting image URLs {(config.PostImageURL ? "enabled" : "disabled")}.", cancellationToken).ConfigureAwait(false);
        }
コード例 #4
0
        private async ValueTask <bool> CheckShouldCheckSizeAsync(CommandContext context, ITargetConfig config, CancellationToken cancellationToken = default)
        {
            _log.LogTrace("Determining if should check the image size");
            if (config is GroupConfig groupConfig)
            {
                // if disabled for all, can return early
                if (!groupConfig.IsEnabled || (!groupConfig.ListenUsers && !groupConfig.ListenMods && !groupConfig.ListenAdmins && !groupConfig.ListenBots))
                {
                    return(false);
                }
                // same if enabled for for all
                if (groupConfig.IsEnabled && groupConfig.ListenUsers && groupConfig.ListenMods && groupConfig.ListenAdmins && groupConfig.ListenBots)
                {
                    return(true);
                }

                // check for all permissions
                WolfGroupMember member = await GetGroupMemberAsync(context, cancellationToken).ConfigureAwait(false);

                if (groupConfig.ListenUsers && member.Capabilities == WolfGroupCapabilities.User)
                {
                    return(true);
                }
                if (groupConfig.ListenMods && member.Capabilities == WolfGroupCapabilities.Mod)
                {
                    return(true);
                }
                if (groupConfig.ListenAdmins && member.HasAdminPrivileges)
                {
                    return(true);
                }
                if (groupConfig.ListenBots)
                {
                    WolfUser user = await context.GetSenderAsync(cancellationToken).ConfigureAwait(false);

                    return(user.Device == WolfDevice.Bot);
                }

                // return false if all privilege checks failed
                return(false);
            }
            else
            {
                return(true);
            }
        }