Browse Source

started working on rule cache

pull/2578/head
Misha133 2 years ago
parent
commit
ef1c0362df
5 changed files with 57 additions and 28 deletions
  1. +1
    -1
      src/Discord.Net.Core/Entities/Guilds/IGuild.cs
  2. +5
    -8
      src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs
  3. +7
    -7
      src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs
  4. +7
    -2
      src/Discord.Net.WebSocket/DiscordSocketClient.cs
  5. +37
    -10
      src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs

+ 1
- 1
src/Discord.Net.Core/Entities/Guilds/IGuild.cs View File

@@ -1282,7 +1282,7 @@ namespace Discord
/// <returns> /// <returns>
/// A task that represents the asynchronous creation operation. The task result contains a <see cref="IAutoModRule"/>. /// A task that represents the asynchronous creation operation. The task result contains a <see cref="IAutoModRule"/>.
/// </returns> /// </returns>
Task<IAutoModRule> GetAutoModRuleAsync(RequestOptions options = null);
Task<IAutoModRule> GetAutoModRuleAsync(ulong ruleId, RequestOptions options = null);


/// <summary> /// <summary>
/// Creates a new auto moderation rule. /// Creates a new auto moderation rule.


+ 5
- 8
src/Discord.Net.Rest/Entities/Guilds/GuildHelper.cs View File

@@ -10,6 +10,7 @@ using Model = Discord.API.Guild;
using RoleModel = Discord.API.Role; using RoleModel = Discord.API.Role;
using ImageModel = Discord.API.Image; using ImageModel = Discord.API.Image;
using System.IO; using System.IO;
using System.Runtime.CompilerServices;


namespace Discord.Rest namespace Discord.Rest
{ {
@@ -1068,15 +1069,11 @@ namespace Discord.Rest
throw new NotImplementedException(); throw new NotImplementedException();
} }


public static Task<AutoModerationRule> GetAutoModRuleAsync(IGuild guild, BaseDiscordClient client, RequestOptions options)
{
throw new NotImplementedException();
}
public static async Task<AutoModerationRule> GetAutoModRuleAsync(ulong ruleId, IGuild guild, BaseDiscordClient client, RequestOptions options)
=> await client.ApiClient.GetGuildAutoModRuleAsync(guild.Id, ruleId, options);


public static Task<AutoModerationRule[]> GetAutoModRulesAsync(IGuild guild, BaseDiscordClient client, RequestOptions options)
{
throw new NotImplementedException();
}
public static async Task<AutoModerationRule[]> GetAutoModRulesAsync(IGuild guild, BaseDiscordClient client, RequestOptions options)
=> await client.ApiClient.GetGuildAutoModRulesAsync(guild.Id, options);


public static Task<AutoModerationRule> ModifyRuleAsync(BaseDiscordClient client, IAutoModRule rule, Action<AutoModRuleProperties> func, RequestOptions options) public static Task<AutoModerationRule> ModifyRuleAsync(BaseDiscordClient client, IAutoModRule rule, Action<AutoModRuleProperties> func, RequestOptions options)
{ {


+ 7
- 7
src/Discord.Net.Rest/Entities/Guilds/RestGuild.cs View File

@@ -1203,17 +1203,17 @@ namespace Discord.Rest




/// <inheritdoc cref="IGuild.GetAutoModRuleAsync"/> /// <inheritdoc cref="IGuild.GetAutoModRuleAsync"/>
public async Task<RestAutoModRule> GetAutoModRuleAsync(RequestOptions options = null)
public async Task<RestAutoModRule> GetAutoModRuleAsync(ulong ruleId, RequestOptions options = null)
{ {
var rule = await GuildHelper.GetAutoModRuleAsync(this, Discord, options);
throw new NotImplementedException();
var rule = await GuildHelper.GetAutoModRuleAsync(ruleId, this, Discord, options);
return RestAutoModRule.Create(Discord, rule);
} }


/// <inheritdoc cref="IGuild.GetAutoModRulesAsync"/> /// <inheritdoc cref="IGuild.GetAutoModRulesAsync"/>
public async Task<RestAutoModRule[]> GetAutoModRulesAsync(RequestOptions options = null) public async Task<RestAutoModRule[]> GetAutoModRulesAsync(RequestOptions options = null)
{ {
var rule = await GuildHelper.GetAutoModRulesAsync(this, Discord, options);
throw new NotImplementedException();
var rules = await GuildHelper.GetAutoModRulesAsync(this, Discord, options);
return rules.Select(x => RestAutoModRule.Create(Discord, x)).ToArray();
} }


/// <inheritdoc cref="IGuild.CreateAutoModRuleAsync"/> /// <inheritdoc cref="IGuild.CreateAutoModRuleAsync"/>
@@ -1572,8 +1572,8 @@ namespace Discord.Rest




/// <inheritdoc/> /// <inheritdoc/>
async Task<IAutoModRule> IGuild.GetAutoModRuleAsync(RequestOptions options)
=> await GetAutoModRuleAsync(options).ConfigureAwait(false);
async Task<IAutoModRule> IGuild.GetAutoModRuleAsync(ulong ruleId, RequestOptions options)
=> await GetAutoModRuleAsync(ruleId, options).ConfigureAwait(false);


/// <inheritdoc/> /// <inheritdoc/>
async Task<IAutoModRule[]> IGuild.GetAutoModRulesAsync(RequestOptions options) async Task<IAutoModRule[]> IGuild.GetAutoModRulesAsync(RequestOptions options)


+ 7
- 2
src/Discord.Net.WebSocket/DiscordSocketClient.cs View File

@@ -2957,10 +2957,15 @@ namespace Discord.WebSocket
return null; return null;
}); });


var rule = new Cacheable<IAutoModRule, ulong>(null, data.RuleId, false, () => null);
var cachedRule = guild.GetAutoModRule(data.RuleId);

var cacheableRule = new Cacheable<IAutoModRule, ulong>(cachedRule,
data.RuleId,
cachedRule is not null,
async () => await guild.GetAutoModRuleAsync(data.RuleId));


var eventData = new AutoModActionExecutedData( var eventData = new AutoModActionExecutedData(
rule,
cacheableRule,
data.TriggerType, data.TriggerType,
cacheableUser, cacheableUser,
cacheableChannel, cacheableChannel,


+ 37
- 10
src/Discord.Net.WebSocket/Entities/Guilds/SocketGuild.cs View File

@@ -22,6 +22,7 @@ using UserModel = Discord.API.User;
using VoiceStateModel = Discord.API.VoiceState; using VoiceStateModel = Discord.API.VoiceState;
using StickerModel = Discord.API.Sticker; using StickerModel = Discord.API.Sticker;
using EventModel = Discord.API.GuildScheduledEvent; using EventModel = Discord.API.GuildScheduledEvent;
using AutoModRuleModel = Discord.API.AutoModerationRule;
using System.IO; using System.IO;


namespace Discord.WebSocket namespace Discord.WebSocket
@@ -43,7 +44,7 @@ namespace Discord.WebSocket
private ConcurrentDictionary<ulong, SocketVoiceState> _voiceStates; private ConcurrentDictionary<ulong, SocketVoiceState> _voiceStates;
private ConcurrentDictionary<ulong, SocketCustomSticker> _stickers; private ConcurrentDictionary<ulong, SocketCustomSticker> _stickers;
private ConcurrentDictionary<ulong, SocketGuildEvent> _events; private ConcurrentDictionary<ulong, SocketGuildEvent> _events;
//private ConcurrentDictionary<ulong, SocketAutoModRule> _automodRules;
private ConcurrentDictionary<ulong, SocketAutoModRule> _automodRules;
private ImmutableArray<GuildEmote> _emotes; private ImmutableArray<GuildEmote> _emotes;


private AudioClient _audioClient; private AudioClient _audioClient;
@@ -392,6 +393,7 @@ namespace Discord.WebSocket
{ {
_audioLock = new SemaphoreSlim(1, 1); _audioLock = new SemaphoreSlim(1, 1);
_emotes = ImmutableArray.Create<GuildEmote>(); _emotes = ImmutableArray.Create<GuildEmote>();
_automodRules = new ConcurrentDictionary<ulong, SocketAutoModRule>();
} }
internal static SocketGuild Create(DiscordSocketClient discord, ClientState state, ExtendedModel model) internal static SocketGuild Create(DiscordSocketClient discord, ClientState state, ExtendedModel model)
{ {
@@ -1812,22 +1814,47 @@ namespace Discord.WebSocket


#region AutoMod #region AutoMod


internal SocketAutoModRule AddOrUpdateAutoModRule(AutoModRuleModel model)
{
if (_automodRules.TryGetValue(model.Id, out var rule))
{
rule.Update(model);
return rule;
}

var socketRule = SocketAutoModRule.Create(Discord, this, model);
_automodRules.TryAdd(model.Id, socketRule);
return socketRule;
}

internal SocketAutoModRule GetAutoModRule(ulong id)
{
return _automodRules.TryGetValue(id, out var rule) ? rule : null;
}

internal SocketAutoModRule RemoveAutoModRule(ulong id)
{
return _automodRules.TryRemove(id, out var rule) ? rule : null;
}

/// <inheritdoc cref="IGuild.GetAutoModRuleAsync"/> /// <inheritdoc cref="IGuild.GetAutoModRuleAsync"/>
public async Task<RestAutoModRule> GetAutoModRuleAsync(RequestOptions options = null)
public async Task<SocketAutoModRule> GetAutoModRuleAsync(ulong ruleId, RequestOptions options = null)
{ {
var rule = await GuildHelper.GetAutoModRuleAsync(this, Discord, options);
throw new NotImplementedException();
var rule = await GuildHelper.GetAutoModRuleAsync(ruleId, this, Discord, options);

return AddOrUpdateAutoModRule(rule);
} }


/// <inheritdoc cref="IGuild.GetAutoModRulesAsync"/> /// <inheritdoc cref="IGuild.GetAutoModRulesAsync"/>
public async Task<RestAutoModRule[]> GetAutoModRulesAsync(RequestOptions options = null)
public async Task<SocketAutoModRule[]> GetAutoModRulesAsync(RequestOptions options = null)
{ {
var rule = await GuildHelper.GetAutoModRulesAsync(this, Discord, options);
throw new NotImplementedException();
var rules = await GuildHelper.GetAutoModRulesAsync(this, Discord, options);

return rules.Select(AddOrUpdateAutoModRule).ToArray();
} }


/// <inheritdoc cref="IGuild.CreateAutoModRuleAsync"/> /// <inheritdoc cref="IGuild.CreateAutoModRuleAsync"/>
public async Task<RestAutoModRule> CreateAutoModRuleAsync(RequestOptions options = null)
public async Task<SocketAutoModRule> CreateAutoModRuleAsync(RequestOptions options = null)
{ {
var rule = await GuildHelper.CreateAutoModRuleAsync(this, Discord, options); var rule = await GuildHelper.CreateAutoModRuleAsync(this, Discord, options);
throw new NotImplementedException(); throw new NotImplementedException();
@@ -2081,8 +2108,8 @@ namespace Discord.WebSocket
} }
/// <inheritdoc/> /// <inheritdoc/>
async Task<IAutoModRule> IGuild.GetAutoModRuleAsync(RequestOptions options)
=> await GetAutoModRuleAsync(options).ConfigureAwait(false);
async Task<IAutoModRule> IGuild.GetAutoModRuleAsync(ulong ruleId, RequestOptions options)
=> await GetAutoModRuleAsync(ruleId, options).ConfigureAwait(false);


/// <inheritdoc/> /// <inheritdoc/>
async Task<IAutoModRule[]> IGuild.GetAutoModRulesAsync(RequestOptions options) async Task<IAutoModRule[]> IGuild.GetAutoModRulesAsync(RequestOptions options)


Loading…
Cancel
Save