xpadd will now trigger rewards and notifications, after so long

This commit is contained in:
Toastie 2025-04-07 01:28:01 +12:00
parent cbb9f8d1ac
commit 11141df435
Signed by: toastie_t0ast
GPG key ID: 0861BE54AD481DC7
2 changed files with 116 additions and 137 deletions
src/EllieBot/Modules/Xp

View file

@ -101,33 +101,33 @@ public partial class Xp : EllieModule<XpService>
}
await Response()
.Paginated()
.PageItems(GetPageItems)
.PageSize(10)
.CurrentPage(page)
.Page((users, curPage) =>
{
var embed = CreateEmbed().WithTitle(GetText(strs.server_leaderboard)).WithOkColor();
.Paginated()
.PageItems(GetPageItems)
.PageSize(10)
.CurrentPage(page)
.Page((users, curPage) =>
{
var embed = CreateEmbed().WithTitle(GetText(strs.server_leaderboard)).WithOkColor();
if (!users.Any())
return embed.WithDescription("-");
if (!users.Any())
return embed.WithDescription("-");
for (var i = 0; i < users.Count; i++)
{
var levelStats = new LevelStats(users[i].Xp);
var user = ((SocketGuild)ctx.Guild).GetUser(users[i].UserId);
for (var i = 0; i < users.Count; i++)
{
var levelStats = new LevelStats(users[i].Xp);
var user = ((SocketGuild)ctx.Guild).GetUser(users[i].UserId);
var userXpData = users[i];
var userXpData = users[i];
var awardStr = string.Empty;
var awardStr = string.Empty;
embed.AddField($"#{i + 1 + (curPage * 10)} {user?.ToString() ?? users[i].UserId.ToString()}",
$"{GetText(strs.level_x(levelStats.Level))} - {levelStats.TotalXp}xp {awardStr}");
}
embed.AddField($"#{i + 1 + (curPage * 10)} {user?.ToString() ?? users[i].UserId.ToString()}",
$"{GetText(strs.level_x(levelStats.Level))} - {levelStats.TotalXp}xp {awardStr}");
}
return embed;
})
.SendAsync();
return embed;
})
.SendAsync();
}
[Cmd]
@ -148,8 +148,8 @@ public partial class Xp : EllieModule<XpService>
await _service.SetLevelAsync(ctx.Guild.Id, userId, level);
await Response()
.Confirm(strs.level_set($"<@{userId}>", Format.Bold(level.ToString())))
.SendAsync();
.Confirm(strs.level_set($"<@{userId}>", Format.Bold(level.ToString())))
.SendAsync();
}
[Cmd]
@ -164,36 +164,28 @@ public partial class Xp : EllieModule<XpService>
if (role.IsManaged)
return;
var count = await _service.AddXpToUsersAsync(ctx.Guild.Id,
await _service.AddXpAsync(ctx.Channel.Id,
amount,
role.Members.Select(x => x.Id).ToArray());
role.Members.Cast<IGuildUser>().ToArray());
await Response()
.Confirm(
strs.xpadd_users(Format.Bold(amount.ToString()), Format.Bold(count.ToString())))
.SendAsync();
.Confirm(strs.xpadd_users(Format.Bold(amount.ToString()), Format.Bold(role.Name)))
.SendAsync();
}
[Cmd]
[RequireContext(ContextType.Guild)]
[UserPerm(GuildPerm.Administrator)]
[Priority(3)]
public async Task XpAdd(int amount, ulong userId)
public async Task XpAdd(int amount, [Leftover] IGuildUser user)
{
if (amount == 0)
return;
await _service.AddXpAsync(userId, ctx.Guild.Id, amount);
var usr = ((SocketGuild)ctx.Guild).GetUser(userId)?.ToString() ?? userId.ToString();
await Response().Confirm(strs.modified(Format.Bold(usr), Format.Bold(amount.ToString()))).SendAsync();
await _service.AddXpAsync(ctx.Channel.Id, amount, user);
await Response().Confirm(strs.modified(Format.Bold(user.ToString()), Format.Bold(amount.ToString()))).SendAsync();
}
[Cmd]
[RequireContext(ContextType.Guild)]
[UserPerm(GuildPerm.Administrator)]
[Priority(4)]
public Task XpAdd(int amount, [Leftover] IGuildUser user)
=> XpAdd(amount, user.Id);
[Cmd]
[RequireContext(ContextType.Guild)]
[OwnerOnly]
@ -216,8 +208,8 @@ public partial class Xp : EllieModule<XpService>
public async Task XpReset(ulong userId)
{
var embed = CreateEmbed()
.WithTitle(GetText(strs.reset))
.WithDescription(GetText(strs.reset_user_confirm));
.WithTitle(GetText(strs.reset))
.WithDescription(GetText(strs.reset_user_confirm));
if (!await PromptUserConfirmAsync(embed))
return;
@ -233,8 +225,8 @@ public partial class Xp : EllieModule<XpService>
public async Task XpReset()
{
var embed = CreateEmbed()
.WithTitle(GetText(strs.reset))
.WithDescription(GetText(strs.reset_server_confirm));
.WithTitle(GetText(strs.reset))
.WithDescription(GetText(strs.reset_server_confirm));
if (!await PromptUserConfirmAsync(embed))
return;
@ -267,12 +259,12 @@ public partial class Xp : EllieModule<XpService>
}
await Response()
.Confirm(GetText(strs.available_commands),
$"""
`{prefix}xpshop bgs`
`{prefix}xpshop frames`
""")
.SendAsync();
.Confirm(GetText(strs.available_commands),
$"""
`{prefix}xpshop bgs`
`{prefix}xpshop frames`
""")
.SendAsync();
}
[Cmd]
@ -300,80 +292,80 @@ public partial class Xp : EllieModule<XpService>
}
await Response()
.Paginated()
.Items(allItems)
.PageSize(1)
.CurrentPage(page)
.AddFooter(false)
.Page((items, _) =>
{
if (!items.Any())
return CreateEmbed()
.WithDescription(GetText(strs.not_found))
.WithErrorColor();
.Paginated()
.Items(allItems)
.PageSize(1)
.CurrentPage(page)
.AddFooter(false)
.Page((items, _) =>
{
if (!items.Any())
return CreateEmbed()
.WithDescription(GetText(strs.not_found))
.WithErrorColor();
var (key, item) = items.FirstOrDefault();
var (_, item) = items.FirstOrDefault();
var eb = CreateEmbed()
.WithOkColor()
.WithTitle(item.Name)
.AddField(GetText(strs.price),
CurrencyHelper.N(item.Price, Culture, _gss.GetCurrencySign()),
true)
.WithImageUrl(string.IsNullOrWhiteSpace(item.Preview)
? item.Url
: item.Preview);
var eb = CreateEmbed()
.WithOkColor()
.WithTitle(item.Name)
.AddField(GetText(strs.price),
CurrencyHelper.N(item.Price, Culture, _gss.GetCurrencySign()),
true)
.WithImageUrl(string.IsNullOrWhiteSpace(item.Preview)
? item.Url
: item.Preview);
if (!string.IsNullOrWhiteSpace(item.Desc))
eb.AddField(GetText(strs.desc), item.Desc);
if (!string.IsNullOrWhiteSpace(item.Desc))
eb.AddField(GetText(strs.desc), item.Desc);
return eb;
})
.Interaction(async current =>
{
var (key, _) = allItems.Skip(current).First();
return eb;
})
.Interaction(async current =>
{
var (key, _) = allItems.Skip(current).First();
var itemType = type == XpShopInputType.Backgrounds
? XpShopItemType.Background
: XpShopItemType.Frame;
var itemType = type == XpShopInputType.Backgrounds
? XpShopItemType.Background
: XpShopItemType.Frame;
var ownedItem = await _service.GetUserItemAsync(ctx.User.Id, itemType, key);
if (ownedItem is not null)
{
var button = new ButtonBuilder(ownedItem.IsUsing
? GetText(strs.in_use)
: GetText(strs.use),
"xpshop:use",
emote: Emoji.Parse("👐"),
isDisabled: ownedItem.IsUsing);
var ownedItem = await _service.GetUserItemAsync(ctx.User.Id, itemType, key);
if (ownedItem is not null)
{
var button = new ButtonBuilder(ownedItem.IsUsing
? GetText(strs.in_use)
: GetText(strs.use),
"xpshop:use",
emote: Emoji.Parse("👐"),
isDisabled: ownedItem.IsUsing);
var inter = _inter.Create(
ctx.User.Id,
button,
OnShopUse,
(key, itemType),
clearAfter: false);
var inter = _inter.Create(
ctx.User.Id,
button,
OnShopUse,
(key, itemType),
clearAfter: false);
return inter;
}
else
{
var button = new ButtonBuilder(GetText(strs.buy),
"xpshop:buy",
emote: Emoji.Parse("💰"));
return inter;
}
else
{
var button = new ButtonBuilder(GetText(strs.buy),
"xpshop:buy",
emote: Emoji.Parse("💰"));
var inter = _inter.Create(
ctx.User.Id,
button,
OnShopBuy,
(key, itemType),
singleUse: true,
clearAfter: false);
var inter = _inter.Create(
ctx.User.Id,
button,
OnShopBuy,
(key, itemType),
singleUse: true,
clearAfter: false);
return inter;
}
})
.SendAsync();
return inter;
}
})
.SendAsync();
}
[Cmd]
@ -396,8 +388,8 @@ public partial class Xp : EllieModule<XpService>
{
BuyResult.XpShopDisabled => await Response().Error(strs.xp_shop_disabled).SendAsync(),
BuyResult.InsufficientFunds => await Response()
.Error(strs.not_enough(_gss.GetCurrencySign()))
.SendAsync(),
.Error(strs.not_enough(_gss.GetCurrencySign()))
.SendAsync(),
BuyResult.AlreadyOwned =>
await Response().Error(strs.xpshop_already_owned).Interaction(GetUseInteraction()).SendAsync(),
BuyResult.UnknownItem => await Response().Error(strs.xpshop_item_not_found).SendAsync(),
@ -407,10 +399,10 @@ public partial class Xp : EllieModule<XpService>
}
await Response()
.Confirm(strs.xpshop_buy_success(type.ToString().ToLowerInvariant(),
key.ToLowerInvariant()))
.Interaction(GetUseInteraction())
.SendAsync();
.Confirm(strs.xpshop_buy_success(type.ToString().ToLowerInvariant(),
key.ToLowerInvariant()))
.Interaction(GetUseInteraction())
.SendAsync();
}
[Cmd]

View file

@ -551,25 +551,12 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand
return false;
}
public async Task<int> AddXpToUsersAsync(ulong guildId, long amount, params ulong[] userIds)
public Task AddXpAsync(ulong channelId, long amount, params IGuildUser[] users)
{
await using var ctx = _db.GetDbContext();
return await ctx.GetTable<UserXpStats>()
.Where(x => x.GuildId == guildId && userIds.Contains(x.UserId))
.UpdateAsync(old => new()
{
Xp = old.Xp + amount
});
}
public async Task AddXpAsync(ulong userId, ulong guildId, int amount)
{
await using var uow = _db.GetDbContext();
var usr = uow.GetOrCreateUserXpStats(guildId, userId);
usr.Xp += amount;
await uow.SaveChangesAsync();
foreach (var user in users)
_usersBatch.Add(new(user, amount, channelId));
return Task.CompletedTask;
}
private Task<bool> TryAddUserGainedXpAsync(ulong userId, float cdInMinutes)