Merge pull request #25 from NoiTheCat/dev/ephemeral-confirms

Option for ephemeral (private) individual command confirmations.
Closes #23
This commit is contained in:
Noi 2023-09-10 12:08:49 -07:00 committed by GitHub
commit f20d95c8d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 152 additions and 13 deletions

View file

@ -8,9 +8,12 @@ public class ConfigCommands : CommandsBase {
internal const string HelpUse12 = "Sets whether to use the 12-hour (AM/PM) format in time zone listings.";
internal const string HelpSetFor = "Sets/updates time zone for a given user.";
internal const string HelpRemoveFor = "Removes time zone for a given user.";
internal const string HelpPrivateConfirms = "Sets whether to make set/update confirmations visible only to the user.";
internal const string HelpBool = "True to enable, False to disable.";
[SlashCommand("use-12hour", HelpUse12)]
public async Task Cmd12Hour([Summary(description: "True to enable, False to disable.")] bool setting) {
public async Task Cmd12Hour([Summary(description: HelpBool)] bool setting) {
using var db = DbContext;
var gs = db.GuildSettings.Where(r => r.GuildId == Context.Guild.Id).SingleOrDefault();
if (gs == null) {
@ -19,31 +22,51 @@ public class ConfigCommands : CommandsBase {
}
gs.Use12HourTime = setting;
await db.SaveChangesAsync();
await RespondAsync($":white_check_mark: Time listing set to **{(setting ? "AM/PM" : "24 hour")}** format.");
await db.SaveChangesAsync().ConfigureAwait(false);
await RespondAsync($":white_check_mark: Time listing set to **{(setting ? "AM/PM" : "24 hour")}** format.",
ephemeral: gs.EphemeralConfirm).ConfigureAwait(false);
}
[SlashCommand("private-confirms", HelpPrivateConfirms)]
public async Task PrivateConfirmations([Summary(description: HelpBool)] bool setting) {
using var db = DbContext;
var gs = db.GuildSettings.Where(r => r.GuildId == Context.Guild.Id).SingleOrDefault();
if (gs == null) {
gs = new() { GuildId = Context.Guild.Id };
db.Add(gs);
}
gs.EphemeralConfirm = setting;
await db.SaveChangesAsync().ConfigureAwait(false);
await RespondAsync($":white_check_mark: Private confirmations **{(setting ? "enabled" : "disabled")}**.",
ephemeral: false).ConfigureAwait(false); // Always show this confirmation despite setting
}
[SlashCommand("set-for", HelpSetFor)]
public async Task CmdSetFor([Summary(description: "The user whose time zone to modify.")] SocketGuildUser user,
[Summary(description: "The new time zone to set.")] string zone) {
using var db = DbContext;
// Extract parameters
var newtz = ParseTimeZone(zone);
if (newtz == null) {
await RespondAsync(ErrInvalidZone);
await RespondAsync(ErrInvalidZone,
ephemeral: db.GuildSettings.Where(r => r.GuildId == Context.Guild.Id).SingleOrDefault()?.EphemeralConfirm ?? false)
.ConfigureAwait(false);
return;
}
using var db = DbContext;
db.UpdateUser(user, newtz);
await RespondAsync($":white_check_mark: Time zone for **{user}** set to **{newtz}**.");
await RespondAsync($":white_check_mark: Time zone for **{user}** set to **{newtz}**.").ConfigureAwait(false);
}
[SlashCommand("remove-for", HelpRemoveFor)]
public async Task CmdRemoveFor([Summary(description: "The user whose time zone to remove.")] SocketGuildUser user) {
using var db = DbContext;
if (db.DeleteUser(user))
await RespondAsync($":white_check_mark: Removed zone information for {user}.");
await RespondAsync($":white_check_mark: Removed zone information for {user}.").ConfigureAwait(false);
else
await RespondAsync($":white_check_mark: No time zone is set for {user}.");
await RespondAsync($":white_check_mark: No time zone is set for {user}.",
ephemeral: db.GuildSettings.Where(r => r.GuildId == Context.Guild.Id).SingleOrDefault()?.EphemeralConfirm ?? false)
.ConfigureAwait(false);
}
}

View file

@ -10,6 +10,7 @@ public class UserCommands : CommandsBase {
+ $"`/remove` - {HelpRemove}";
const string EmbedHelpField2 =
$"`/config use-12hour` - {ConfigCommands.HelpUse12}\n"
+ $"`/config private-confirms` - {ConfigCommands.HelpPrivateConfirms}\n"
+ $"`/set-for` - {ConfigCommands.HelpSetFor}\n"
+ $"`/remove-for` - {ConfigCommands.HelpRemoveFor}";
@ -67,7 +68,8 @@ public class UserCommands : CommandsBase {
using var db = DbContext;
var userlist = db.GetGuildZones(Context.Guild.Id);
if (userlist.Count == 0) {
await RespondAsync(":x: Nothing to show. Register your time zones with the bot using the `/set` command.");
await RespondAsync(":x: Nothing to show. Register your time zones with the bot using the `/set` command.",
ephemeral: true).ConfigureAwait(false);
return;
}
@ -157,7 +159,9 @@ public class UserCommands : CommandsBase {
}
using var db = DbContext;
db.UpdateUser((SocketGuildUser)Context.User, parsedzone);
await RespondAsync($":white_check_mark: Your time zone has been set to **{parsedzone}**.");
await RespondAsync($":white_check_mark: Your time zone has been set to **{parsedzone}**.",
ephemeral: db.GuildSettings.Where(r => r.GuildId == Context.Guild.Id).SingleOrDefault()?.EphemeralConfirm ?? false)
.ConfigureAwait(false);
}
[SlashCommand("remove", HelpRemove)]
@ -165,7 +169,11 @@ public class UserCommands : CommandsBase {
public async Task CmdRemove() {
using var db = DbContext;
var success = db.DeleteUser((SocketGuildUser)Context.User);
if (success) await RespondAsync(":white_check_mark: Your zone has been removed.");
else await RespondAsync(":x: You don't have a time zone set.");
if (success) await RespondAsync(":white_check_mark: Your zone has been removed.",
ephemeral: db.GuildSettings.Where(r => r.GuildId == Context.Guild.Id).SingleOrDefault()?.EphemeralConfirm ?? false)
.ConfigureAwait(false);
else await RespondAsync(":x: You don't have a time zone set.",
ephemeral: db.GuildSettings.Where(r => r.GuildId == Context.Guild.Id).SingleOrDefault()?.EphemeralConfirm ?? false)
.ConfigureAwait(false);
}
}

View file

@ -6,4 +6,6 @@ public class GuildConfiguration {
public ulong GuildId { get; set; }
public bool Use12HourTime { get; set; }
public bool EphemeralConfirm { get; set; }
}

View file

@ -0,0 +1,73 @@
// <auto-generated />
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using WorldTime.Data;
#nullable disable
namespace WorldTime.Data.Migrations
{
[DbContext(typeof(BotDatabaseContext))]
[Migration("20230909184554_AddEphemeral")]
partial class AddEphemeral
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.2")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
modelBuilder.Entity("WorldTime.Data.GuildConfiguration", b =>
{
b.Property<decimal>("GuildId")
.ValueGeneratedOnAdd()
.HasColumnType("numeric(20,0)")
.HasColumnName("guild_id");
b.Property<bool>("EphemeralConfirm")
.HasColumnType("boolean")
.HasColumnName("ephemeral_confirm");
b.Property<bool>("Use12HourTime")
.ValueGeneratedOnAdd()
.HasColumnType("boolean")
.HasDefaultValue(false)
.HasColumnName("use12hour_time");
b.HasKey("GuildId")
.HasName("pk_guild_settings");
b.ToTable("guild_settings", (string)null);
});
modelBuilder.Entity("WorldTime.Data.UserEntry", b =>
{
b.Property<long>("GuildId")
.HasColumnType("bigint")
.HasColumnName("guild_id");
b.Property<long>("UserId")
.HasColumnType("bigint")
.HasColumnName("user_id");
b.Property<string>("TimeZone")
.IsRequired()
.HasColumnType("text")
.HasColumnName("zone");
b.HasKey("GuildId", "UserId")
.HasName("userdata_pkey");
b.ToTable("userdata", (string)null);
});
#pragma warning restore 612, 618
}
}
}

View file

@ -0,0 +1,29 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace WorldTime.Data.Migrations
{
/// <inheritdoc />
public partial class AddEphemeral : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<bool>(
name: "ephemeral_confirm",
table: "guild_settings",
type: "boolean",
nullable: false,
defaultValue: false);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "ephemeral_confirm",
table: "guild_settings");
}
}
}

View file

@ -16,7 +16,7 @@ namespace WorldTime.Data.Migrations
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.1")
.HasAnnotation("ProductVersion", "7.0.2")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
@ -28,6 +28,10 @@ namespace WorldTime.Data.Migrations
.HasColumnType("numeric(20,0)")
.HasColumnName("guild_id");
b.Property<bool>("EphemeralConfirm")
.HasColumnType("boolean")
.HasColumnName("ephemeral_confirm");
b.Property<bool>("Use12HourTime")
.ValueGeneratedOnAdd()
.HasColumnType("boolean")