Code cleanup

This commit is contained in:
Andriy
2023-07-29 15:03:45 +03:00
parent 5e4183acfa
commit a24332370d
36 changed files with 46 additions and 111 deletions
+1 -3
View File
@@ -1,6 +1,4 @@
using System;
namespace Kruzya.TelegramBot.Core.Cache
namespace Kruzya.TelegramBot.Core.Cache
{
public interface ICacheStorage<TKey, TValue>
{
+7 -9
View File
@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using SQLitePCL;
namespace Kruzya.TelegramBot.Core.Cache
{
@@ -32,7 +30,7 @@ namespace Kruzya.TelegramBot.Core.Cache
/// <summary>
///
/// </summary>
private Dictionary<TKey, CacheEntry<TValue>> dict = new Dictionary<TKey, CacheEntry<TValue>>();
private readonly Dictionary<TKey, CacheEntry<TValue>> _dict = new();
/// <summary>
/// The TTL for all entries if not set.
@@ -51,20 +49,20 @@ namespace Kruzya.TelegramBot.Core.Cache
public bool TryGetValue(TKey key, out TValue value)
{
value = default;
if (!dict.ContainsKey(key))
if (!_dict.ContainsKey(key))
{
return false;
}
CacheEntry<TValue> temp;
if (!dict.TryGetValue(key, out temp))
if (!_dict.TryGetValue(key, out temp))
{
return false;
}
if (temp.IsExpired())
{
dict.Remove(key);
_dict.Remove(key);
return false;
}
@@ -90,12 +88,12 @@ namespace Kruzya.TelegramBot.Core.Cache
public void SetValue(TKey key, TValue value, int timeToLive)
{
if (dict.ContainsKey(key))
if (_dict.ContainsKey(key))
{
dict.Remove(key);
_dict.Remove(key);
}
dict.Add(key, new CacheEntry<TValue>()
_dict.Add(key, new CacheEntry<TValue>()
{
Created = DateTime.Now,
ExpiresAt = timeToLive,
-1
View File
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace Kruzya.TelegramBot.Core.Data
-4
View File
@@ -1,12 +1,8 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Xml.Serialization;
namespace Kruzya.TelegramBot.Core.Data
{
+1 -2
View File
@@ -1,5 +1,4 @@
using System;
using Telegram.Bot.Types;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
namespace Kruzya.TelegramBot.Core.Extensions;
+1 -4
View File
@@ -1,7 +1,4 @@
using System.Threading.Tasks;
using Kruzya.TelegramBot.Core.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore;
namespace Kruzya.TelegramBot.Core.Extensions
{
@@ -1,14 +1,9 @@
using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using BotFramework;
using BotFramework.Abstractions;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Telegram.Bot;
using Telegram.Bot.Exceptions;
using Telegram.Bot.Types;
namespace Kruzya.TelegramBot.Core.Service
{
-1
View File
@@ -1,5 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Configuration;
@@ -34,7 +34,7 @@ public partial class BanStickerSet
public async Task<bool> PreCommand(ParamParser.StickerSet.Param? stickerSetParam = null)
{
if (stickerSetParam != null && !stickerSetParam.IsValid)
if (stickerSetParam is { IsValid: false })
{
await Reply("Неверный набор стикеров.");
return false;
+1 -2
View File
@@ -1,5 +1,4 @@
using System;
using System.Linq;
using System.Linq;
using System.Threading.Tasks;
using BotFramework;
using BotFramework.Attributes;
+1 -3
View File
@@ -1,6 +1,4 @@
#nullable enable
using System.Threading.Tasks;
using System.Threading.Tasks;
using BotFramework.Attributes;
using BotFramework.Enums;
using Kruzya.TelegramBot.Core.Data;
+4 -15
View File
@@ -3,10 +3,8 @@ using System.Threading.Tasks;
using BotFramework;
using BotFramework.Attributes;
using BotFramework.Enums;
using BotFramework.Setup;
using Kruzya.TelegramBot.CombotAntiSpam.Combot;
using Kruzya.TelegramBot.Core.Extensions;
using Microsoft.Extensions.Logging;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
@@ -15,8 +13,7 @@ namespace Kruzya.TelegramBot.CombotAntiSpam;
public class UserJoinHandler : BotEventHandler
{
protected ICombotClient _combotClient;
protected ILogger _logger;
private readonly ICombotClient _combotClient;
public UserJoinHandler(ICombotClient combotClient)
{
@@ -27,17 +24,9 @@ public class UserJoinHandler : BotEventHandler
[Message(MessageFlag.HasNewChatMembers)]
public async Task OnUserJoined()
{
// var canKickMembers = (await Bot.GetChatMemberAsync(Chat, (await Bot.GetMeAsync()).Id)).CanRestrictMembers == true;
var meUserId = (await Bot.GetMeAsync()).Id;
var meChatMember = await Bot.GetChatMemberAsync(Chat, meUserId);
var canKickMembers = await Bot.IsUserAdminAsync(Chat, await Bot.GetMeAsync());
var canKickMembers = false;
if (meChatMember is ChatMemberAdministrator adminMember)
{
canKickMembers = adminMember.CanRestrictMembers;
}
foreach (var member in RawUpdate.Message.NewChatMembers)
foreach (var member in RawUpdate.Message!.NewChatMembers!)
{
if (!(await _combotClient.IsSpammerAsync(member)))
{
@@ -45,7 +34,7 @@ public class UserJoinHandler : BotEventHandler
}
var messageText = new StringBuilder();
messageText.Append($"⚠️ <b>Combot Anti-Spam</b>\n\n");
messageText.Append("⚠️ <b>Combot Anti-Spam</b>\n\n");
messageText.Append($"User {member.ToHtml(true)} is spammer.\n");
messageText.Append(
$"More details you can find on <a href=\"https://cas.chat/query?u={member.Id}\">CAS site</a>.");
+2 -1
View File
@@ -29,9 +29,10 @@ public class ContentStore : Module
{
services.AddOption<AllowNsfw>();
var kittiesConfig = Configuration.GetSection("KittiesService");
services.AddHttpClient<IKittiesApi, KittiesApi>(c =>
{
var kittiesConfig = Configuration.GetSection("KittiesService");
c.BaseAddress = kittiesConfig.GetValue<Uri>("Uri");
c.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
"Bearer",
@@ -1,6 +1,4 @@
using Kruzya.TelegramBot.Core.Data;
using Microsoft.Extensions.Configuration;
using West.TelegramBot.ContentStore.API;
using West.TelegramBot.ContentStore.API;
using West.TelegramBot.ContentStore.Model;
using West.TelegramBot.ContentStore.Option;
@@ -1,5 +1,4 @@
using Microsoft.Extensions.Configuration;
using West.TelegramBot.ContentStore.API;
using West.TelegramBot.ContentStore.API;
using West.TelegramBot.ContentStore.Model;
using West.TelegramBot.ContentStore.Option;
@@ -1,6 +1,4 @@
using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions;
using Newtonsoft.Json;
using Newtonsoft.Json;
using Telegram.Bot.Types;
using West.TelegramBot.ContentStore.Model;
using West.TelegramBot.ContentStore.Option;
-1
View File
@@ -3,7 +3,6 @@ global using GuessCache = Kruzya.TelegramBot.Core.Cache.MemoryCache<Telegram.Bot
using Kruzya.TelegramBot.Core;
using Kruzya.TelegramBot.Core.Options;
using Microsoft.Extensions.DependencyInjection;
@@ -4,7 +4,6 @@ using System;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using BotFramework;
using Castle.Core.Internal;
using Kruzya.TelegramBot.Core;
using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions;
+1 -1
View File
@@ -44,7 +44,7 @@ public class Guess : CommonHandler
{
ChatId = Chat.Id,
DeleteAt = DateTime.UtcNow.AddSeconds(20),
MessageId = Message.MessageId
MessageId = Message!.MessageId
});
var cooldownOption = await _db.UserValues.FindOrCreateOption(Chat.Id, From.Id, "guessCooldown");
+1 -3
View File
@@ -1,6 +1,4 @@
using Telegram.Bot.Types;
namespace West.Entertainment.Handler;
namespace West.Entertainment.Handler;
public class GuessData
{
+1 -3
View File
@@ -2,7 +2,6 @@
using BotFramework.Enums;
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using System.Linq;
using Kruzya.TelegramBot.Core;
using Kruzya.TelegramBot.Core.Data;
@@ -14,7 +13,6 @@ using Kruzya.TelegramBot.Core.AutoDelete;
using System.Collections.Concurrent;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types;
using Microsoft.AspNetCore.Components.Forms;
using Kruzya.TelegramBot.Core.Handler;
namespace West.Entertainment.Handler;
@@ -46,7 +44,7 @@ public class Roll : CommonHandler
{
ChatId = Chat.Id,
DeleteAt = DateTime.UtcNow.AddSeconds(20),
MessageId = Message.MessageId
MessageId = Message!.MessageId
});
if (cd > DateTime.Now)
+4 -4
View File
@@ -1,7 +1,7 @@
/// This file is a part of RSS Bot for Telegram.
/// License: MIT
/// Author: CrazyHackGUT aka Kruzya (Sergey Gut) <kruzefag@gmail.com>
///
// This file is a part of RSS Bot for Telegram.
// License: MIT
// Author: CrazyHackGUT aka Kruzya (Sergey Gut) <kruzefag@gmail.com>
//
using System;
using System.Collections.Generic;
+4 -5
View File
@@ -1,12 +1,11 @@
/// This file is a part of RSS Bot for Telegram.
/// License: MIT
/// Author: CrazyHackGUT aka Kruzya (Sergey Gut) <kruzefag@gmail.com>
///
// This file is a part of RSS Bot for Telegram.
// License: MIT
// Author: CrazyHackGUT aka Kruzya (Sergey Gut) <kruzefag@gmail.com>
//
using System;
using System.ComponentModel.DataAnnotations;
using System.Text;
using System.Web;
using Kruzya.TelegramBot.RichSiteSummary.UrchinTrackingModule;
using Telegram.Bot.Types.Enums;
@@ -1,7 +1,7 @@
/// This file is a part of RSS Bot for Telegram.
/// License: MIT
/// Author: CrazyHackGUT aka Kruzya (Sergey Gut) <kruzefag@gmail.com>
///
// This file is a part of RSS Bot for Telegram.
// License: MIT
// Author: CrazyHackGUT aka Kruzya (Sergey Gut) <kruzefag@gmail.com>
//
using System;
using System.Web;
+4 -4
View File
@@ -1,7 +1,7 @@
/// This file is a part of RSS Bot for Telegram.
/// License: MIT
/// Author: CrazyHackGUT aka Kruzya (Sergey Gut) <kruzefag@gmail.com>
///
// This file is a part of RSS Bot for Telegram.
// License: MIT
// Author: CrazyHackGUT aka Kruzya (Sergey Gut) <kruzefag@gmail.com>
//
using System;
using System.ComponentModel.DataAnnotations;
@@ -5,7 +5,6 @@ using System.Text;
using System.Threading.Tasks;
using BotFramework.Attributes;
using BotFramework.Enums;
using BotFramework.Setup;
using Kruzya.TelegramBot.Core.Extensions;
using Kruzya.TelegramBot.RichSiteSummary.Data;
using Microsoft.EntityFrameworkCore;
+1 -3
View File
@@ -1,5 +1,4 @@
using System;
using BotFramework;
using BotFramework;
using Kruzya.TelegramBot.Core;
using Kruzya.TelegramBot.Core.Extensions;
using Kruzya.TelegramBot.RichSiteSummary.Data;
@@ -7,7 +6,6 @@ using Kruzya.TelegramBot.RichSiteSummary.Service;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Telegram.Bot.Types;
namespace Kruzya.TelegramBot.RichSiteSummary
{
@@ -5,7 +5,6 @@ using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using System.Linq;
using CodeHollow.FeedReader;
@@ -20,8 +19,6 @@ using Telegram.Bot.Types.Enums;
using Feed = Kruzya.TelegramBot.RichSiteSummary.Data.Feed;
using CodeHollow.FeedReader.Feeds;
using CodeHollow.FeedReader.Feeds.Itunes;
using CodeHollow.FeedReader.Feeds.MediaRSS;
namespace Kruzya.TelegramBot.RichSiteSummary.Service
{
@@ -2,13 +2,11 @@
using System.Collections.Concurrent;
using System.Linq;
using System.Threading.Tasks;
using BotFramework;
using BotFramework.Abstractions;
using Kruzya.TelegramBot.Core.Service;
using Kruzya.TelegramBot.RichSiteSummary.Data;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Telegram.Bot;
using Telegram.Bot.Types;
namespace Kruzya.TelegramBot.RichSiteSummary.Service
@@ -2,7 +2,6 @@ using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web;
using Microsoft.EntityFrameworkCore.Internal;
namespace Kruzya.TelegramBot.RichSiteSummary.UrchinTrackingModule
{
@@ -1,7 +1,6 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using BotFramework;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
+2 -4
View File
@@ -4,8 +4,6 @@ using System.Threading.Tasks;
using BotFramework;
using BotFramework.Attributes;
using BotFramework.Enums;
using BotFramework.Setup;
using Kruzya.TelegramBot.Core.Cache;
using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions;
using Microsoft.Extensions.Logging;
@@ -49,12 +47,12 @@ namespace Kruzya.TelegramBot.UrlLimitations
0 || await message.HasUnknownMention(Bot))
{
var targetChat = new ChatId(Chat.Id);
Task.WaitAll(new Task[] {
Task.WaitAll(
Bot.BanChatMemberAsync(new ChatId(Chat.Id), From.Id, DateTime.Now.AddDays(1)),
Bot.DeleteMessageAsync(targetChat, message.MessageId),
Bot.SendTextMessageAsync(targetChat,
$"Member {From.ToHtml(true)} has been banned.\n<b>Reason</b>: <pre>Spam suspicion</pre>", parseMode: ParseMode.Html)
});
);
}
}
}
+1 -5
View File
@@ -1,8 +1,4 @@
using System;
using Kruzya.TelegramBot.Core;
using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions;
using Microsoft.Extensions.DependencyInjection;
using Kruzya.TelegramBot.Core;
namespace Kruzya.TelegramBot.UrlLimitations
{
@@ -3,11 +3,8 @@ using System.Threading.Tasks;
using BotFramework;
using BotFramework.Attributes;
using BotFramework.Enums;
using BotFramework.Setup;
using Kruzya.TelegramBot.Core.Cache;
using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions;
using Telegram.Bot.Types.Enums;
namespace Kruzya.TelegramBot.UrlLimitations
{
-1
View File
@@ -7,7 +7,6 @@ using BotFramework;
using BotFramework.Attributes;
using BotFramework.Enums;
using BotFramework.Utils;
using Castle.Core.Internal;
using Kruzya.TelegramBot.Core.Cache;
using Kruzya.TelegramBot.Core.Data;
using Kruzya.TelegramBot.Core.Extensions;
+1 -2
View File
@@ -1,5 +1,4 @@
using System;
using Kruzya.TelegramBot.Core;
using Kruzya.TelegramBot.Core;
namespace UserGroupTag
{