public async Task StartAsync() { try { var guild = _client.GetGuild(373829959187169280); if (guild != null) { var invites = await guild.GetInvitesAsync(); var usedInviteLinks = invites.Where(x => x.Uses > 0).ToList(); var inviters = usedInviteLinks.Select(x => x.Inviter).Distinct().ToList(); foreach (IUser inviter in inviters) { var inviteTotal = usedInviteLinks.Where(x => x.Inviter.Id == inviter.Id).Sum(x => x.Uses); _currentState.TryAdd(inviter.Id, inviteTotal ?? 0); } _client.UserJoined += UserJoinedAsync; _timer.Start(); } } catch (Exception ex) { _client.UserJoined -= UserJoinedAsync; await RLBot.Log(new LogMessage(LogSeverity.Critical, "UserHandler.StartAsync", ex.Message, ex)); } finally { _client.Ready -= StartAsync; } }
public override async Task <PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services) { if (CoolingDown.ContainsKey(context.User.Id)) { DateTimeOffset endTime; if (CoolingDown.TryGetValue(context.User.Id, out endTime)) { if (endTime > DateTimeOffset.Now) { string name = (context as IGuildUser)?.Nickname ?? context.User.Username; int diff = (int)Math.Ceiling((endTime - DateTimeOffset.Now).TotalSeconds); int seconds = diff % 60; diff -= seconds; int minutes = diff % 3600; diff -= minutes; int hours = diff % 86400; int days = diff - hours; string time = ""; if (days > 0) { time = $"{days / 86400}days "; } if (hours > 0) { time = time + $"{hours / 3600}hours "; } if (minutes > 0) { time = time + $"{minutes / 60}minutes "; } if (seconds > 0) { time = time + $"{seconds}seconds "; } return(PreconditionResult.FromError($"{name}, `{time}`before you can use this command again.")); } } else { //something really glitchy happened, timer interfered? //for now we'll ignore this possibility except for logging it await RLBot.Log(new LogMessage(LogSeverity.Critical, "CheckPermissions", "glitchy cooldown error occurred.")); } } CoolingDown.AddOrUpdate(context.User.Id, DateTimeOffset.Now + CooldownTime, (key, val) => DateTimeOffset.Now + CooldownTime); return(PreconditionResult.FromSuccess()); }
private void CleanupTimer_Elapsed(object sender, ElapsedEventArgs e) { lock (cleanupLock) //lock to prevent this from accumulating (if the timeout elapses before we're done) { foreach (var kvp in CoolingDown) { if (kvp.Value < DateTimeOffset.Now) { DateTimeOffset throwaway; if (!CoolingDown.TryRemove(kvp.Key, out throwaway)) { //log a threading error? RLBot.Log(new LogMessage(LogSeverity.Critical, "CleanupTimer_Elapsed", "Cleanup timer threading error.")); } } } } }