#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
        public async Task UpdateAsync()
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
        {
            DiscordServerInfo info = Program.Replicant.AvailableServers[Context.Guild.Id];

            Program.Replicant.ReadSinceTimestamp(Context.Guild, info.LastMessageReceived, Context.Channel);
        }
        public async Task ClearUserAsync()
        {
            DiscordServerInfo guild = Program.Replicant.AvailableServers[Context.Guild.Id];

            guild.TargetUserId = null;
            guild.Locked       = true;
            guild.ClearMessages();
            await ReplyAsync("Cleared the replicated user");
        }
        public async void ReadSinceTimestamp(SocketGuild guild, DateTime lastReceivedTime, ISocketMessageChannel replyChannel)
        {
            await BotLog(new LogMessage(LogSeverity.Info, "Replicator", "Reading new messages since last login"));

            DiscordServerInfo serverInfo = AvailableServers[guild.Id];

            serverInfo.Locked = true;
            foreach (var channel in guild.TextChannels)
            {
                await replyChannel.SendMessageAsync($"Reading new messages in channel {channel.Mention}");

                ChannelPermissions permissions = guild.GetUser(_client.CurrentUser.Id).GetPermissions(channel);
                if (permissions.ReadMessageHistory && permissions.ViewChannel)
                {
                    var      lastMessage = (await channel.GetMessagesAsync(1).Flatten().ToList())[0];
                    DateTime currentTime = lastMessage.Timestamp.UtcDateTime;
                    if (lastMessage != null)
                    {
                        while (currentTime >= lastReceivedTime)
                        {
                            var messageCollection = await channel.GetMessagesAsync(lastMessage, Direction.Before, 10).Flatten().ToList();

                            foreach (var message in messageCollection)
                            {
                                if (string.IsNullOrEmpty(message.Content) || message.Content[0] != '!')
                                {
                                    lastMessage = message;
                                    currentTime = lastMessage.Timestamp.UtcDateTime;
                                    if (currentTime >= lastReceivedTime)
                                    {
                                        break;
                                    }
                                    if (serverInfo.AvailableMessages.ContainsKey(lastMessage.Id))
                                    {
                                        break;
                                    }
                                    serverInfo.GuildTotalMessages += 1;
                                    if (TestValidMessage(message, serverInfo))
                                    {
                                        serverInfo.AvailableMessages.Add(message.Id, message.Content);
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    await replyChannel.SendMessageAsync($"No permission to read in channel {channel.Mention}");
                }
            }
            await replyChannel.SendMessageAsync("Read all new messages");

            serverInfo.Locked = false;
            serverInfo.LastMessageReceived = DateTime.UtcNow;
            serverInfo.Flush();
        }
        public async Task SetUserAsync(IUser targetUser)
        {
            DiscordServerInfo guild = Program.Replicant.AvailableServers[Context.Guild.Id];

            guild.TargetUserId = targetUser.Id;
            guild.Enabled      = false;
            guild.ClearMessages();
            await ReplyAsync($"Set the target replicated player to {Context.Guild.GetUser(targetUser.Id).Nickname}");
            await ReplyAsync("Will require a full re-read of server messages to activate (!replicator readall)");
        }
 public DiscordServerInfoFields(DiscordServerInfo server)
 {
     GuildId             = server.GuildId;
     TargetUserId        = server.TargetUserId;
     DisabledUserIds     = server.DisabledUserIds;
     DisabledSubstrings  = server.DisabledSubstrings;
     AvailableMessages   = server.AvailableMessages;
     LastMessageReceived = server.LastMessageReceived;
     AutoUpdateMessages  = server.AutoUpdateMessages;
     Enabled             = server.Enabled;
     GuildTotalMessages  = server.GuildTotalMessages;
 }
示例#6
0
            public async Task GetInfo()
            {
                DiscordServerInfo info    = Program.Replicant.AvailableServers[Context.Guild.Id];
                string            builder = $"Replicated User: {(info.TargetUserId == null ? "None" : Context.Guild.GetUser((ulong)info.TargetUserId).Nickname)}\n";

                builder += $"Enabled: {info.Enabled}\n";
                builder += $"Probability: {info.Proability}\n";
                builder += $"Server messages: {info.GuildTotalMessages}\n";
                builder += $"Target messages: {info.TargetTotalMessages}\n";
                builder += $"Last message time: {info.LastMessageReceived} UTC\n";
                await ReplyAsync(builder);
            }
 private bool TestValidMessage(IMessage message, DiscordServerInfo serverInfo)
 {
     if (message.Author.Id == serverInfo.TargetUserId)
     {
         if (message.MentionedUserIds.Count == 0 && message.MentionedRoleIds.Count == 0 && message.MentionedChannelIds.Count == 0)
         {
             if (!message.Content.Contains(GlobalDisabledSubstrings))
             {
                 if (message.Embeds.Count == 0)
                 {
                     if (!message.Content.Contains(serverInfo.DisabledSubstrings))
                     {
                         return(true);
                     }
                 }
             }
         }
     }
     return(false);
 }