Skip to content

Commit

Permalink
Add !debug overrides importall
Browse files Browse the repository at this point in the history
  • Loading branch information
FloatingMilkshake committed Jul 4, 2024
1 parent dec0ebd commit 92f9d63
Showing 1 changed file with 77 additions and 39 deletions.
116 changes: 77 additions & 39 deletions Commands/Debug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -355,48 +355,40 @@ await ctx.RespondAsync(new DiscordMessageBuilder().WithContent(response)
public async Task Import(CommandContext ctx,
[Description("The channel to import overrides from.")] DiscordChannel channel)
{
// Import all overrides for channel to db
foreach (var overwrite in channel.PermissionOverwrites)
{
// Ignore role overrides
if (overwrite.Type == DiscordOverwriteType.Role) continue;
// Import overrides
var (success, failedOverwrite) = await ImportOverridesFromChannelAsync(channel);

if (success)
await ctx.RespondAsync($"{Program.cfgjson.Emoji.Success} Overrides for {channel.Mention} imported successfully!");
else
await ctx.RespondAsync(
$"{Program.cfgjson.Emoji.Error} Something went wrong while trying to fetch the overrides for {failedOverwrite}!" +
" There are overrides in the database but I could not parse them. Check the database manually for details.");
}

// Get user's current overrides from db
var userOverrides = await Program.db.HashGetAsync("overrides", overwrite.Id.ToString());
if (string.IsNullOrWhiteSpace(userOverrides))
{
// User doesn't have any overrides in db, so just add the new one
await Program.db.HashSetAsync("overrides", overwrite.Id.ToString(),
JsonConvert.SerializeObject(new Dictionary<ulong, DiscordOverwrite>
{
{ channel.Id, overwrite }
}));
}
else
{
// User has overrides in db, so add the new one to the existing ones
var overwrites =
JsonConvert.DeserializeObject<Dictionary<ulong, DiscordOverwrite>>(userOverrides);
if (overwrites is null)
{
await ctx.RespondAsync(
$"{Program.cfgjson.Emoji.Error} Something went wrong while trying to fetch the overrides for {overwrite.Id}!" +
" There are overrides in the database but I could not parse them. Check the database manually for details.");
return;
}

if (overwrites.ContainsKey(channel.Id))
overwrites[channel.Id] = overwrite;
else
overwrites.Add(channel.Id, overwrite);
[Command("importall")]
[Description("Import all overrides from all channels to the database.")]
public async Task ImportAll(CommandContext ctx)
{
var msg = await ctx.RespondAsync($"{Program.cfgjson.Emoji.Loading} Working...");

// Get all channels
var channels = await ctx.Guild.GetChannelsAsync();

// Update the db
await Program.db.HashSetAsync("overrides", overwrite.Id.ToString(),
JsonConvert.SerializeObject(overwrites));
}
}
bool anyImportFailed = false;

await ctx.RespondAsync($"{Program.cfgjson.Emoji.Success} Overrides for {channel.Mention} imported successfully!");
foreach (var channel in channels)
{
// Import overrides
var (success, failedOverwrite) = await ImportOverridesFromChannelAsync(channel);

if (!success) anyImportFailed = true;
}

if (anyImportFailed)
await msg.ModifyAsync($"{Program.cfgjson.Emoji.Error} Some overrides failed to import. Most likely this means I found overrides in the database but couldn't parse them. Check the database manually for details.");
else
await msg.ModifyAsync($"{Program.cfgjson.Emoji.Success} All overrides imported successfully!");
}

[Command("remove")]
Expand Down Expand Up @@ -439,6 +431,52 @@ public async Task GetDMChannel(CommandContext ctx, DiscordUser user)
await ctx.RespondAsync(dmChannel.Id.ToString());
}

private static async Task<(bool success, ulong failedOverwrite)> ImportOverridesFromChannelAsync(DiscordChannel channel)
{
// Imports overrides from the specified channel to the database. See 'debug overrides import' and 'debug overrides importall'
// Return (true, 0) on success, (false, <ID of failed overwrite>) on failure

// Import all overrides for channel to db
foreach (var overwrite in channel.PermissionOverwrites)
{
// Ignore role overrides
if (overwrite.Type == DiscordOverwriteType.Role) continue;

// Get user's current overrides from db
var userOverrides = await Program.db.HashGetAsync("overrides", overwrite.Id.ToString());
if (string.IsNullOrWhiteSpace(userOverrides))
{
// User doesn't have any overrides in db, so just add the new one
await Program.db.HashSetAsync("overrides", overwrite.Id.ToString(),
JsonConvert.SerializeObject(new Dictionary<ulong, DiscordOverwrite>
{
{ channel.Id, overwrite }
}));
}
else
{
// User has overrides in db, so add the new one to the existing ones
var overwrites =
JsonConvert.DeserializeObject<Dictionary<ulong, DiscordOverwrite>>(userOverrides);
if (overwrites is null)
{
return (false, overwrite.Id);
}

if (overwrites.ContainsKey(channel.Id))
overwrites[channel.Id] = overwrite;
else
overwrites.Add(channel.Id, overwrite);

// Update the db
await Program.db.HashSetAsync("overrides", overwrite.Id.ToString(),
JsonConvert.SerializeObject(overwrites));
}
}

return (true, 0);
}

}

}
Expand Down

0 comments on commit 92f9d63

Please sign in to comment.