Exemple #1
0
        public async Task RateLimits()
        {
            _loggerService.LogInformation("Received 'ratelimits' command from user '{User}'", Context.User.ToString());

            var rateLimit = await _rateLimitQueries.GetAsync();

            if (rateLimit is null)
            {
                await Context.User.SendMessageAsync("Failed to get Rate Limits!");

                return;
            }

            var embed = EmbedHelper.RateLimits(rateLimit);

            if (Context.IsPrivate)
            {
                await Context.User.SendMessageAsync(embed : embed);
            }
            else
            {
                await Context.Channel.SendMessageAsync(embed : embed);
            }
        }
        private async void Bot_OnMessageReceived(object?sender, IMessage message)
        {
            if (!message.Conversation.IsChannel)
            {
                return;
            }

            const string prefix = "!nmm ";

            if (message.Text.StartsWith(prefix))
            {
                var command = message.Text.Remove(0, prefix.Length);

                const string subscribe = "subscribe ";
                if (command.StartsWith("subscribe "))
                {
                    var argsText = command.Remove(0, subscribe.Length);
                    var args     = argsText.Split(' ', StringSplitOptions.RemoveEmptyEntries);
                    if (args.Length == 2 && uint.TryParse(args[0], out var gameId) && uint.TryParse(args[1], out var modId))
                    {
                        if (await _mediator.Send(new SubscribeCommand(message.Conversation.Id, gameId, modId)))
                        {
                            await message.ReplyWith("Successful!", true);

                            return;
                        }
                        else
                        {
                            await message.ReplyWith("Failed!", true);

                            return;
                        }
                    }
                    if (args.Length == 1)
                    {
                        if (await _mediator.Send(new Subscribe2Command(message.Conversation.Id, args[0])))
                        {
                            await message.ReplyWith("Successful!", true);

                            return;
                        }
                        else
                        {
                            await message.ReplyWith("Failed!", true);

                            return;
                        }
                    }
                    else
                    {
                        await message.ReplyWith("Failed!", true);

                        return;
                    }
                }

                const string unsubscribe = "unsubscribe ";
                if (command.StartsWith(unsubscribe))
                {
                    var argsText = command.Remove(0, unsubscribe.Length);
                    var args     = argsText.Split(' ', StringSplitOptions.RemoveEmptyEntries);
                    if (args.Length == 2 && uint.TryParse(args[0], out var gameId) && uint.TryParse(args[1], out var modId))
                    {
                        if (await _mediator.Send(new UnsubscribeCommand(message.Conversation.Id, gameId, modId)))
                        {
                            await message.ReplyWith("Successful!", true);

                            return;
                        }
                        else
                        {
                            await message.ReplyWith("Failed!", true);

                            return;
                        }
                    }
                    if (args.Length == 1)
                    {
                        if (await _mediator.Send(new Unsubscribe2Command(message.Conversation.Id, args[0])))
                        {
                            await message.ReplyWith("Successful!", true);

                            return;
                        }
                        else
                        {
                            await message.ReplyWith("Failed!", true);

                            return;
                        }
                    }
                    else
                    {
                        await message.ReplyWith("Failed!", true);

                        return;
                    }
                }

                const string about = "about";
                if (command.StartsWith(about))
                {
                    var uptime            = _clock.GetCurrentInstant() - Process.GetCurrentProcess().StartTime.ToUniversalTime().ToInstant();
                    var subscriptionCount = await _subscriptionQueries.GetAllAsync().CountAsync();

                    var embed = AttachmentHelper.About(subscriptionCount, uptime);
                    await message.ReplyWith(new BotMessage { Attachments = { embed } }, true);
                }

                const string subscriptions = "subscriptions";
                if (command.StartsWith(subscriptions))
                {
                    var subscriptionList = await _subscriptionQueries.GetAllAsync().Where(s => s.ChannelId == message.Conversation.Id).ToImmutableArrayAsync();

                    if (subscriptionList.Length > 0)
                    {
                        await message.ReplyWith($@"Subscriptions:
```
{string.Join('\n', subscriptionList.Select(s => $"Game: {s.NexusModsGameId}; Mod: {s.NexusModsModId}"))}
```", true);
                    }
                    else
                    {
                        await message.ReplyWith("No subscriptions found!", true);
                    }
                }

                const string ratelimits = "ratelimits";
                if (command.StartsWith(ratelimits))
                {
                    var rateLimit = await _rateLimitQueries.GetAsync();

                    if (rateLimit is null)
                    {
                        await message.ReplyWith("Failed to get Rate Limits!", true);

                        return;
                    }

                    var embed = AttachmentHelper.RateLimits(rateLimit);
                    await message.ReplyWith(new BotMessage { Attachments = { embed } });
                }

                const string authorize = "authorize";
                if (command.StartsWith(authorize))
                {
                    var isAuthorized = await _authorizationQueries.IsAuthorizedAsync();

                    if (isAuthorized)
                    {
                        await message.ReplyWith("Already authorized!", true);

                        return;
                    }

                    var uuid = Guid.NewGuid();
                    await using var ssoAuthorizationHandler = await _mediator.Send(new SSOAuthorizeCommand (uuid));

                    ssoAuthorizationHandler.OnReadyAsync += async() =>
                    {
                        var applicationSlug = "vortex";
                        await message.ReplyWith($"https://www.nexusmods.com/sso?id={uuid}&application={applicationSlug}", true);
                    };
                    ssoAuthorizationHandler.OnErrorAsync += async() =>
                    {
                        await message.ReplyWith("Failed!", true);
                    };
                    ssoAuthorizationHandler.OnAuthorizedAsync += async() =>
                    {
                        await message.ReplyWith("Successful!", true);
                    };
                    await ssoAuthorizationHandler;
                }

                const string help = "help";
                if (command.StartsWith(help))
                {
                    await message.ReplyWith(@"help
about
subscriptions
subscribe [Game Id] [Mod Id]
unsubscribe [Game Id] [Mod Id]
subscribe [Mod Url]
unsubscribe [Mod Url]
ratelimits
authorize", true);
                }
            }
        }