Made code a bit easier to read, made attempt at multi noti fix
This commit is contained in:
parent
acee597910
commit
fdc5c1be66
|
|
@ -30,31 +30,27 @@ public class VoiceStateManager
|
||||||
{
|
{
|
||||||
// Joining channel
|
// Joining channel
|
||||||
_logger.LogInformation($"New yapper detected: {voice_state.User.Username}");
|
_logger.LogInformation($"New yapper detected: {voice_state.User.Username}");
|
||||||
if (!_guildVoiceStates.TryGetValue(voice_state.GuildId, out var yappers) || yappers.Count < 0)
|
if (!_guildVoiceStates.TryGetValue(voice_state.GuildId, out var yappers) || yappers.Count == 0)
|
||||||
{
|
{
|
||||||
|
yappers ??= new HashSet<ulong>();
|
||||||
var channels = await _restClient.GetGuildChannelsAsync(voice_state.GuildId);
|
var channels = await _restClient.GetGuildChannelsAsync(voice_state.GuildId);
|
||||||
var alertChannelId = channels.FirstOrDefault(channel => channel.Name == "bot-tinkering")?.Id;
|
var alertChannelId = channels.FirstOrDefault(channel => channel.Name == "bot-tinkering")?.Id;
|
||||||
var guildRoles = await _restClient.GetGuildRolesAsync(voice_state.GuildId,
|
var guildRoles = await _restClient.GetGuildRolesAsync(voice_state.GuildId,
|
||||||
new RestRequestProperties() { AuditLogReason = "Role lookup" });
|
new RestRequestProperties() { AuditLogReason = "Role lookup" });
|
||||||
var yapperRoleId = guildRoles.FirstOrDefault(role => role.Name.ToLower() == "yapper")?.Id;
|
var yapperRoleId = guildRoles.FirstOrDefault(role => role.Name.ToLower() == "yapper")?.Id;
|
||||||
if (alertChannelId is not null && yapperRoleId is not null && (!_yapperNotificationTimeoutCache.TryGetValue(voice_state.GuildId, out DateTimeOffset timeout) || DateTimeOffset.UtcNow.CompareTo(timeout) > 0))
|
if (alertChannelId is null || yapperRoleId is null)
|
||||||
{
|
{
|
||||||
// Notify that new yapper has arrived
|
_logger.LogError($"Found incorrect state, alert channel or yapper role does not exist.");
|
||||||
await _restClient.SendMessageAsync(
|
return;
|
||||||
alertChannelId.GetValueOrDefault(),
|
|
||||||
new MessageProperties()
|
|
||||||
{
|
|
||||||
AllowedMentions = new AllowedMentionsProperties() {AllowedRoles = [yapperRoleId.GetValueOrDefault()]},
|
|
||||||
Content = $"<@&{yapperRoleId.GetValueOrDefault()}> " + PhraseProvider.GetYapperPhrase()
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
yappers = new HashSet<ulong>() { voice_state.UserId };
|
if (!CheckIfTimeoutExpired(voice_state.GuildId)) return;
|
||||||
}
|
|
||||||
else
|
// Notify that new yapper has arrived
|
||||||
{
|
await SendYapperNotification(yapperRoleId.GetValueOrDefault(), alertChannelId.GetValueOrDefault());
|
||||||
yappers = yappers.Append(voice_state.UserId).ToHashSet();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
yappers.Add(voice_state.UserId);
|
||||||
_guildVoiceStates[voice_state.GuildId] = yappers;
|
_guildVoiceStates[voice_state.GuildId] = yappers;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
@ -65,11 +61,41 @@ public class VoiceStateManager
|
||||||
{
|
{
|
||||||
yappers.Remove(voice_state.UserId);
|
yappers.Remove(voice_state.UserId);
|
||||||
_guildVoiceStates[voice_state.GuildId] = yappers;
|
_guildVoiceStates[voice_state.GuildId] = yappers;
|
||||||
var expiration = DateTimeOffset.UtcNow.AddHours(2);
|
if (yappers.Count == 0) SetGuildTimeout(voice_state.GuildId);
|
||||||
_yapperNotificationTimeoutCache.Set(voice_state.GuildId, expiration, expiration);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void SetGuildTimeout(ulong guild_id)
|
||||||
|
{
|
||||||
|
#if DEBUG
|
||||||
|
var expiration = DateTimeOffset.UtcNow.AddMinutes(1);
|
||||||
|
#else
|
||||||
|
var expiration = DateTimeOffset.UtcNow.AddHours(2);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
_yapperNotificationTimeoutCache.Set(guild_id, expiration, expiration);
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool CheckIfTimeoutExpired(ulong guild_id)
|
||||||
|
{
|
||||||
|
var timeOutCached =
|
||||||
|
_yapperNotificationTimeoutCache.TryGetValue(guild_id, out DateTimeOffset timeout);
|
||||||
|
if (timeOutCached == false) return true;
|
||||||
|
if (timeout < DateTimeOffset.UtcNow) return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task SendYapperNotification(ulong yapperRoleId, ulong alertChannelId)
|
||||||
|
{
|
||||||
|
await _restClient.SendMessageAsync(
|
||||||
|
alertChannelId,
|
||||||
|
new MessageProperties()
|
||||||
|
{
|
||||||
|
AllowedMentions = new AllowedMentionsProperties() {AllowedRoles = [yapperRoleId]},
|
||||||
|
Content = $"<@&{yapperRoleId}> " + PhraseProvider.GetYapperPhrase()
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue