feat: split ignores into 'ignore messages' and 'ignore entities'
This commit is contained in:
parent
d48ab7e16e
commit
0cac964aa6
32 changed files with 730 additions and 488 deletions
|
|
@ -116,6 +116,7 @@ public class DatabasePool
|
|||
|
||||
SqlMapper.AddTypeHandler(new PassthroughTypeHandler<Instant>());
|
||||
SqlMapper.AddTypeHandler(new JsonTypeHandler<Guild.ChannelConfig>());
|
||||
SqlMapper.AddTypeHandler(new JsonTypeHandler<Guild.MessageConfig>());
|
||||
}
|
||||
|
||||
// Copied from PluralKit:
|
||||
|
|
|
|||
|
|
@ -0,0 +1,3 @@
|
|||
update guilds set channels = (channels || messages) - 'IgnoredRoles';
|
||||
|
||||
alter table guilds drop column messages;
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
alter table guilds
|
||||
add column messages jsonb not null default '{}';
|
||||
|
||||
-- Extract the current message-related configuration options into the new "messages" column
|
||||
-- noinspection SqlWithoutWhere
|
||||
update guilds
|
||||
set messages = jsonb_build_object('IgnoredUsers', channels['IgnoredUsers'], 'IgnoredChannels',
|
||||
channels['IgnoredChannels'], 'IgnoredUsersPerChannel',
|
||||
channels['IgnoredUsersPerChannel']);
|
||||
|
||||
-- We don't update the "channels" column as it will be cleared out automatically over time,
|
||||
-- as channel configurations are updated by the bot
|
||||
|
|
@ -19,6 +19,7 @@ public class ChannelsBackup
|
|||
{
|
||||
public List<ulong> IgnoredChannels { get; init; } = [];
|
||||
public List<ulong> IgnoredUsers { get; init; } = [];
|
||||
public List<ulong> IgnoredRoles { get; init; } = [];
|
||||
public Dictionary<ulong, List<ulong>> IgnoredUsersPerChannel { get; init; } = [];
|
||||
public Dictionary<ulong, ulong> Redirects { get; init; } = [];
|
||||
|
||||
|
|
@ -46,12 +47,18 @@ public class ChannelsBackup
|
|||
public ulong MessageDelete { get; init; }
|
||||
public ulong MessageDeleteBulk { get; init; }
|
||||
|
||||
public Guild.ChannelConfig ToGuildConfig() =>
|
||||
public Guild.MessageConfig ToMessageConfig() =>
|
||||
new()
|
||||
{
|
||||
IgnoredChannels = IgnoredChannels,
|
||||
IgnoredUsers = IgnoredUsers,
|
||||
IgnoredRoles = IgnoredRoles,
|
||||
IgnoredUsersPerChannel = IgnoredUsersPerChannel,
|
||||
};
|
||||
|
||||
public Guild.ChannelConfig ToChannelConfig() =>
|
||||
new()
|
||||
{
|
||||
Redirects = Redirects,
|
||||
GuildUpdate = GuildUpdate,
|
||||
GuildEmojisUpdate = GuildEmojisUpdate,
|
||||
|
|
@ -78,35 +85,36 @@ public class ChannelsBackup
|
|||
MessageDeleteBulk = MessageDeleteBulk,
|
||||
};
|
||||
|
||||
public static ChannelsBackup FromGuildConfig(Guild.ChannelConfig channels) =>
|
||||
public static ChannelsBackup FromGuildConfig(Guild guild) =>
|
||||
new()
|
||||
{
|
||||
IgnoredChannels = channels.IgnoredChannels,
|
||||
IgnoredUsers = channels.IgnoredUsers,
|
||||
IgnoredUsersPerChannel = channels.IgnoredUsersPerChannel,
|
||||
Redirects = channels.Redirects,
|
||||
GuildUpdate = channels.GuildUpdate,
|
||||
GuildEmojisUpdate = channels.GuildEmojisUpdate,
|
||||
GuildRoleCreate = channels.GuildRoleCreate,
|
||||
GuildRoleUpdate = channels.GuildRoleUpdate,
|
||||
GuildRoleDelete = channels.GuildRoleDelete,
|
||||
ChannelCreate = channels.ChannelCreate,
|
||||
ChannelUpdate = channels.ChannelUpdate,
|
||||
ChannelDelete = channels.ChannelDelete,
|
||||
GuildMemberAdd = channels.GuildMemberAdd,
|
||||
GuildMemberUpdate = channels.GuildMemberUpdate,
|
||||
GuildKeyRoleUpdate = channels.GuildKeyRoleUpdate,
|
||||
GuildMemberNickUpdate = channels.GuildMemberNickUpdate,
|
||||
GuildMemberAvatarUpdate = channels.GuildMemberAvatarUpdate,
|
||||
GuildMemberTimeout = channels.GuildMemberTimeout,
|
||||
GuildMemberRemove = channels.GuildMemberRemove,
|
||||
GuildMemberKick = channels.GuildMemberKick,
|
||||
GuildBanAdd = channels.GuildBanAdd,
|
||||
GuildBanRemove = channels.GuildBanRemove,
|
||||
InviteCreate = channels.InviteCreate,
|
||||
InviteDelete = channels.InviteDelete,
|
||||
MessageUpdate = channels.MessageUpdate,
|
||||
MessageDelete = channels.MessageDelete,
|
||||
MessageDeleteBulk = channels.MessageDeleteBulk,
|
||||
IgnoredChannels = guild.Messages.IgnoredChannels,
|
||||
IgnoredUsers = guild.Messages.IgnoredUsers,
|
||||
IgnoredRoles = guild.Messages.IgnoredRoles,
|
||||
IgnoredUsersPerChannel = guild.Messages.IgnoredUsersPerChannel,
|
||||
Redirects = guild.Channels.Redirects,
|
||||
GuildUpdate = guild.Channels.GuildUpdate,
|
||||
GuildEmojisUpdate = guild.Channels.GuildEmojisUpdate,
|
||||
GuildRoleCreate = guild.Channels.GuildRoleCreate,
|
||||
GuildRoleUpdate = guild.Channels.GuildRoleUpdate,
|
||||
GuildRoleDelete = guild.Channels.GuildRoleDelete,
|
||||
ChannelCreate = guild.Channels.ChannelCreate,
|
||||
ChannelUpdate = guild.Channels.ChannelUpdate,
|
||||
ChannelDelete = guild.Channels.ChannelDelete,
|
||||
GuildMemberAdd = guild.Channels.GuildMemberAdd,
|
||||
GuildMemberUpdate = guild.Channels.GuildMemberUpdate,
|
||||
GuildKeyRoleUpdate = guild.Channels.GuildKeyRoleUpdate,
|
||||
GuildMemberNickUpdate = guild.Channels.GuildMemberNickUpdate,
|
||||
GuildMemberAvatarUpdate = guild.Channels.GuildMemberAvatarUpdate,
|
||||
GuildMemberTimeout = guild.Channels.GuildMemberTimeout,
|
||||
GuildMemberRemove = guild.Channels.GuildMemberRemove,
|
||||
GuildMemberKick = guild.Channels.GuildMemberKick,
|
||||
GuildBanAdd = guild.Channels.GuildBanAdd,
|
||||
GuildBanRemove = guild.Channels.GuildBanRemove,
|
||||
InviteCreate = guild.Channels.InviteCreate,
|
||||
InviteDelete = guild.Channels.InviteDelete,
|
||||
MessageUpdate = guild.Channels.MessageUpdate,
|
||||
MessageDelete = guild.Channels.MessageDelete,
|
||||
MessageDeleteBulk = guild.Channels.MessageDeleteBulk,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,18 +24,28 @@ public class Guild
|
|||
public required ulong Id { get; init; }
|
||||
|
||||
public ChannelConfig Channels { get; init; } = new();
|
||||
public MessageConfig Messages { get; init; } = new();
|
||||
public string[] BannedSystems { get; set; } = [];
|
||||
public ulong[] KeyRoles { get; set; } = [];
|
||||
|
||||
// These channels and roles are ignored for channel/role update/delete events.
|
||||
public ulong[] IgnoredChannels { get; set; } = [];
|
||||
public ulong[] IgnoredRoles { get; set; } = [];
|
||||
|
||||
public bool IsSystemBanned(PluralkitApiService.PkSystem system) =>
|
||||
BannedSystems.Contains(system.Id) || BannedSystems.Contains(system.Uuid.ToString());
|
||||
|
||||
public bool IsMessageIgnored(Snowflake channelId, Snowflake? userId)
|
||||
public bool IsMessageIgnored(
|
||||
Snowflake channelId,
|
||||
Snowflake? userId,
|
||||
IReadOnlyList<Snowflake>? roleIds
|
||||
)
|
||||
{
|
||||
if (
|
||||
Channels is { MessageDelete: 0, MessageUpdate: 0, MessageDeleteBulk: 0 }
|
||||
|| Channels.IgnoredChannels.Contains(channelId.ToUlong())
|
||||
|| (userId != null && Channels.IgnoredUsers.Contains(userId.Value.ToUlong()))
|
||||
|| Messages.IgnoredChannels.Contains(channelId.ToUlong())
|
||||
|| (userId != null && Messages.IgnoredUsers.Contains(userId.Value.ToUlong()))
|
||||
|| (roleIds != null && roleIds.Any(r => Messages.IgnoredRoles.Any(id => r.Value == id)))
|
||||
)
|
||||
return true;
|
||||
|
||||
|
|
@ -43,7 +53,7 @@ public class Guild
|
|||
return false;
|
||||
|
||||
if (
|
||||
Channels.IgnoredUsersPerChannel.TryGetValue(
|
||||
Messages.IgnoredUsersPerChannel.TryGetValue(
|
||||
channelId.ToUlong(),
|
||||
out var thisChannelIgnoredUsers
|
||||
)
|
||||
|
|
@ -53,11 +63,16 @@ public class Guild
|
|||
return false;
|
||||
}
|
||||
|
||||
public class ChannelConfig
|
||||
public class MessageConfig
|
||||
{
|
||||
public List<ulong> IgnoredChannels { get; set; } = [];
|
||||
public List<ulong> IgnoredRoles { get; set; } = [];
|
||||
public List<ulong> IgnoredUsers { get; init; } = [];
|
||||
public Dictionary<ulong, List<ulong>> IgnoredUsersPerChannel { get; init; } = [];
|
||||
}
|
||||
|
||||
public class ChannelConfig
|
||||
{
|
||||
public Dictionary<ulong, ulong> Redirects { get; init; } = [];
|
||||
|
||||
public ulong GuildUpdate { get; set; }
|
||||
|
|
|
|||
|
|
@ -131,24 +131,31 @@ public class GuildRepository(ILogger logger, DatabaseConnection conn)
|
|||
new { GuildId = guildId.Value, RoleId = roleId.Value }
|
||||
);
|
||||
|
||||
public async Task UpdateChannelConfigAsync(Snowflake id, Guild.ChannelConfig config) =>
|
||||
public async Task UpdateChannelConfigAsync(Snowflake id, Guild config) =>
|
||||
await conn.ExecuteAsync(
|
||||
"update guilds set channels = @Channels::jsonb where id = @Id",
|
||||
new { Id = id.Value, Channels = config }
|
||||
"update guilds set channels = @Channels::jsonb, messages = @Messages::jsonb where id = @Id",
|
||||
new
|
||||
{
|
||||
Id = id.Value,
|
||||
config.Channels,
|
||||
config.Messages,
|
||||
}
|
||||
);
|
||||
|
||||
public async Task ImportConfigAsync(
|
||||
ulong id,
|
||||
Guild.ChannelConfig channels,
|
||||
Guild.MessageConfig messages,
|
||||
string[] bannedSystems,
|
||||
ulong[] keyRoles
|
||||
) =>
|
||||
await conn.ExecuteAsync(
|
||||
"update guilds set channels = @channels::jsonb, banned_systems = @bannedSystems, key_roles = @keyRoles where id = @id",
|
||||
"update guilds set channels = @channels::jsonb, messages = @messages::jsonb, banned_systems = @bannedSystems, key_roles = @keyRoles where id = @id",
|
||||
new
|
||||
{
|
||||
id,
|
||||
channels,
|
||||
messages,
|
||||
bannedSystems,
|
||||
keyRoles,
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue