Exemple #1
0
        //private static async void ClearSubscriptionsOnElapsed(object sender, ElapsedEventArgs e)
        //{
        //    Client.Logger.LogDebug(BotLoggerEvents.Timers, $"ClearSubscriptionsOnElapsed running");

        //    for (int i = 0; i < Subscriber.Subscribers.Count; ++i)
        //    {
        //        var sub = Subscriber.Subscribers.Values.ToArray()[i];
        //        if (DateTime.Now > sub.SubscriptionEnd)
        //        {
        //            try
        //            {
        //                var guild = Client.Guilds[Bot.BotSettings.Guild];
        //                var member = await guild.GetMemberAsync(sub.Member);
        //                try
        //                {
        //                    if (member != null)
        //                    {
        //                        await member.SendMessageAsync("Ваша подписка истекла :cry:");
        //                    }
        //                }
        //                catch (NotFoundException) { }
        //                catch (ArgumentException) { }

        //                try
        //                {
        //                    await DonatorCommands.DeletePrivateRoleAsync(guild, member.Id);
        //                }
        //                catch (Exceptions.NotFoundException) { }

        //                Subscriber.Subscribers.Remove(sub.Member);
        //                Subscriber.Save(Bot.BotSettings.SubscriberXML);
        //            }
        //            catch (Exception ex)
        //            {
        //                Client.Logger.LogError(BotLoggerEvents.Timers, ex, $"Возникла ошибка при очистке подписок.");
        //            }
        //        }
        //    }
        //}

        private static async void DeleteShipsOnElapsed(object sender, ElapsedEventArgs e)
        {
            Client.Logger.LogDebug(BotLoggerEvents.Timers, $"DeleteShipsOnElapsed running");

            foreach (var ship in PrivateShip.GetAll())
            {
                if ((DateTime.Now - ship.LastUsed).Days >= 3)
                {
                    try
                    {
                        var guild = await Client.GetGuildAsync(Bot.BotSettings.Guild);

                        try
                        {
                            var channel = guild.GetChannel(ship.Channel);
                            await channel.DeleteAsync();
                        }
                        catch (NotFoundException)
                        {
                        }

                        try
                        {
                            var member = await guild.GetMemberAsync(ship.GetCaptain().MemberId);

                            await member.SendMessageAsync($"Твой корабль **{ship.Name}** был удалён из-за неактивности.");
                        }
                        catch (UnauthorizedException)
                        {
                        }
                        catch (NotFoundException)
                        {
                        }

                        PrivateShip.Delete(ship.Name);

                        await guild.GetChannel(Bot.BotSettings.ModlogChannel).SendMessageAsync("**Удаление корабля**\n\n" +
                                                                                               $"**Модератор:** {Client.CurrentUser}\n" +
                                                                                               $"**Название:** {ship.Name}\n" +
                                                                                               $"**Дата:** {DateTime.Now}");
                    }
                    catch (NullReferenceException ex)
                    {
                        Client.Logger.LogError(BotLoggerEvents.Timers, $"DeleteShipsOnElapsed Ship: {ship.Name} Channel: {ship.Channel} {ex.Message}");
                    }
                }
            }
        }