private async Task AddReminderAsync(CommandContext ctx, TimeSpan timespan, DiscordChannel channel, string message, bool repeat = false) { if (string.IsNullOrWhiteSpace(message)) { throw new InvalidCommandUsageException("Missing time or repeat string."); } if (message.Length > 250) { throw new InvalidCommandUsageException("Message must be shorter than 250 characters."); } if (!(channel is null) && !channel.PermissionsFor(ctx.Member).HasPermission(Permissions.AccessChannels | Permissions.SendMessages)) { throw new CommandFailedException("You cannot send reminder to that channel!"); } if (channel is null && await ctx.Client.CreateDmChannelAsync(ctx.User.Id) is null) { throw new CommandFailedException("I cannot send DMs to you, please enable it so that I can remind you."); } bool privileged; using (DatabaseContext db = this.Database.CreateContext()) privileged = db.PrivilegedUsers.Any(u => u.UserId == ctx.User.Id); if (!ctx.Client.CurrentApplication.Owners.Any(o => o.Id == ctx.User.Id) && !privileged) { if (timespan < TimeSpan.Zero || timespan.TotalMinutes < 1 || timespan.TotalDays > 31) { throw new InvalidCommandUsageException("Time span cannot be less than 1 minute or greater than 31 days."); } if (this.Shared.RemindExecuters.TryGetValue(ctx.User.Id, out ConcurrentDictionary <int, SavedTaskExecutor> texecs) && texecs.Count >= 20) { throw new CommandFailedException("You cannot have more than 20 reminders scheduled!"); } } DateTimeOffset when = DateTimeOffset.Now + timespan; var task = new SendMessageTaskInfo(channel?.Id ?? 0, ctx.User.Id, message, when, repeat, timespan); await SavedTaskExecutor.ScheduleAsync(this.Shared, this.Database, ctx.Client, task); if (repeat) { await this.InformAsync(ctx, StaticDiscordEmoji.AlarmClock, $"I will repeatedly remind {channel?.Mention ?? "you"} every {Formatter.Bold(timespan.Humanize(4, minUnit: TimeUnit.Second))} to:\n\n{message}", important : false); } else { await this.InformAsync(ctx, StaticDiscordEmoji.AlarmClock, $"I will remind {channel?.Mention ?? "you"} in {Formatter.Bold(timespan.Humanize(4, minUnit: TimeUnit.Second))} ({when.ToUtcTimestamp()}) to:\n\n{message}", important : false); } }
public async Task TempBanAsync(CommandContext ctx, [Description("User (doesn't have to be a member).")] DiscordUser user, [Description("Time span.")] TimeSpan timespan, [RemainingText, Description("Reason.")] string reason = null) { if (user.Id == ctx.User.Id) { throw new CommandFailedException("You can't ban yourself."); } await ctx.Guild.BanMemberAsync(user.Id, 0, ctx.BuildInvocationDetailsString(reason)); DateTime until = DateTime.UtcNow + timespan; await this.InformAsync(ctx, $"{Formatter.Bold(ctx.User.Username)} BANNED {Formatter.Bold(user.ToString())} until {Formatter.Bold(until.ToLongTimeString())} UTC!"); var task = new UnbanTaskInfo(ctx.Guild.Id, user.Id, until); await SavedTaskExecutor.ScheduleAsync(this.Shared, this.Database, (DiscordClientImpl)ctx.Client, task); }
public async Task TempBanAsync(CommandContext ctx, [Description("Time span.")] TimeSpan timespan, [Description("Member.")] DiscordMember member, [RemainingText, Description("Reason.")] string reason = null) { if (member.Id == ctx.User.Id) { throw new CommandFailedException("You can't ban yourself."); } await member.BanAsync(delete_message_days : 0, reason : ctx.BuildInvocationDetailsString($"(tempban for {timespan.ToString()}) " + reason)); DateTimeOffset until = DateTimeOffset.Now + timespan; await this.InformAsync(ctx, $"{Formatter.Bold(ctx.User.Username)} BANNED {Formatter.Bold(member.Username)} until {Formatter.Bold(until.ToString())} UTC!"); var task = new UnbanTaskInfo(ctx.Guild.Id, member.Id, until); await SavedTaskExecutor.ScheduleAsync(this.Shared, this.Database, (DiscordClientImpl)ctx.Client, task); }
private async Task AddReminderAsync(CommandContext ctx, TimeSpan timespan, DiscordChannel channel, string message, bool repeat = false) { if (string.IsNullOrWhiteSpace(message)) { throw new InvalidCommandUsageException("Missing time or repeat string."); } if (message.Length > 250) { throw new InvalidCommandUsageException("Message must be shorter than 250 characters."); } if (timespan.TotalMinutes < 1 || timespan.TotalDays > 31) { throw new InvalidCommandUsageException("Time span cannot be less than 1 minute or greater than 31 days."); } if (this.Shared.RemindExecuters.ContainsKey(ctx.User.Id) && this.Shared.RemindExecuters[ctx.User.Id].Count >= 20) { throw new CommandFailedException("You cannot have more than 20 reminders scheduled!"); } DateTimeOffset when = DateTimeOffset.Now + timespan; var task = new SendMessageTaskInfo(channel?.Id ?? 0, ctx.User.Id, message, when, repeat, timespan); await SavedTaskExecutor.ScheduleAsync(this.Shared, this.Database, (DiscordClientImpl)ctx.Client, task); if (repeat) { await this.InformAsync(ctx, StaticDiscordEmoji.AlarmClock, $"I will repeatedly remind {channel?.Mention ?? "you"} every {Formatter.Bold(timespan.Humanize(5))} to:\n\n{message}", important : false); } else { await this.InformAsync(ctx, StaticDiscordEmoji.AlarmClock, $"I will remind {channel?.Mention ?? "you"} in {Formatter.Bold(timespan.Humanize(5))} ({when.ToUtcTimestamp()}) to:\n\n{message}", important : false); } }
public async Task TempBanAsync(CommandContext ctx, [Description("User (doesn't have to be a member).")] DiscordUser user, [Description("Time span.")] TimeSpan timespan, [RemainingText, Description("Reason.")] string reason = null) { if (user.Id == ctx.User.Id) { throw new CommandFailedException("You can't ban yourself."); } if (timespan.TotalMinutes < 1 || timespan.TotalDays > 31) { throw new InvalidCommandUsageException("Given time period cannot be lower than 1 minute or greater than 1 month"); } await ctx.Guild.BanMemberAsync(user.Id, 0, ctx.BuildInvocationDetailsString(reason)); DateTime until = DateTime.UtcNow + timespan; await this.InformAsync(ctx, $"{ctx.Member.Mention} {Formatter.Bold("BANNED")} {user.ToString()} for {Formatter.Bold(timespan.Humanize(4, minUnit: TimeUnit.Second))}!"); var task = new UnbanTaskInfo(ctx.Guild.Id, user.Id, until); await SavedTaskExecutor.ScheduleAsync(this.Shared, this.Database, ctx.Client, task); }
public async Task TempBanAsync(CommandContext ctx, [Description("Time span.")] TimeSpan timespan, [Description("Member.")] DiscordMember member, [RemainingText, Description("Reason.")] string reason = null) { if (member.Id == ctx.User.Id) { throw new CommandFailedException("You can't ban yourself."); } if (timespan.TotalMinutes < 1 || timespan.TotalDays > 31) { throw new InvalidCommandUsageException("Given time period cannot be lower than 1 minute or greater than 1 month"); } await member.BanAsync(delete_message_days : 0, reason : ctx.BuildInvocationDetailsString($"(tempban for {timespan.ToString()}) " + reason)); var until = DateTimeOffset.Now + timespan; await this.InformAsync(ctx, $"{ctx.Member.Mention} {Formatter.Bold("BANNED")} {member.DisplayName} for {Formatter.Bold(timespan.Humanize(4, minUnit: TimeUnit.Second))}!"); var task = new UnbanTaskInfo(ctx.Guild.Id, member.Id, until); await SavedTaskExecutor.ScheduleAsync(this.Shared, this.Database, ctx.Client, task); }
public async Task PunishMemberAsync(DiscordGuild guild, DiscordMember member, PunishmentActionType type, TimeSpan?cooldown = null, string reason = null) { try { DiscordRole muteRole; SavedTaskInfo task; switch (type) { case PunishmentActionType.Kick: await member.RemoveAsync(reason ?? this.reason); break; case PunishmentActionType.PermanentMute: muteRole = await this.GetOrCreateMuteRoleAsync(guild); if (member.Roles.Contains(muteRole)) { return; } await member.GrantRoleAsync(muteRole, reason ?? this.reason); break; case PunishmentActionType.PermanentBan: await member.BanAsync(1, reason : reason ?? this.reason); break; case PunishmentActionType.TemporaryBan: await member.BanAsync(0, reason : reason ?? this.reason); task = new UnbanTaskInfo(guild.Id, member.Id, cooldown is null ? null : DateTimeOffset.Now + cooldown); await SavedTaskExecutor.ScheduleAsync(this.shard.SharedData, this.shard.Database, this.shard.Client, task); break; case PunishmentActionType.TemporaryMute: muteRole = await this.GetOrCreateMuteRoleAsync(guild); if (member.Roles.Contains(muteRole)) { return; } await member.GrantRoleAsync(muteRole, reason ?? this.reason); task = new UnmuteTaskInfo(guild.Id, member.Id, muteRole.Id, cooldown is null ? null : DateTimeOffset.Now + cooldown); await SavedTaskExecutor.ScheduleAsync(this.shard.SharedData, this.shard.Database, this.shard.Client, task); break; } } catch { var logchn = this.shard.SharedData.GetLogChannelForGuild(this.shard.Client, guild); if (!(logchn is null)) { var emb = new DiscordEmbedBuilder { Title = "User punish attemp failed! Check my permissions...", Color = DiscordColor.Red }; emb.AddField("User", member?.ToString() ?? "unknown", inline: true); emb.AddField("Reason", reason ?? this.reason, inline: false); await logchn.SendMessageAsync(embed : emb.Build()); } } }