mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
Reputation managment.
This commit is contained in:
@@ -5,7 +5,7 @@ namespace Kruzya.TelegramBot.Core.Extensions
|
|||||||
{
|
{
|
||||||
public static class UserExtension
|
public static class UserExtension
|
||||||
{
|
{
|
||||||
public static string ToHtml(this User user, bool byUsername = true)
|
public static string ToHtml(this User user, bool byUsername = false)
|
||||||
{
|
{
|
||||||
var text = new StringBuilder();
|
var text = new StringBuilder();
|
||||||
text.Append($"<a href=\"tg://user?id={user.Id}\">");
|
text.Append($"<a href=\"tg://user?id={user.Id}\">");
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using BotFramework;
|
using BotFramework;
|
||||||
using JetBrains.Annotations;
|
|
||||||
using Kruzya.TelegramBot.Core.Data;
|
using Kruzya.TelegramBot.Core.Data;
|
||||||
using Kruzya.TelegramBot.Core.Extensions;
|
using Kruzya.TelegramBot.Core.Extensions;
|
||||||
using Telegram.Bot;
|
using Telegram.Bot;
|
||||||
@@ -46,7 +45,7 @@ namespace West.TelegramBot.ChatManagement.Handler
|
|||||||
{
|
{
|
||||||
await Bot.SendTextMessageAsync(
|
await Bot.SendTextMessageAsync(
|
||||||
Chat.Id,
|
Chat.Id,
|
||||||
$"{From.ToHtml(false)} <code>заблокировал</code> {user.ToHtml(false)} <code>на 7 дней</code>",
|
$"{From.ToHtml()} <code>заблокировал</code> {user.ToHtml()} <code>на 7 дней</code>",
|
||||||
disableNotification: true, parseMode: ParseMode.Html);
|
disableNotification: true, parseMode: ParseMode.Html);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,32 +1,110 @@
|
|||||||
#nullable enable
|
#nullable enable
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using BotFramework.Attributes;
|
using BotFramework.Attributes;
|
||||||
using BotFramework.Enums;
|
using BotFramework.Enums;
|
||||||
using Kruzya.TelegramBot.Core.Data;
|
using Kruzya.TelegramBot.Core.Data;
|
||||||
using Kruzya.TelegramBot.Core.Extensions;
|
using Kruzya.TelegramBot.Core.Extensions;
|
||||||
|
using Telegram.Bot.Types;
|
||||||
|
|
||||||
namespace West.TelegramBot.ChatManagement.Handler
|
namespace West.TelegramBot.ChatManagement.Handler
|
||||||
{
|
{
|
||||||
public class Reputation : ManagementHandler
|
public class Reputation : ManagementHandler
|
||||||
{
|
{
|
||||||
|
private static readonly List<string> RepUpChars = new()
|
||||||
|
{
|
||||||
|
"+",
|
||||||
|
"👍",
|
||||||
|
"👍🏼",
|
||||||
|
"👍🏽",
|
||||||
|
"👍🏾",
|
||||||
|
"👍🏿",
|
||||||
|
"👍🏻"
|
||||||
|
};
|
||||||
|
|
||||||
|
private static readonly List<string> RepDownChars = new()
|
||||||
|
{
|
||||||
|
"-",
|
||||||
|
"👎",
|
||||||
|
"👎🏼",
|
||||||
|
"👎🏽",
|
||||||
|
"👎🏾",
|
||||||
|
"👎🏿",
|
||||||
|
"👎🏻"
|
||||||
|
};
|
||||||
|
|
||||||
public Reputation(CoreContext db) : base(db) { }
|
public Reputation(CoreContext db) : base(db) { }
|
||||||
|
|
||||||
[Command("whois", CommandParseMode.Both)]
|
[Command("whois", CommandParseMode.Both)]
|
||||||
public async Task HandleWhois()
|
public async Task HandleWhois()
|
||||||
{
|
{
|
||||||
var user = RepliedUser ?? From;
|
var user = RepliedUser ?? From;
|
||||||
var message = $"{user.ToHtml(false)}\n";
|
var message = $"{user.ToHtml()}\n";
|
||||||
if (user.IsBot)
|
if (user.IsBot)
|
||||||
{
|
{
|
||||||
message += "<code>Бот</code>";
|
message += "<code>Бот</code>";
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
message += $"<code>Предупреждения: {(await GetWarnOption(user)).GetValue<int>()}</code>";
|
message += $"<code>Репутация: {await GetUserReputation(user)}\n";
|
||||||
|
message += $"Предупреждения: {(await GetWarnOption(user)).GetValue<int>()}</code>";
|
||||||
}
|
}
|
||||||
|
|
||||||
await Reply(message);
|
await Reply(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Message(InChat.Public, MessageFlag.HasText | MessageFlag.HasSticker)]
|
||||||
|
public async Task HandleReputation()
|
||||||
|
{
|
||||||
|
var text = Message?.Text;
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(text))
|
||||||
|
{
|
||||||
|
text = Message?.Sticker?.Emoji;
|
||||||
|
}
|
||||||
|
if (text == null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var receiver = RepliedUser;
|
||||||
|
var canChangeRep = receiver is { IsBot: false } && !From.IsBot && receiver.Id != From.Id;
|
||||||
|
|
||||||
|
if (canChangeRep && (RepUpChars.Contains(text) || RepDownChars.Contains(text)))
|
||||||
|
{
|
||||||
|
var repOption = await GetReputationOption(receiver!);
|
||||||
|
var repCount = repOption.GetValue<int>();
|
||||||
|
|
||||||
|
string action;
|
||||||
|
|
||||||
|
if (RepUpChars.Contains(text))
|
||||||
|
{
|
||||||
|
repCount++;
|
||||||
|
action = "увеличил";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
repCount--;
|
||||||
|
action = "уменьшил";
|
||||||
|
}
|
||||||
|
|
||||||
|
repOption.SetValue(repCount);
|
||||||
|
Db.MarkAsModified(repOption);
|
||||||
|
|
||||||
|
await Reply($"{From.ToHtml()}<code>({await GetUserReputation(From)}) " +
|
||||||
|
$"{action} репутацию </code>{receiver.ToHtml()}<code>({repCount})</code>");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<int> GetUserReputation(User user)
|
||||||
|
{
|
||||||
|
return (await GetReputationOption(user)).GetValue<int>();
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<BotUserValue> GetReputationOption(User user)
|
||||||
|
{
|
||||||
|
return await Db.UserValues.FindOrCreateOption(Chat.Id, user.Id, "reputation");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -26,7 +26,7 @@ namespace West.TelegramBot.ChatManagement.Handler
|
|||||||
warnOption.SetValue(warnCount);
|
warnOption.SetValue(warnCount);
|
||||||
Db.MarkAsModified(warnOption);
|
Db.MarkAsModified(warnOption);
|
||||||
|
|
||||||
await Bot.SendTextMessageAsync(Chat.Id, $"{warnUser.ToHtml(false)}<code>, Вам выдано предупреждение! " +
|
await Bot.SendTextMessageAsync(Chat.Id, $"{warnUser.ToHtml()}<code>, Вам выдано предупреждение! " +
|
||||||
$"Текущее кол-во: {warnCount}/3</code>",
|
$"Текущее кол-во: {warnCount}/3</code>",
|
||||||
disableNotification: true,
|
disableNotification: true,
|
||||||
parseMode: ParseMode.Html);
|
parseMode: ParseMode.Html);
|
||||||
@@ -49,7 +49,7 @@ namespace West.TelegramBot.ChatManagement.Handler
|
|||||||
Db.MarkAsModified(warnOption);
|
Db.MarkAsModified(warnOption);
|
||||||
|
|
||||||
await Bot.SendTextMessageAsync(Chat.Id,
|
await Bot.SendTextMessageAsync(Chat.Id,
|
||||||
$"{RepliedUser.ToHtml(false)}<code>, с Вас снято предупреждение! " +
|
$"{RepliedUser.ToHtml()}<code>, с Вас снято предупреждение! " +
|
||||||
$"Текущее кол-во: {warnCount}/3</code>",
|
$"Текущее кол-во: {warnCount}/3</code>",
|
||||||
disableNotification: true,
|
disableNotification: true,
|
||||||
parseMode: ParseMode.Html);
|
parseMode: ParseMode.Html);
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ namespace Kruzya.TelegramBot.CombotAntiSpam
|
|||||||
|
|
||||||
var messageText = new StringBuilder();
|
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()} is spammer.\n");
|
messageText.Append($"User {member.ToHtml(true)} is spammer.\n");
|
||||||
messageText.Append(
|
messageText.Append(
|
||||||
$"More details you can find on <a href=\"https://cas.chat/query?u={member.Id}\">CAS site</a>.");
|
$"More details you can find on <a href=\"https://cas.chat/query?u={member.Id}\">CAS site</a>.");
|
||||||
|
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ namespace Kruzya.TelegramBot.UrlLimitations
|
|||||||
Bot.BanChatMemberAsync(new ChatId(Chat.Id), From.Id, DateTime.Now.AddDays(1)),
|
Bot.BanChatMemberAsync(new ChatId(Chat.Id), From.Id, DateTime.Now.AddDays(1)),
|
||||||
Bot.DeleteMessageAsync(targetChat, message.MessageId),
|
Bot.DeleteMessageAsync(targetChat, message.MessageId),
|
||||||
Bot.SendTextMessageAsync(targetChat,
|
Bot.SendTextMessageAsync(targetChat,
|
||||||
$"Member {From.ToHtml()} has been banned.\n<b>Reason</b>: <pre>Spam suspicion</pre>", ParseMode.Html)
|
$"Member {From.ToHtml(true)} has been banned.\n<b>Reason</b>: <pre>Spam suspicion</pre>", ParseMode.Html)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user