diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c3894a4c..af14c0066 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ Experimental changelog. Mostly based on [keepachangelog](https://keepachangelog. ## Unreleased +## [4.3.4] - 07.08.2022 + +### Fixed + +- Fixed users getting XP out of nowhere while voice xp is enabled + ## [4.3.3] - 06.08.2022 ### Added diff --git a/src/NadekoBot/Modules/Xp/XpService.cs b/src/NadekoBot/Modules/Xp/XpService.cs index 44fae7319..1177ba64e 100644 --- a/src/NadekoBot/Modules/Xp/XpService.cs +++ b/src/NadekoBot/Modules/Xp/XpService.cs @@ -593,12 +593,12 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand { if (ShouldTrackVoiceChannel(channel)) { - foreach (var user in channel.Users) + foreach (var user in channel.ConnectedUsers) await ScanUserForVoiceXp(user, channel); } else { - foreach (var user in channel.Users) + foreach (var user in channel.ConnectedUsers) await UserLeftVoiceChannel(user, channel); } } @@ -617,7 +617,7 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand } private bool ShouldTrackVoiceChannel(SocketVoiceChannel channel) - => channel.Users.Where(UserParticipatingInVoiceChannel).Take(2).Count() >= 2; + => channel.ConnectedUsers.Where(UserParticipatingInVoiceChannel).Take(2).Count() >= 2; private bool UserParticipatingInVoiceChannel(SocketGuildUser user) => !user.IsDeafened && !user.IsMuted && !user.IsSelfDeafened && !user.IsSelfMuted; @@ -634,6 +634,18 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand TimeSpan.FromMinutes(_xpConfig.Data.VoiceMaxMinutes), overwrite: false); } + + // private void UserJoinedVoiceChannel(SocketGuildUser user) + // { + // var key = $"{_creds.RedisKey()}_user_xp_vc_join_{user.Id}"; + // var value = DateTimeOffset.UtcNow.ToUnixTimeSeconds(); + // + // _cache.Redis.GetDatabase() + // .StringSet(key, + // value, + // TimeSpan.FromMinutes(_xpConfig.Data.VoiceMaxMinutes), + // when: When.NotExists); + // } private async Task UserLeftVoiceChannel(SocketGuildUser user, SocketVoiceChannel channel) { @@ -654,6 +666,7 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand if (actualXp > 0) { + Log.Information("Adding {Amount} voice xp to {User}", actualXp, user.ToString()); await _xpGainQueue.Writer.WriteAsync(new() { Guild = channel.Guild, @@ -663,6 +676,38 @@ public class XpService : INService, IReadyExecutor, IExecNoCommand }); } } + + /* + * private void UserLeftVoiceChannel(SocketGuildUser user, SocketVoiceChannel channel) + { + var key = $"{_creds.RedisKey()}_user_xp_vc_join_{user.Id}"; + var value = _cache.Redis.GetDatabase().StringGet(key); + _cache.Redis.GetDatabase().KeyDelete(key); + + // Allow for if this function gets called multiple times when a user leaves a channel. + if (value.IsNull) + return; + + if (!value.TryParse(out long startUnixTime)) + return; + + var dateStart = DateTimeOffset.FromUnixTimeSeconds(startUnixTime); + var dateEnd = DateTimeOffset.UtcNow; + var minutes = (dateEnd - dateStart).TotalMinutes; + var xp = _xpConfig.Data.VoiceXpPerMinute * minutes; + var actualXp = (int)Math.Floor(xp); + + if (actualXp > 0) + { + _addMessageXp.Enqueue(new() + { + Guild = channel.Guild, + User = user, + XpAmount = actualXp + }); + } + } + */ private bool ShouldTrackXp(SocketGuildUser user, ulong channelId) { diff --git a/src/NadekoBot/Services/Impl/StatsService.cs b/src/NadekoBot/Services/Impl/StatsService.cs index 8d9c965d5..8bc333e19 100644 --- a/src/NadekoBot/Services/Impl/StatsService.cs +++ b/src/NadekoBot/Services/Impl/StatsService.cs @@ -7,7 +7,7 @@ namespace NadekoBot.Services; public sealed class StatsService : IStatsService, IReadyExecutor, INService { - public const string BOT_VERSION = "4.3.3"; + public const string BOT_VERSION = "4.3.4"; public string Author => "Kwoth#2452";