public static async Task DoBanMember(DiscordUserData data, DiscordGuild server, DiscordUser victim, DiscordUser cataylist, string reason, bool is_automated, DateTime expire_time) { //Apply the ban to the account DiscordUserDataStatusBanned status = new DiscordUserDataStatusBanned { applied_since = DateTime.UtcNow.Ticks, expiry = expire_time.Ticks, is_applied = true, is_automated = is_automated, reason = reason }; if (!is_automated) { status.catalyst = cataylist.Id; } data.temp_banned = status; //Notify the user DiscordEmbedBuilder builder = new DiscordEmbedBuilder(); builder.Title = "You have been temporarily banned from " + server.Name + "."; if (is_automated) { builder.Description = "You were removed from this server. You have had too many strikes applied to your account."; } else { builder.Description = "You were removed from this server. You were removed by a member."; } if (!is_automated) { builder.AddField("Banned By", $"{cataylist.Username}#{cataylist.Discriminator}"); builder.AddField("Reason", reason); } builder.AddField("Expires", BotTools.DateTimeToString(expire_time)); builder.AddField("Expires In", BotTools.DateTimeOffsetToString(expire_time - DateTime.UtcNow)); builder.Color = DiscordColor.Red; builder.Footer = new DiscordEmbedBuilder.EmbedFooter { Text = Program.FOOTER_TEXT }; DiscordEmbed embed = builder.Build(); //Send await BotTools.SendMemberMsg(victim.Id, embed); //Kick DiscordMember victimMember = await server.GetMemberAsync(victim.Id); await server.RemoveMemberAsync(victimMember, "Automated temporary ban system."); //Save data.Save(); }
public static async Task OnCmd(DSharpPlus.EventArgs.MessageCreateEventArgs e, string prefix, string content, DiscordMember member, DiscordPermissionLevel perms, DiscordUserData data) { if (content.Length == 0) { await ReturnHelp(e); return; } //Do it //Find the user DiscordUser victim = await BotTools.ParseOfflineName(content.Trim(' ')); //If not found, complain if (victim == null) { DiscordEmbedBuilder badBuilder = new DiscordEmbedBuilder(); badBuilder.Title = "User Not Found"; badBuilder.Description = $"\"{content.Trim(' ')}\" was not found. Try their ID."; badBuilder.Color = DiscordColor.Yellow; badBuilder.Footer = new DiscordEmbedBuilder.EmbedFooter { Text = Program.FOOTER_TEXT }; await e.Message.RespondAsync(embed : badBuilder.Build()); return; } //Get user DiscordUserData victimData = BotTools.GetUserDataById(victim.Id); //Set as not active victimData.temp_banned.is_applied = false; //Save victimData.Save(); //Write OK DiscordEmbedBuilder okBuilder = new DiscordEmbedBuilder(); okBuilder.Title = "User Unbanned"; okBuilder.Description = $"{victim.Username}#{victim.Discriminator} ({victim.Id}) was unbanned."; okBuilder.Color = DiscordColor.Green; okBuilder.Footer = new DiscordEmbedBuilder.EmbedFooter { Text = Program.FOOTER_TEXT }; await e.Message.RespondAsync(embed : okBuilder.Build()); }
private static async Task Discord_MessageCreated(DSharpPlus.EventArgs.MessageCreateEventArgs e) { //Check if this a command try { string message = e.Message.Content; if (message.StartsWith(config.command_prefix[0]) && !e.Author.IsBot && e.Guild != null) { //Get the command message = message.Substring(1); int indexOfBreak = message.IndexOf(' '); if (indexOfBreak != -1) { string prefix = message.Substring(0, indexOfBreak).TrimEnd(' '); message = message.Substring(indexOfBreak).TrimStart(' '); await CommandProcessor.OnCommand(e, prefix, message); } else { await CommandProcessor.OnCommand(e, message, ""); } } } catch (Exception ex) { Console.WriteLine(ex.Message + ex.StackTrace); } if (e.Guild != null) { //Get the member data and update if needed DiscordUserData data = BotTools.GetUserDataById(e.Author.Id); string name = $"{e.Author.Username}#{e.Author.Discriminator}"; if (data.latest_name != name) { data.latest_name = name; data.Save(); } } }
public static async Task OnCmd(DSharpPlus.EventArgs.MessageCreateEventArgs e, string prefix, string content, DiscordMember member, DiscordPermissionLevel perms, DiscordUserData data) { //Check if this is prompting for help string[] split = content.Split(','); if (content.Length == 0 || split.Length < 3) { await ReturnHelp(e); return; } //Parse the message parts string memberString = split[0]; string timeString = split[1]; string messageString = content.Substring(2 + memberString.Length + timeString.Length); //Find the user DiscordUser victim = await BotTools.ParseName(e.Guild, memberString.Trim(' ')); //If the bot failed to find the user, throw an error if (victim == null) { await ReturnError(e, "Failed to Find User", $"Failed to find the user, ``{memberString.Trim(' ')}``. You can use their ID, their full name, or their first name. You can also mention them."); return; } //Find time DateTime time; int timeParseErrorCount = 0; int timeParseCount = 0; if (timeString.Trim(' ').ToLower() != "never") { time = BotTools.ParseTime(timeString.Trim(' '), out timeParseErrorCount, out timeParseCount); } else { await ReturnError(e, "Cannot Perma Ban", "Please use the ban function built into Discord to permaban a member."); return; } //If the time parse didn't find any, or if there were any errors, stop if ((timeParseCount == 0 || timeParseErrorCount > 0)) { await ReturnError(e, "Failed to Parse Time", "There was a problem parsing the time you entered. Do not include commas in the time, make sure you use spaces to separate each part, and make sure you do not use weeks. For help, do not include any arguments and run the command again."); return; } //Limit message to 900 characters if (messageString.Length > 900) { await ReturnError(e, "Reason is Too Long", "Sorry, the reason is limited by Discord to 900 characters."); return; } //Get the user data for the person we just banned and apply. DiscordUserData victimUserData = BotTools.GetUserDataById(victim.Id); victimUserData.latest_name = $"{victim.Username}#{victim.Discriminator}"; victimUserData.temp_banned = new DiscordUserDataStatusBanned { applied_since = DateTime.UtcNow.Ticks, catalyst = e.Author.Id, expiry = time.Ticks, is_applied = true, is_automated = false, reason = messageString }; //Apply the ban to the account await ActionTools.BanTools.DoBanMember(victimUserData, e.Guild, victim, e.Author, messageString, false, time); //Save victimUserData.Save(); //Write OK await ReturnOk(e, victim, time); }
public static async Task OnCmd(DSharpPlus.EventArgs.MessageCreateEventArgs e, string prefix, string content, DiscordMember member, DiscordPermissionLevel perms, DiscordUserData data) { //Check if this is prompting for help string[] split = content.Split(','); if (content.Length == 0 || split.Length < 3) { await ReturnHelp(e); return; } //Parse the message parts string memberString = split[0]; string timeString = split[1]; string messageString = content.Substring(2 + memberString.Length + timeString.Length); //Find the user DiscordUser victim = await BotTools.ParseName(e.Guild, memberString.Trim(' ')); //If the bot failed to find the user, throw an error if (victim == null) { await ReturnError(e, "Failed to Find User", $"Failed to find the user, ``{memberString.Trim(' ')}``. You can use their ID, their full name, or their first name. You can also mention them."); return; } //Find time bool isForever = true; DateTime?time; int timeParseErrorCount = 0; int timeParseCount = 0; if (timeString.Trim(' ').ToLower() != "never") { time = BotTools.ParseTime(timeString.Trim(' '), out timeParseErrorCount, out timeParseCount); isForever = false; } else { time = null; } //If the time parse didn't find any, or if there were any errors, stop if (time != null && (timeParseCount == 0 || timeParseErrorCount > 0)) { await ReturnError(e, "Failed to Parse Time", "There was a problem parsing the time you entered. Do not include commas in the time, make sure you use spaces to separate each part, and make sure you do not use weeks. For help, do not include any arguments and run the command again."); return; } //Limit message to 900 characters if (messageString.Length > 900) { await ReturnError(e, "Reason is Too Long", "Sorry, the reason is limited by Discord to 900 characters."); return; } //Awesome. Now, create the strike DiscordUserDataStrike strike = new DiscordUserDataStrike { time = DateTime.UtcNow.Ticks, striker = e.Author.Id, message = messageString }; if (time.HasValue) { strike.expire_time = time.Value.Ticks; } //Get the user data for the person we just struck and apply. DiscordUserData victimUserData = BotTools.GetUserDataById(victim.Id); victimUserData.strikes.Add(strike); //Send a notification of this to the person await NotifyStrike(victim, member, strike, e.Guild, victimUserData); //Check if we need to issue a ban for this member var activeStrikes = victimUserData.GetActiveStrikes(); if (activeStrikes.Length >= Program.config.ban_strikes) { //Ban time! Find when the latest ban expire happens DateTime latest_expire = DateTime.MaxValue; bool does_expire = false; foreach (var a in activeStrikes) { if (a.expire_time.HasValue) { does_expire = true; if (a.GetExpiry().Value < latest_expire) { latest_expire = a.GetExpiry().Value; } } } if (does_expire) { //Temp ban await ActionTools.BanTools.DoBanMember(victimUserData, e.Guild, victim, e.Author, "You are banned for having too many strikes.", true, latest_expire); } else { //Send message await ActionTools.BanTools.NotifyPermaBan(e.Guild, victim, e.Author, "You are banned for having too many strikes.", true); //Ban from the server DiscordMember victimMember = await e.Guild.GetMemberAsync(victim.Id); await e.Guild.BanMemberAsync(victimMember, 0, "Temporary ban system."); } } //Save victimUserData.Save(); //Return OK await ReturnOk(e, victim, activeStrikes.Length); }