public async Task OnUserJoinedAsync(SocketGuildUser user) { if (!user.IsUser()) { return; } var latestInvites = await GetLatestInvitesOfGuildAsync(user.Guild); var usedInvite = FindUsedInvite(user.Guild, latestInvites); if (usedInvite == null) { Logger.LogWarning($"User {user.GetFullName()} ({user.Id}) user unkown invite."); return; } DBDiscordUser inviteCreator = null; if (usedInvite.Creator != null) { inviteCreator = await GrillBotRepository.UsersRepository.GetOrCreateUserAsync(user.Guild.Id, usedInvite.Creator.Id, UsersIncludes.Invites); await GrillBotRepository.CommitAsync(); } var inviteEntity = await GrillBotRepository.InviteRepository.FindInviteAsync(usedInvite.Code); if (inviteEntity == null) { inviteEntity = new Invite() { ChannelIdSnowflake = usedInvite.ChannelId, Code = usedInvite.Code, CreatedAt = usedInvite.CreatedAt.HasValue ? usedInvite.CreatedAt.Value.UtcDateTime : (DateTime?)null, CreatorId = inviteCreator?.ID }; await GrillBotRepository.AddAsync(inviteEntity); } var joinedUser = await GrillBotRepository.UsersRepository.GetOrCreateUserAsync(user.Guild.Id, user.Id, UsersIncludes.Invites); joinedUser.UsedInviteCode = usedInvite.Code; await GrillBotRepository.CommitAsync(); AppendInvites(user.Guild, latestInvites); }
public async Task AssignInviteToUserAsync(SocketUser user, SocketGuild guild, string code) { var latestInvites = await GetLatestInvitesOfGuildAsync(guild); var usedInvite = latestInvites.Find(o => o.Code == code); if (usedInvite == null) { throw new NotFoundException($"Pozvánka s kódem `{code}` neexistuje"); } DBDiscordUser inviteCreator = null; if (usedInvite.Creator != null) { inviteCreator = await GrillBotRepository.UsersRepository.GetOrCreateUserAsync(guild.Id, usedInvite.Creator.Id, UsersIncludes.Invites); await GrillBotRepository.CommitAsync(); } var inviteEntity = await GrillBotRepository.InviteRepository.FindInviteAsync(usedInvite.Code); if (inviteEntity == null) { inviteEntity = new Invite() { ChannelIdSnowflake = usedInvite.ChannelId, Code = usedInvite.Code, CreatedAt = usedInvite.CreatedAt.HasValue ? usedInvite.CreatedAt.Value.UtcDateTime : (DateTime?)null, CreatorId = inviteCreator?.ID }; await GrillBotRepository.AddAsync(inviteEntity); } var userEntity = await GrillBotRepository.UsersRepository.GetOrCreateUserAsync(guild.Id, user.Id, UsersIncludes.Invites); userEntity.UsedInviteCode = code; await GrillBotRepository.CommitAsync(); AppendInvites(guild, latestInvites); }