[reputation] /addrep command

This commit is contained in:
West14
2022-04-23 21:42:17 +03:00
parent 634695d0d8
commit 4accf36ffa
3 changed files with 41 additions and 17 deletions
+1 -1
View File
@@ -10,7 +10,7 @@ namespace Kruzya.TelegramBot.Core.Service
public Task<double> GetUserReputationAsync(Chat chat, User user); public Task<double> GetUserReputationAsync(Chat chat, User user);
public Task<double> SetUserReputationAsync(Chat chat, User user, double value); public Task<double> SetUserReputationAsync(Chat chat, User user, double value);
public Task<double> GetReputationDiffAsync(Chat chat, User sender, User receiver); public Task<double> GetReputationDiffAsync(Chat chat, User sender, User receiver);
public Task IncrementReputationAsync(Chat chat, User user, double diff); public Task<double> IncrementReputationAsync(Chat chat, User user, double diff);
public Task<IEnumerable<IReputationEntity>> GetChatRatingAsync(Chat chat, int limit = 10, int offset = 0); public Task<IEnumerable<IReputationEntity>> GetChatRatingAsync(Chat chat, int limit = 10, int offset = 0);
} }
} }
+38 -14
View File
@@ -128,27 +128,29 @@ public class Reputation : CommonHandler
[ParametrizedCommand(InChat.Public, "setrep", CommandParseMode.Both)] [ParametrizedCommand(InChat.Public, "setrep", CommandParseMode.Both)]
public async Task HandleSetRep(double reputation) public async Task HandleSetRep(double reputation)
{ {
if (!_userService.IsUserSuper(From)) if (!CanModifyUserReputation(RepliedUser))
{
_logger.LogInformation("{User} attempted to call /setrep in {Chat}", From, Chat.Title);
return;
}
if (RepliedUser == null || RepliedUser.IsBot)
{ {
return; return;
} }
reputation = await _reputationService.SetUserReputationAsync(Chat, RepliedUser, reputation); await SendReputationModifyResponse(
await Reply( await _reputationService.SetUserReputationAsync(Chat, RepliedUser, reputation)
new HtmlString()
.Code("Пользователю ")
.UserMention(RepliedUser)
.Code($" установлена репутация: {reputation}")
.ToString()
); );
} }
[ParametrizedCommand(InChat.Public, "addrep", CommandParseMode.Both)]
public async Task HandleAddRep(double reputation)
{
if (!CanModifyUserReputation(RepliedUser))
{
return;
}
await SendReputationModifyResponse(
await _reputationService.IncrementReputationAsync(Chat, RepliedUser, reputation)
);
}
[Command(InChat.Public, "top", CommandParseMode.Both)] [Command(InChat.Public, "top", CommandParseMode.Both)]
public async Task HandleTop() public async Task HandleTop()
{ {
@@ -166,6 +168,28 @@ public class Reputation : CommonHandler
disableNotification: true); disableNotification: true);
} }
private async Task SendReputationModifyResponse(double reputation)
{
await Reply(
new HtmlString()
.Code("Пользователю ")
.UserMention(RepliedUser)
.Code($" установлена репутация: {reputation}")
.ToString()
);
}
private bool CanModifyUserReputation(User? user)
{
if (!_userService.IsUserSuper(From))
{
_logger.LogInformation("{User} attempted to modify someones reputation in {Chat}", From, Chat.Title);
return false;
}
return user is {IsBot: false};
}
private async Task<double> GetUserReputation(User user) private async Task<double> GetUserReputation(User user)
{ {
return await _reputationService.GetUserReputationAsync(Chat, user); return await _reputationService.GetUserReputationAsync(Chat, user);
@@ -37,9 +37,9 @@ public class ReputationService : IReputation
return rep.Value; return rep.Value;
} }
public async Task IncrementReputationAsync(Chat chat, User user, double diff) public async Task<double> IncrementReputationAsync(Chat chat, User user, double diff)
{ {
await SetUserReputationAsync(chat, user, await GetUserReputationAsync(chat, user) + diff); return await SetUserReputationAsync(chat, user, await GetUserReputationAsync(chat, user) + diff);
} }
public async Task<double> GetReputationDiffAsync(Chat chat, User sender, User receiver) public async Task<double> GetReputationDiffAsync(Chat chat, User sender, User receiver)