xpadd will now trigger rewards and notifications, after so long
This commit is contained in:
parent
cbb9f8d1ac
commit
11141df435
2 changed files with 116 additions and 137 deletions
src/EllieBot/Modules/Xp
|
@ -101,33 +101,33 @@ public partial class Xp : EllieModule<XpService>
|
||||||
}
|
}
|
||||||
|
|
||||||
await Response()
|
await Response()
|
||||||
.Paginated()
|
.Paginated()
|
||||||
.PageItems(GetPageItems)
|
.PageItems(GetPageItems)
|
||||||
.PageSize(10)
|
.PageSize(10)
|
||||||
.CurrentPage(page)
|
.CurrentPage(page)
|
||||||
.Page((users, curPage) =>
|
.Page((users, curPage) =>
|
||||||
{
|
{
|
||||||
var embed = CreateEmbed().WithTitle(GetText(strs.server_leaderboard)).WithOkColor();
|
var embed = CreateEmbed().WithTitle(GetText(strs.server_leaderboard)).WithOkColor();
|
||||||
|
|
||||||
if (!users.Any())
|
if (!users.Any())
|
||||||
return embed.WithDescription("-");
|
return embed.WithDescription("-");
|
||||||
|
|
||||||
for (var i = 0; i < users.Count; i++)
|
for (var i = 0; i < users.Count; i++)
|
||||||
{
|
{
|
||||||
var levelStats = new LevelStats(users[i].Xp);
|
var levelStats = new LevelStats(users[i].Xp);
|
||||||
var user = ((SocketGuild)ctx.Guild).GetUser(users[i].UserId);
|
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()}",
|
embed.AddField($"#{i + 1 + (curPage * 10)} {user?.ToString() ?? users[i].UserId.ToString()}",
|
||||||
$"{GetText(strs.level_x(levelStats.Level))} - {levelStats.TotalXp}xp {awardStr}");
|
$"{GetText(strs.level_x(levelStats.Level))} - {levelStats.TotalXp}xp {awardStr}");
|
||||||
}
|
}
|
||||||
|
|
||||||
return embed;
|
return embed;
|
||||||
})
|
})
|
||||||
.SendAsync();
|
.SendAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Cmd]
|
[Cmd]
|
||||||
|
@ -148,8 +148,8 @@ public partial class Xp : EllieModule<XpService>
|
||||||
|
|
||||||
await _service.SetLevelAsync(ctx.Guild.Id, userId, level);
|
await _service.SetLevelAsync(ctx.Guild.Id, userId, level);
|
||||||
await Response()
|
await Response()
|
||||||
.Confirm(strs.level_set($"<@{userId}>", Format.Bold(level.ToString())))
|
.Confirm(strs.level_set($"<@{userId}>", Format.Bold(level.ToString())))
|
||||||
.SendAsync();
|
.SendAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Cmd]
|
[Cmd]
|
||||||
|
@ -164,36 +164,28 @@ public partial class Xp : EllieModule<XpService>
|
||||||
if (role.IsManaged)
|
if (role.IsManaged)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var count = await _service.AddXpToUsersAsync(ctx.Guild.Id,
|
await _service.AddXpAsync(ctx.Channel.Id,
|
||||||
amount,
|
amount,
|
||||||
role.Members.Select(x => x.Id).ToArray());
|
role.Members.Cast<IGuildUser>().ToArray());
|
||||||
|
|
||||||
await Response()
|
await Response()
|
||||||
.Confirm(
|
.Confirm(strs.xpadd_users(Format.Bold(amount.ToString()), Format.Bold(role.Name)))
|
||||||
strs.xpadd_users(Format.Bold(amount.ToString()), Format.Bold(count.ToString())))
|
.SendAsync();
|
||||||
.SendAsync();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Cmd]
|
[Cmd]
|
||||||
[RequireContext(ContextType.Guild)]
|
[RequireContext(ContextType.Guild)]
|
||||||
[UserPerm(GuildPerm.Administrator)]
|
[UserPerm(GuildPerm.Administrator)]
|
||||||
[Priority(3)]
|
[Priority(3)]
|
||||||
public async Task XpAdd(int amount, ulong userId)
|
public async Task XpAdd(int amount, [Leftover] IGuildUser user)
|
||||||
{
|
{
|
||||||
if (amount == 0)
|
if (amount == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
await _service.AddXpAsync(userId, ctx.Guild.Id, amount);
|
await _service.AddXpAsync(ctx.Channel.Id, amount, user);
|
||||||
var usr = ((SocketGuild)ctx.Guild).GetUser(userId)?.ToString() ?? userId.ToString();
|
await Response().Confirm(strs.modified(Format.Bold(user.ToString()), Format.Bold(amount.ToString()))).SendAsync();
|
||||||
await Response().Confirm(strs.modified(Format.Bold(usr), 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]
|
[Cmd]
|
||||||
[RequireContext(ContextType.Guild)]
|
[RequireContext(ContextType.Guild)]
|
||||||
[OwnerOnly]
|
[OwnerOnly]
|
||||||
|
@ -216,8 +208,8 @@ public partial class Xp : EllieModule<XpService>
|
||||||
public async Task XpReset(ulong userId)
|
public async Task XpReset(ulong userId)
|
||||||
{
|
{
|
||||||
var embed = CreateEmbed()
|
var embed = CreateEmbed()
|
||||||
.WithTitle(GetText(strs.reset))
|
.WithTitle(GetText(strs.reset))
|
||||||
.WithDescription(GetText(strs.reset_user_confirm));
|
.WithDescription(GetText(strs.reset_user_confirm));
|
||||||
|
|
||||||
if (!await PromptUserConfirmAsync(embed))
|
if (!await PromptUserConfirmAsync(embed))
|
||||||
return;
|
return;
|
||||||
|
@ -233,8 +225,8 @@ public partial class Xp : EllieModule<XpService>
|
||||||
public async Task XpReset()
|
public async Task XpReset()
|
||||||
{
|
{
|
||||||
var embed = CreateEmbed()
|
var embed = CreateEmbed()
|
||||||
.WithTitle(GetText(strs.reset))
|
.WithTitle(GetText(strs.reset))
|
||||||
.WithDescription(GetText(strs.reset_server_confirm));
|
.WithDescription(GetText(strs.reset_server_confirm));
|
||||||
|
|
||||||
if (!await PromptUserConfirmAsync(embed))
|
if (!await PromptUserConfirmAsync(embed))
|
||||||
return;
|
return;
|
||||||
|
@ -267,12 +259,12 @@ public partial class Xp : EllieModule<XpService>
|
||||||
}
|
}
|
||||||
|
|
||||||
await Response()
|
await Response()
|
||||||
.Confirm(GetText(strs.available_commands),
|
.Confirm(GetText(strs.available_commands),
|
||||||
$"""
|
$"""
|
||||||
`{prefix}xpshop bgs`
|
`{prefix}xpshop bgs`
|
||||||
`{prefix}xpshop frames`
|
`{prefix}xpshop frames`
|
||||||
""")
|
""")
|
||||||
.SendAsync();
|
.SendAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Cmd]
|
[Cmd]
|
||||||
|
@ -300,80 +292,80 @@ public partial class Xp : EllieModule<XpService>
|
||||||
}
|
}
|
||||||
|
|
||||||
await Response()
|
await Response()
|
||||||
.Paginated()
|
.Paginated()
|
||||||
.Items(allItems)
|
.Items(allItems)
|
||||||
.PageSize(1)
|
.PageSize(1)
|
||||||
.CurrentPage(page)
|
.CurrentPage(page)
|
||||||
.AddFooter(false)
|
.AddFooter(false)
|
||||||
.Page((items, _) =>
|
.Page((items, _) =>
|
||||||
{
|
{
|
||||||
if (!items.Any())
|
if (!items.Any())
|
||||||
return CreateEmbed()
|
return CreateEmbed()
|
||||||
.WithDescription(GetText(strs.not_found))
|
.WithDescription(GetText(strs.not_found))
|
||||||
.WithErrorColor();
|
.WithErrorColor();
|
||||||
|
|
||||||
var (key, item) = items.FirstOrDefault();
|
var (_, item) = items.FirstOrDefault();
|
||||||
|
|
||||||
var eb = CreateEmbed()
|
var eb = CreateEmbed()
|
||||||
.WithOkColor()
|
.WithOkColor()
|
||||||
.WithTitle(item.Name)
|
.WithTitle(item.Name)
|
||||||
.AddField(GetText(strs.price),
|
.AddField(GetText(strs.price),
|
||||||
CurrencyHelper.N(item.Price, Culture, _gss.GetCurrencySign()),
|
CurrencyHelper.N(item.Price, Culture, _gss.GetCurrencySign()),
|
||||||
true)
|
true)
|
||||||
.WithImageUrl(string.IsNullOrWhiteSpace(item.Preview)
|
.WithImageUrl(string.IsNullOrWhiteSpace(item.Preview)
|
||||||
? item.Url
|
? item.Url
|
||||||
: item.Preview);
|
: item.Preview);
|
||||||
|
|
||||||
if (!string.IsNullOrWhiteSpace(item.Desc))
|
if (!string.IsNullOrWhiteSpace(item.Desc))
|
||||||
eb.AddField(GetText(strs.desc), item.Desc);
|
eb.AddField(GetText(strs.desc), item.Desc);
|
||||||
|
|
||||||
return eb;
|
return eb;
|
||||||
})
|
})
|
||||||
.Interaction(async current =>
|
.Interaction(async current =>
|
||||||
{
|
{
|
||||||
var (key, _) = allItems.Skip(current).First();
|
var (key, _) = allItems.Skip(current).First();
|
||||||
|
|
||||||
var itemType = type == XpShopInputType.Backgrounds
|
var itemType = type == XpShopInputType.Backgrounds
|
||||||
? XpShopItemType.Background
|
? XpShopItemType.Background
|
||||||
: XpShopItemType.Frame;
|
: XpShopItemType.Frame;
|
||||||
|
|
||||||
var ownedItem = await _service.GetUserItemAsync(ctx.User.Id, itemType, key);
|
var ownedItem = await _service.GetUserItemAsync(ctx.User.Id, itemType, key);
|
||||||
if (ownedItem is not null)
|
if (ownedItem is not null)
|
||||||
{
|
{
|
||||||
var button = new ButtonBuilder(ownedItem.IsUsing
|
var button = new ButtonBuilder(ownedItem.IsUsing
|
||||||
? GetText(strs.in_use)
|
? GetText(strs.in_use)
|
||||||
: GetText(strs.use),
|
: GetText(strs.use),
|
||||||
"xpshop:use",
|
"xpshop:use",
|
||||||
emote: Emoji.Parse("👐"),
|
emote: Emoji.Parse("👐"),
|
||||||
isDisabled: ownedItem.IsUsing);
|
isDisabled: ownedItem.IsUsing);
|
||||||
|
|
||||||
var inter = _inter.Create(
|
var inter = _inter.Create(
|
||||||
ctx.User.Id,
|
ctx.User.Id,
|
||||||
button,
|
button,
|
||||||
OnShopUse,
|
OnShopUse,
|
||||||
(key, itemType),
|
(key, itemType),
|
||||||
clearAfter: false);
|
clearAfter: false);
|
||||||
|
|
||||||
return inter;
|
return inter;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var button = new ButtonBuilder(GetText(strs.buy),
|
var button = new ButtonBuilder(GetText(strs.buy),
|
||||||
"xpshop:buy",
|
"xpshop:buy",
|
||||||
emote: Emoji.Parse("💰"));
|
emote: Emoji.Parse("💰"));
|
||||||
|
|
||||||
var inter = _inter.Create(
|
var inter = _inter.Create(
|
||||||
ctx.User.Id,
|
ctx.User.Id,
|
||||||
button,
|
button,
|
||||||
OnShopBuy,
|
OnShopBuy,
|
||||||
(key, itemType),
|
(key, itemType),
|
||||||
singleUse: true,
|
singleUse: true,
|
||||||
clearAfter: false);
|
clearAfter: false);
|
||||||
|
|
||||||
return inter;
|
return inter;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.SendAsync();
|
.SendAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Cmd]
|
[Cmd]
|
||||||
|
@ -396,8 +388,8 @@ public partial class Xp : EllieModule<XpService>
|
||||||
{
|
{
|
||||||
BuyResult.XpShopDisabled => await Response().Error(strs.xp_shop_disabled).SendAsync(),
|
BuyResult.XpShopDisabled => await Response().Error(strs.xp_shop_disabled).SendAsync(),
|
||||||
BuyResult.InsufficientFunds => await Response()
|
BuyResult.InsufficientFunds => await Response()
|
||||||
.Error(strs.not_enough(_gss.GetCurrencySign()))
|
.Error(strs.not_enough(_gss.GetCurrencySign()))
|
||||||
.SendAsync(),
|
.SendAsync(),
|
||||||
BuyResult.AlreadyOwned =>
|
BuyResult.AlreadyOwned =>
|
||||||
await Response().Error(strs.xpshop_already_owned).Interaction(GetUseInteraction()).SendAsync(),
|
await Response().Error(strs.xpshop_already_owned).Interaction(GetUseInteraction()).SendAsync(),
|
||||||
BuyResult.UnknownItem => await Response().Error(strs.xpshop_item_not_found).SendAsync(),
|
BuyResult.UnknownItem => await Response().Error(strs.xpshop_item_not_found).SendAsync(),
|
||||||
|
@ -407,10 +399,10 @@ public partial class Xp : EllieModule<XpService>
|
||||||
}
|
}
|
||||||
|
|
||||||
await Response()
|
await Response()
|
||||||
.Confirm(strs.xpshop_buy_success(type.ToString().ToLowerInvariant(),
|
.Confirm(strs.xpshop_buy_success(type.ToString().ToLowerInvariant(),
|
||||||
key.ToLowerInvariant()))
|
key.ToLowerInvariant()))
|
||||||
.Interaction(GetUseInteraction())
|
.Interaction(GetUseInteraction())
|
||||||
.SendAsync();
|
.SendAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Cmd]
|
[Cmd]
|
||||||
|
|
|
@ -551,25 +551,12 @@ public class XpService : IEService, IReadyExecutor, IExecNoCommand
|
||||||
return false;
|
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();
|
foreach (var user in users)
|
||||||
return await ctx.GetTable<UserXpStats>()
|
_usersBatch.Add(new(user, amount, channelId));
|
||||||
.Where(x => x.GuildId == guildId && userIds.Contains(x.UserId))
|
|
||||||
.UpdateAsync(old => new()
|
return Task.CompletedTask;
|
||||||
{
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Task<bool> TryAddUserGainedXpAsync(ulong userId, float cdInMinutes)
|
private Task<bool> TryAddUserGainedXpAsync(ulong userId, float cdInMinutes)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue