mirror of
https://github.com/Jetsparrow/karmabot.git
synced 2026-01-21 00:56:09 +03:00
37 lines
1.4 KiB
C#
37 lines
1.4 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using JetBotLib;
|
|
using JetKarmaBot.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace JetKarmaBot.Services.Handling
|
|
{
|
|
public class SaveData : IRequestHandler
|
|
{
|
|
public async Task Handle(RequestContext ctx, Func<RequestContext, Task> next)
|
|
{
|
|
KarmaContext db = ctx.GetFeature<KarmaContext>();
|
|
await AddUserToDatabase(db, ctx.EventArgs.Message.From);
|
|
if (ctx.EventArgs.Message.ReplyToMessage != null)
|
|
await AddUserToDatabase(db, ctx.EventArgs.Message.ReplyToMessage.From);
|
|
if (!await db.Chats.AnyAsync(x => x.ChatId == ctx.EventArgs.Message.Chat.Id))
|
|
db.Chats.Add(new Models.Chat
|
|
{
|
|
ChatId = ctx.EventArgs.Message.Chat.Id
|
|
});
|
|
await next(ctx);
|
|
}
|
|
private async Task AddUserToDatabase(KarmaContext db, Telegram.Bot.Types.User u)
|
|
{
|
|
string un;
|
|
if (u.Username == null)
|
|
un = u.FirstName + (u.LastName != null ? " " + u.LastName : "");
|
|
else
|
|
un = "@" + u.Username;
|
|
if (!await db.Users.AnyAsync(x => x.UserId == u.Id))
|
|
await db.Users.AddAsync(new Models.User { UserId = u.Id, Username = un });
|
|
else
|
|
(await db.Users.FindAsync(u.Id)).Username = un;
|
|
}
|
|
}
|
|
} |