Skip to content

Commit

Permalink
some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
th0mk committed Jun 21, 2023
1 parent 16ac168 commit 4f09b8b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
60 changes: 59 additions & 1 deletion src/FMBot.Bot/Services/SupporterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ public async Task UpdateDiscordSupporters()
{
discordUsersLeft.Remove(discordSupporter.DiscordUserId);

if (existingSupporter.LastPayment != discordSupporter.EndsAt)
if (existingSupporter.LastPayment != discordSupporter.EndsAt && existingSupporter.Expired != true)
{
Log.Information("Updating Discord supporter {discordUserId}", discordSupporter.DiscordUserId);

Expand All @@ -689,6 +689,32 @@ public async Task UpdateDiscordSupporters()
await supporterAuditLogChannel.SendMessageAsync(embeds: new[] { embed.Build() });
}

if (existingSupporter.Expired == true && discordSupporter.Active)
{
Log.Information("Re-activating Discord supporter {discordUserId}", discordSupporter.DiscordUserId);

var reActivatedSupporter = await ReActivateSupporter(existingSupporter, discordSupporter);
await ModifyGuildRole(discordSupporter.DiscordUserId);
await RunFullUpdate(discordSupporter.DiscordUserId);

var user = await this._client.Rest.GetUserAsync(discordSupporter.DiscordUserId);
if (user != null)
{
await SendSupporterWelcomeMessage(user, false, reActivatedSupporter);
}

discordUsersLeft.Remove(discordSupporter.DiscordUserId);

var supporterAuditLogChannel = new DiscordWebhookClient(this._botSettings.Bot.SupporterAuditLogWebhookUrl);
var embed = new EmbedBuilder().WithDescription(
$"Re-activated Discord supporter {discordSupporter.DiscordUserId} - <@{discordSupporter.DiscordUserId}>");
await supporterAuditLogChannel.SendMessageAsync(embeds: new[] { embed.Build() });

Log.Information("Re-activated Discord supporter {discordUserId}", discordSupporter.DiscordUserId);

continue;
}

if (existingSupporter.Expired != true && !discordSupporter.Active)
{
Log.Information("Removing Discord supporter {discordUserId}", discordSupporter.DiscordUserId);
Expand Down Expand Up @@ -794,6 +820,38 @@ private async Task<Supporter> AddDiscordSupporter(ulong id, DiscordEntitlement e
return supporterToAdd;
}

private async Task<Supporter> ReActivateSupporter(Supporter supporter, DiscordEntitlement entitlement)
{
await using var db = await this._contextFactory.CreateDbContextAsync();
var user = await db.Users
.AsQueryable()
.FirstOrDefaultAsync(f => f.DiscordUserId == supporter.DiscordUserId.Value);

if (user == null)
{
Log.Warning("Someone who isn't registered in .fmbot just re-activated their Discord subscription - ID {discordUserId}", supporter.DiscordUserId);
}
else
{
if (user.UserType == UserType.User)
{
user.UserType = UserType.Supporter;
}

db.Update(user);
}

supporter.Expired = null;
supporter.SupporterMessagesEnabled = true;
supporter.VisibleInOverview = true;
supporter.LastPayment = entitlement.EndsAt;

db.Update(supporter);
await db.SaveChangesAsync();

return supporter;
}

private async Task ExpireSupporter(ulong id, Supporter supporter)
{
await using var db = await this._contextFactory.CreateDbContextAsync();
Expand Down
5 changes: 4 additions & 1 deletion src/FMBot.Subscriptions/Services/DiscordSkuService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ public async Task<List<DiscordEntitlement>> GetEntitlements()

var result = JsonSerializer.Deserialize<List<DiscordEntitlementResponseModel>>(requestBody, jsonSerializerOptions);

return result.Select(ResponseToModel).ToList();
return result
.Where(w => w.UserId.HasValue)
.Select(ResponseToModel)
.ToList();
}
catch (Exception ex)
{
Expand Down

0 comments on commit 4f09b8b

Please sign in to comment.