1
Fork 0
mirror of https://gitlab.com/Kwoth/nadekobot.git synced 2024-10-02 20:13:13 +00:00
This commit is contained in:
Kwoth 2023-09-05 23:22:31 +00:00
commit f35b1a9219
30 changed files with 21096 additions and 41 deletions

View file

@ -1,4 +1,6 @@
#nullable disable
using NadekoBot.Services.Database.Models;
namespace NadekoBot.Modules.Utility.Services;
public interface IRemindService
@ -8,5 +10,6 @@ public interface IRemindService
ulong? guildId,
bool isPrivate,
DateTime time,
string message);
string message,
ReminderType reminderType);
}

View file

@ -9,4 +9,11 @@ public class Reminder : DbEntity
public ulong UserId { get; set; }
public string Message { get; set; }
public bool IsPrivate { get; set; }
public ReminderType Type { get; set; }
}
public enum ReminderType
{
User,
Timely
}

View file

@ -149,10 +149,24 @@ public partial class Gambling : GamblingModule<GamblingService>
ctx.Guild?.Id,
true,
when,
GetText(strs.timely_time));
GetText(strs.timely_time),
ReminderType.Timely);
await smc.RespondConfirmAsync(_eb, GetText(strs.remind_timely(tt)), ephemeral: true);
}
private NadekoInteraction CreateRemindMeInteraction(int period)
{
return _inter
.Create(ctx.User.Id,
new SimpleInteraction<DateTime>(
new ButtonBuilder(
label: "Remind me",
emote: Emoji.Parse("⏰"),
customId: "timely:remind_me"),
RemindTimelyAction,
DateTime.UtcNow.Add(TimeSpan.FromHours(period))));
}
[Cmd]
public async Task Timely()
@ -164,12 +178,20 @@ public partial class Gambling : GamblingModule<GamblingService>
await ReplyErrorLocalizedAsync(strs.timely_none);
return;
}
var inter = CreateRemindMeInteraction(period);
if (await _service.ClaimTimelyAsync(ctx.User.Id, period) is { } rem)
{
// Removes timely button if there is a timely reminder in DB
if (_service.UserHasTimelyReminder(ctx.User.Id))
{
inter = null;
}
var now = DateTime.UtcNow;
var relativeTag = TimestampTag.FromDateTime(now.Add(rem), TimestampTagStyles.Relative);
await ReplyPendingLocalizedAsync(strs.timely_already_claimed(relativeTag));
await ReplyPendingLocalizedAsync(strs.timely_already_claimed(relativeTag), inter);
return;
}
@ -179,19 +201,9 @@ public partial class Gambling : GamblingModule<GamblingService>
await _cs.AddAsync(ctx.User.Id, val, new("timely", "claim"));
var inter = _inter
.Create(ctx.User.Id,
new SimpleInteraction<DateTime>(
new ButtonBuilder(
label: "Remind me",
emote: Emoji.Parse("⏰"),
customId: "timely:remind_me"),
RemindTimelyAction,
DateTime.UtcNow.Add(TimeSpan.FromHours(period))));
await ReplyConfirmLocalizedAsync(strs.timely(N(val), period), inter);
}
[Cmd]
[OwnerOnly]
public async Task TimelyReset()

View file

@ -209,6 +209,13 @@ public class GamblingService : INService, IReadyExecutor
}
}
public bool UserHasTimelyReminder(ulong userId)
{
var db = _db.GetDbContext();
return db.GetTable<Reminder>().Any(x => x.UserId == userId
&& x.Type == ReminderType.Timely);
}
public async Task RemoveAllTimelyClaimsAsync()
=> await _cache.RemoveAsync(_timelyKey);
}

View file

@ -269,8 +269,9 @@ public partial class Gambling
? "-"
: string.Join("\n",
itemList.Where(x => waifuItems.TryGetValue(x.ItemEmoji, out _))
.OrderBy(x => waifuItems[x.ItemEmoji].Price)
.OrderByDescending(x => waifuItems[x.ItemEmoji].Price)
.GroupBy(x => x.ItemEmoji)
.Take(60)
.Select(x => $"{x.Key} x{x.Count(),-3}")
.Chunk(2)
.Select(x => string.Join(" ", x)));
@ -283,8 +284,10 @@ public partial class Gambling
var fansList = await _service.GetFansNames(wi.WaifuId);
var fansStr = fansList
.Select((x) => claimsNames.Contains(x) ? $"{x} 💞" : x)
.Join('\n');
.Shuffle()
.Take(30)
.Select((x) => claimsNames.Contains(x) ? $"{x} 💞" : x)
.Join('\n');
if (string.IsNullOrWhiteSpace(fansStr))
@ -370,4 +373,4 @@ public partial class Gambling
await ReplyErrorLocalizedAsync(strs.not_enough(CurrencySign));
}
}
}
}

View file

@ -49,7 +49,8 @@ public partial class Utility
if (!await RemindInternal(target,
meorhere == MeOrHere.Me || ctx.Guild is null,
remindData.Time,
remindData.What))
remindData.What,
ReminderType.User))
await ReplyErrorLocalizedAsync(strs.remind_too_long);
}
@ -73,7 +74,7 @@ public partial class Utility
}
if (!await RemindInternal(channel.Id, false, remindData.Time, remindData.What))
if (!await RemindInternal(channel.Id, false, remindData.Time, remindData.What, ReminderType.User))
await ReplyErrorLocalizedAsync(strs.remind_too_long);
}
@ -172,7 +173,8 @@ public partial class Utility
ulong targetId,
bool isPrivate,
TimeSpan ts,
string message)
string message,
ReminderType reminderType)
{
var time = DateTime.UtcNow + ts;

View file

@ -232,7 +232,8 @@ public class RemindService : INService, IReadyExecutor, IRemindService
ulong? guildId,
bool isPrivate,
DateTime time,
string message)
string message,
ReminderType reminderType)
{
var rem = new Reminder
{
@ -242,6 +243,7 @@ public class RemindService : INService, IReadyExecutor, IRemindService
IsPrivate = isPrivate,
When = time,
Message = message,
Type = reminderType
};
await using var ctx = _db.GetDbContext();

View file

@ -58,17 +58,17 @@ public partial class Xp
[Cmd]
public async Task ClubCreate([Leftover] string clubName)
{
if (string.IsNullOrWhiteSpace(clubName) || clubName.Length > 20)
var result = await _service.CreateClubAsync(ctx.User, clubName);
if (result == ClubCreateResult.NameTooLong)
{
await ReplyErrorLocalizedAsync(strs.club_name_too_long);
return;
}
var result = await _service.CreateClubAsync(ctx.User, clubName);
if (result == ClubCreateResult.NameTaken)
{
await ReplyErrorLocalizedAsync(strs.club_create_error_name);
await ReplyErrorLocalizedAsync(strs.club_name_taken);
return;
}
@ -420,5 +420,32 @@ public partial class Xp
return ctx.Channel.EmbedAsync(embed);
}
[Cmd]
public async Task ClubRename([Leftover] string clubName)
{
var res = await _service.RenameClubAsync(ctx.User.Id, clubName);
switch (res)
{
case ClubRenameResult.NameTooLong:
await ReplyErrorLocalizedAsync(strs.club_name_too_long);
return;
case ClubRenameResult.Success:
{
var embed = _eb.Create().WithTitle(GetText(strs.club_renamed(clubName))).WithOkColor();
await ctx.Channel.EmbedAsync(embed);
return;
}
case ClubRenameResult.NameTaken:
await ReplyErrorLocalizedAsync(strs.club_name_taken);
return;
case ClubRenameResult.NotOwnerOrAdmin:
await ReplyErrorLocalizedAsync(strs.club_admin_perms);
return;
default:
return;
}
}
}
}

View file

@ -22,6 +22,9 @@ public class ClubService : INService, IClubService
public async Task<ClubCreateResult> CreateClubAsync(IUser user, string clubName)
{
if (!CheckClubName(clubName))
return ClubCreateResult.NameTooLong;
//must be lvl 5 and must not be in a club already
await using var uow = _db.GetDbContext();
@ -316,4 +319,31 @@ public class ClubService : INService, IClubService
using var uow = _db.GetDbContext();
return uow.Set<ClubInfo>().GetClubLeaderboardPage(page);
}
public async Task<ClubRenameResult> RenameClubAsync(ulong userId, string clubName)
{
if (!CheckClubName(clubName))
return ClubRenameResult.NameTooLong;
await using var uow = _db.GetDbContext();
var club = uow.Set<ClubInfo>().GetByOwnerOrAdmin(userId);
if (club is null)
return ClubRenameResult.NotOwnerOrAdmin;
if (await uow.Set<ClubInfo>().AnyAsyncEF(x => x.Name == clubName))
return ClubRenameResult.NameTaken;
club.Name = clubName;
await uow.SaveChangesAsync();
return ClubRenameResult.Success;
}
private static bool CheckClubName(string clubName)
{
return !(string.IsNullOrWhiteSpace(clubName) || clubName.Length > 20);
}
}

View file

@ -21,6 +21,7 @@ public interface IClubService
ClubUnbanResult UnBan(ulong ownerUserId, string userName, out ClubInfo club);
ClubKickResult Kick(ulong kickerId, string userName, out ClubInfo club);
List<ClubInfo> GetClubLeaderboardPage(int page);
Task<ClubRenameResult> RenameClubAsync(ulong userId, string clubName);
}
public enum ClubApplyResult

View file

@ -6,4 +6,5 @@ public enum ClubCreateResult
AlreadyInAClub,
NameTaken,
InsufficientLevel,
NameTooLong
}

View file

@ -0,0 +1,9 @@
namespace NadekoBot.Modules.Xp.Services;
public enum ClubRenameResult
{
NotOwnerOrAdmin,
Success,
NameTaken,
NameTooLong
}

View file

@ -238,10 +238,11 @@ public abstract class NadekoContext : DbContext
.HasForeignKey<ClubInfo>(x => x.OwnerId)
.OnDelete(DeleteBehavior.SetNull);
ci.HasAlternateKey(x => new
{
x.Name
});
ci.HasIndex(x => new
{
x.Name
})
.IsUnique();
#endregion

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,29 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace NadekoBot.Db.Migrations.Mysql
{
/// <inheritdoc />
public partial class timelyremindertype : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "type",
table: "reminders",
type: "int",
nullable: false,
defaultValue: 0);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "type",
table: "reminders");
}
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,73 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace NadekoBot.Db.Migrations.Mysql
{
/// <inheritdoc />
public partial class clubinfonameindex : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropUniqueConstraint(
name: "ak_clubs_name",
table: "clubs");
migrationBuilder.AlterColumn<string>(
name: "name",
table: "clubs",
type: "varchar(20)",
maxLength: 20,
nullable: true,
collation: "utf8mb4_bin",
oldClrType: typeof(string),
oldType: "varchar(20)",
oldMaxLength: 20,
oldCollation: "utf8mb4_bin")
.Annotation("MySql:CharSet", "utf8mb4")
.OldAnnotation("MySql:CharSet", "utf8mb4");
migrationBuilder.CreateIndex(
name: "ix_clubs_name",
table: "clubs",
column: "name",
unique: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropIndex(
name: "ix_clubs_name",
table: "clubs");
migrationBuilder.UpdateData(
table: "clubs",
keyColumn: "name",
keyValue: null,
column: "name",
value: "");
migrationBuilder.AlterColumn<string>(
name: "name",
table: "clubs",
type: "varchar(20)",
maxLength: 20,
nullable: false,
collation: "utf8mb4_bin",
oldClrType: typeof(string),
oldType: "varchar(20)",
oldMaxLength: 20,
oldNullable: true,
oldCollation: "utf8mb4_bin")
.Annotation("MySql:CharSet", "utf8mb4")
.OldAnnotation("MySql:CharSet", "utf8mb4");
migrationBuilder.AddUniqueConstraint(
name: "ak_clubs_name",
table: "clubs",
column: "name");
}
}
}

View file

@ -135,7 +135,6 @@ namespace NadekoBot.Db.Migrations.Mysql
.HasColumnName("imageurl");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(20)
.HasColumnType("varchar(20)")
.HasColumnName("name")
@ -152,8 +151,9 @@ namespace NadekoBot.Db.Migrations.Mysql
b.HasKey("Id")
.HasName("pk_clubs");
b.HasAlternateKey("Name")
.HasName("ak_clubs_name");
b.HasIndex("Name")
.IsUnique()
.HasDatabaseName("ix_clubs_name");
b.HasIndex("OwnerId")
.IsUnique()
@ -2112,6 +2112,10 @@ namespace NadekoBot.Db.Migrations.Mysql
.HasColumnType("bigint unsigned")
.HasColumnName("serverid");
b.Property<int>("Type")
.HasColumnType("int")
.HasColumnName("type");
b.Property<ulong>("UserId")
.HasColumnType("bigint unsigned")
.HasColumnName("userid");

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,29 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace NadekoBot.Db.Migrations
{
/// <inheritdoc />
public partial class timelyremindertype : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "type",
table: "reminders",
type: "integer",
nullable: false,
defaultValue: 0);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "type",
table: "reminders");
}
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,59 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace NadekoBot.Db.Migrations
{
/// <inheritdoc />
public partial class clubinfonameindex : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropUniqueConstraint(
name: "ak_clubs_name",
table: "clubs");
migrationBuilder.AlterColumn<string>(
name: "name",
table: "clubs",
type: "character varying(20)",
maxLength: 20,
nullable: true,
oldClrType: typeof(string),
oldType: "character varying(20)",
oldMaxLength: 20);
migrationBuilder.CreateIndex(
name: "ix_clubs_name",
table: "clubs",
column: "name",
unique: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropIndex(
name: "ix_clubs_name",
table: "clubs");
migrationBuilder.AlterColumn<string>(
name: "name",
table: "clubs",
type: "character varying(20)",
maxLength: 20,
nullable: false,
defaultValue: "",
oldClrType: typeof(string),
oldType: "character varying(20)",
oldMaxLength: 20,
oldNullable: true);
migrationBuilder.AddUniqueConstraint(
name: "ak_clubs_name",
table: "clubs",
column: "name");
}
}
}

View file

@ -17,7 +17,7 @@ namespace NadekoBot.Db.Migrations
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "6.0.7")
.HasAnnotation("ProductVersion", "7.0.4")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
@ -144,7 +144,6 @@ namespace NadekoBot.Db.Migrations
.HasColumnName("imageurl");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(20)
.HasColumnType("character varying(20)")
.HasColumnName("name");
@ -160,8 +159,9 @@ namespace NadekoBot.Db.Migrations
b.HasKey("Id")
.HasName("pk_clubs");
b.HasAlternateKey("Name")
.HasName("ak_clubs_name");
b.HasIndex("Name")
.IsUnique()
.HasDatabaseName("ix_clubs_name");
b.HasIndex("OwnerId")
.IsUnique()
@ -2212,6 +2212,10 @@ namespace NadekoBot.Db.Migrations
.HasColumnType("numeric(20,0)")
.HasColumnName("serverid");
b.Property<int>("Type")
.HasColumnType("integer")
.HasColumnName("type");
b.Property<decimal>("UserId")
.HasColumnType("numeric(20,0)")
.HasColumnName("userid");

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,29 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace NadekoBot.Db.Migrations.Sqlite
{
/// <inheritdoc />
public partial class timelyremindertype : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "Type",
table: "Reminders",
type: "INTEGER",
nullable: false,
defaultValue: 0);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "Type",
table: "Reminders");
}
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,59 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace NadekoBot.Db.Migrations.Sqlite
{
/// <inheritdoc />
public partial class clubinfonameindex : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropUniqueConstraint(
name: "AK_Clubs_Name",
table: "Clubs");
migrationBuilder.AlterColumn<string>(
name: "Name",
table: "Clubs",
type: "TEXT",
maxLength: 20,
nullable: true,
oldClrType: typeof(string),
oldType: "TEXT",
oldMaxLength: 20);
migrationBuilder.CreateIndex(
name: "IX_Clubs_Name",
table: "Clubs",
column: "Name",
unique: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropIndex(
name: "IX_Clubs_Name",
table: "Clubs");
migrationBuilder.AlterColumn<string>(
name: "Name",
table: "Clubs",
type: "TEXT",
maxLength: 20,
nullable: false,
defaultValue: "",
oldClrType: typeof(string),
oldType: "TEXT",
oldMaxLength: 20,
oldNullable: true);
migrationBuilder.AddUniqueConstraint(
name: "AK_Clubs_Name",
table: "Clubs",
column: "Name");
}
}
}

View file

@ -109,7 +109,6 @@ namespace NadekoBot.Db.Migrations.Sqlite
.HasColumnType("TEXT");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(20)
.HasColumnType("TEXT");
@ -121,7 +120,8 @@ namespace NadekoBot.Db.Migrations.Sqlite
b.HasKey("Id");
b.HasAlternateKey("Name");
b.HasIndex("Name")
.IsUnique();
b.HasIndex("OwnerId")
.IsUnique();
@ -1648,6 +1648,9 @@ namespace NadekoBot.Db.Migrations.Sqlite
b.Property<ulong>("ServerId")
.HasColumnType("INTEGER");
b.Property<int>("Type")
.HasColumnType("INTEGER");
b.Property<ulong>("UserId")
.HasColumnType("INTEGER");

View file

@ -2364,3 +2364,7 @@ doas:
desc: "Execute the command as if you were the target user. Requires bot ownership and server administrator permission."
args:
- "@Thief .give all @Admin"
clubrename:
desc: "Renames your club. Requires you club ownership or club-admin status."
args:
- "New cool club name"

View file

@ -841,7 +841,6 @@
"club_insuff_lvl": "You're insufficient level to join that club.",
"club_join_banned": "You're banned from that club.",
"club_already_in": "You are already a member of a club.",
"club_create_error_name": "Failed creating the club. A club with that name already exists.",
"club_name_too_long": "Club name is too long.",
"club_created": "Club {0} successfully created!",
"club_create_insuff_lvl": "You don't meet the minimum level requirements in order to create a club.",
@ -875,6 +874,8 @@
"club_apps_for": "Applicants for {0} club",
"club_leaderboard": "Club leaderboard - page {0}",
"club_kick_hierarchy": "Only club owner can kick club admins. Owner can't be kicked.",
"club_renamed": "Club has been renamed to {0}",
"club_name_taken": "A club with that name already exists.",
"template_reloaded": "Xp template has been reloaded.",
"expr_edited": "Expression Edited",
"self_assign_are_exclusive": "You can only choose 1 role from each group.",