Пример #1
0
        public async Task InvokeTagAsync(ulong guildId, ulong channelId, string name)
        {
            var tag = await _hiromiContext
                      .Tags
                      .Where(x => x.GuildId == guildId)
                      .Where(x => x.Name == name)
                      .FirstOrDefaultAsync();

            var messageChannel = _discordSocketClient.GetChannel(channelId) as IMessageChannel;

            // Check if the channel is recorded
            var channel = await _hiromiContext
                          .Channels
                          .Where(x => x.GuildId == guildId)
                          .Where(x => x.ChannelId == channelId)
                          .FirstOrDefaultAsync();

            if (channel is null)
            {
                channel = new Channel
                {
                    GuildId      = guildId,
                    ChannelId    = channelId,
                    AllowTags    = true,
                    IsLogChannel = false
                };

                _hiromiContext.Add(channel);
                await _hiromiContext.SaveChangesAsync();
            }

            if (!channel.AllowTags)
            {
                return;
            }

            // If the Tag is not found, show similar matches
            if (tag is null)
            {
                var tags = await GetTagSummaryMatches(guildId, name);

                var embed = TagViews.FormatSimilarTags(name, tags);
                await messageChannel.SendMessageAsync(embed : embed);

                return;
            }



            await messageChannel.SendMessageAsync(tag.Content, allowedMentions : AllowedMentions.None);

            tag.Uses++;
            await _hiromiContext.SaveChangesAsync();
        }
Пример #2
0
        public async Task CreateReminderAsync(ulong userId, ulong guildId, string message, TimeSpan duration)
        {
            var reminder = new Reminder
            {
                UserId      = userId,
                GuildId     = guildId,
                Message     = message,
                TimeInvoked = DateTime.Now,
                Duration    = duration
            };

            _hiromiContext.Add(reminder);
            await _hiromiContext.SaveChangesAsync();

            var timer = new Timer(async _ => await HandleReminderCallbackAsync(reminder),
                                  null,
                                  reminder.Duration,
                                  Timeout.InfiniteTimeSpan);

            _reminders.TryAdd(reminder.Id, timer);
        }
Пример #3
0
        public async Task StoreCommandsInDbAsync(ulong guildId, ulong channelId, IEnumerable <string> commands)
        {
            var channel = await _hiromiContext
                          .Channels
                          .Where(x => x.GuildId == guildId)
                          .Where(x => x.ChannelId == channelId)
                          .FirstOrDefaultAsync();

            if (channel is null)
            {
                _hiromiContext.Add(new Channel
                {
                    GuildId      = guildId,
                    ChannelId    = channelId,
                    IsLogChannel = false
                });

                await _hiromiContext.SaveChangesAsync();
            }

            var current = await _hiromiContext
                          .Channels
                          .Where(x => x.GuildId == guildId)
                          .Where(x => x.ChannelId == channelId)
                          .FirstOrDefaultAsync();

            foreach (var command in commands)
            {
                if (current.Commands.Contains(command))
                {
                    continue;
                }

                current.Commands.Add(command);
            }

            await _hiromiContext.SaveChangesAsync();
        }
Пример #4
0
        public async Task Handle(MessageReceivedNotification notification, CancellationToken cancellationToken)
        {
#if !DEBUG
            var received = notification.Message;

            var message = new Message
            {
                MessageId = received.Id,
                UserId    = received.Author.Id,
                ChannelId = received.Channel.Id,
                GuildId   = (received.Channel as IGuildChannel) !.Guild.Id
            };

            _hiromiContext.Add(message);
            await _hiromiContext.SaveChangesAsync(cancellationToken);
#endif
        }
Пример #5
0
        public async Task Handle(JoinedGuildNotification notification, CancellationToken cancellationToken)
        {
            var guild = notification.Guild;

            var current = await _hiromiContext
                          .Guilds
                          .FirstOrDefaultAsync(x => x.GuildId == notification.Guild.Id, cancellationToken);

            if (current != null)
            {
                return;
            }

            _hiromiContext.Add(new Guild
            {
                GuildId = guild.Id
            });

            await _hiromiContext.SaveChangesAsync(cancellationToken);
        }
Пример #6
0
        public async Task Handle(MessageReceivedNotification notification, CancellationToken cancellationToken)
        {
            if (!(notification.Message is IUserMessage message) ||
                !(message.Author is IGuildUser user) ||
                !(message.Channel is IGuildChannel currentChannel))
            {
                return;
            }

            var guildId = currentChannel.Guild.Id;

            var guild = await _hiromiContext
                        .Guilds
                        .FirstOrDefaultAsync(x => x.GuildId == guildId, cancellationToken);

            if (guild is null)
            {
                _hiromiContext.Add(new Guild
                {
                    GuildId = guildId,
                });

                await _hiromiContext.SaveChangesAsync(cancellationToken);
            }

            var matches = _messageLinkRegex.Matches(message.Content);

            foreach (Match match in matches)
            {
                if (ulong.TryParse(match.Groups["GuildId"].Value, out _) &&
                    ulong.TryParse(match.Groups["ChannelId"].Value, out var channelId) &&
                    ulong.TryParse(match.Groups["MessageId"].Value, out var messageId))
                {
                    var channel = _discordSocketClient.GetChannel(channelId);
                    if (channel is IGuildChannel guildChannel && channel is ISocketMessageChannel messageChannel)
                    {
                        var botUser = await guildChannel.Guild.GetCurrentUserAsync();

                        var botChannelPermissions  = botUser.GetPermissions(guildChannel);
                        var userChannelPermissions = user.GetPermissions(guildChannel);

                        if (!botChannelPermissions.ViewChannel ||
                            !userChannelPermissions.ViewChannel ||
                            guildChannel.GuildId != (message.Channel as IGuildChannel).GuildId)
                        {
                            return;
                        }

                        var cacheMode = botChannelPermissions.ReadMessageHistory
                            ? CacheMode.AllowDownload
                            : CacheMode.CacheOnly;

                        var retrievedMessage = await messageChannel.GetMessageAsync(messageId, cacheMode);

                        if (retrievedMessage != null)
                        {
                            await message.Channel.SendMessageAsync(embed : CreateEmbed(user, retrievedMessage));
                        }
                    }
                }
            }
        }