Esempio n. 1
0
        public async Task About()
        {
            _loggerService.LogInformation("Received 'about' command from user '{User}'", Context.User.ToString());

            var uptime            = _clock.GetCurrentInstant() - Process.GetCurrentProcess().StartTime.ToUniversalTime().ToInstant();
            var subscriptionCount = await _subscriptionQueries.GetAllAsync().CountAsync();

            var embed = EmbedHelper.About(Context.Client.Guilds.Count, subscriptionCount, uptime);

            if (Context.IsPrivate)
            {
                await Context.User.SendMessageAsync(embed : embed);

                return;
            }

            await Context.Channel.SendMessageAsync(embed : embed);
        }
Esempio n. 2
0
        protected override async Task Handle(IssueChangedPriorityIntegrationEvent command)
        {
            var embed = EmbedHelper.PriorityChanged(command.Issue, command.PreviousPriority);

            await foreach (var(channelId, nexusModsGameId, nexusModsModId, _, _) in _subscriptionQueries.GetAllAsync())
            {
                if (await _discordClient.GetChannelAsync(channelId) is not IMessageChannel channel)
                {
                    continue;
                }
                if (nexusModsGameId != command.Issue.NexusModsGameId || nexusModsModId != command.Issue.NexusModsModId)
                {
                    continue;
                }
                await channel.SendMessageAsync(embed : embed);
            }
        }
        protected override async Task Handle(CommentRemovedReplyIntegrationEvent command)
        {
            var embed = EmbedHelper.DeletedCommentReply(command.Comment, command.Reply);

            await foreach (var(channelId, nexusModsGameId, nexusModsModId, _, _) in _subscriptionQueries.GetAllAsync())
            {
                if (await _discordClient.GetChannelAsync(channelId) is not IMessageChannel channel)
                {
                    continue;
                }
                if (nexusModsGameId != command.Comment.NexusModsGameId || nexusModsModId != command.Comment.NexusModsModId)
                {
                    continue;
                }
                await channel.SendMessageAsync(embed : embed);
            }
        }
        protected override async Task Handle(CommentChangedIsLockedIntegrationEvent command)
        {
            var embed = AttachmentHelper.IsLockedChanged(command.Comment);

            await foreach (var(channelId, nexusModsGameId, nexusModsModId, _, _) in _subscriptionQueries.GetAllAsync())
            {
                if (await _slackBot.GetConversationById(channelId) is not {
                } channel)
                {
                    continue;
                }
                if (nexusModsGameId != command.Comment.NexusModsGameId || nexusModsModId != command.Comment.NexusModsModId)
                {
                    continue;
                }
                await _slackBot.Send(new BotMessage { Conversation = new ConversationByRef(channel), Attachments = { embed } });
            }
        }
Esempio n. 5
0
        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);
                }
            }
        }