/// <summary> /// Warns a user and adds a entry in the logs /// </summary> /// <param name="context">Gives the context needed to execute the command.</param> /// <param name="mentionedUser">Gives the user that needs to be warned.</param> /// <param name="reason">Gives a reason of why the user was warned.</param> public async Task AddWarn(SocketCommandContext context, SocketGuildUser mentionedUser, string reason) { var user = context.User as SocketGuildUser; if (user == null) { //todo: error handle return; } if (user.GuildPermissions.MuteMembers) { var adminLogHandler = new AdminLogHandler(); await adminLogHandler.AddLogAsync(context.Guild.Id.ToString(), Convert.ToInt64(mentionedUser.Id), mentionedUser.Username, "Warn", reason); var builder = new EmbedBuilder(); builder.WithAuthor(user.Username, user.GetAvatarUrl()); builder.WithTitle("A user has been warned"); builder.WithColor(255, 183, 229); builder.AddField("Warned User", mentionedUser.Username, true); builder.AddField("Warned User ID", mentionedUser.Id, true); builder.AddField("Reason", reason); builder.WithCurrentTimestamp(); await context.Channel.SendMessageAsync("", false, builder.Build()); } else { await context.Channel.SendMessageAsync("You do not have the permissions to warn."); } }
/// <summary> /// Kicks a user from the server. /// </summary> /// <param name="context">Gives the needed context to execute the command.</param> /// <param name="mentionedUser">Gives the user that needs to be kicked.</param> /// <param name="reason">Gives a reason of why the user was kicked.</param> public async Task KickUser(SocketCommandContext context, SocketGuildUser mentionedUser, string reason) { var user = context.User as SocketGuildUser; if (user == null) { //todo: error handle return; } if (user.GuildPermissions.KickMembers) { if (!mentionedUser.GuildPermissions.Administrator) { await mentionedUser.KickAsync(reason); var builder = new EmbedBuilder(); builder.WithAuthor(user.Username, user.GetAvatarUrl()); builder.WithTitle("A user has been kicked!"); builder.WithColor(255, 183, 229); builder.AddField("Kicked user", mentionedUser, true); builder.AddField("\u200b", "\u200b", true); builder.AddField("Kicked user ID", mentionedUser.Id, true); if (reason != null) { builder.AddField("Reason", reason); } builder.WithCurrentTimestamp(); var adminLogHandler = new AdminLogHandler(); //If no reason was given set the reason to 'no reason given' reason = reason ?? "No reason given"; //Adds an entry in the logs await adminLogHandler.AddLogAsync(context.Guild.Id.ToString(), Convert.ToInt64(mentionedUser.Id), mentionedUser.Username, "Kick", reason); await context.Channel.SendMessageAsync("", false, builder.Build()); } else { await context.Channel.SendMessageAsync("You are not allowed to kick an Administrator."); } } else { await context.Channel.SendMessageAsync("You have insufficient permissions to perform this command."); } }
/// <summary> /// Mutes a user for a set time period. /// </summary> /// <param name="context">Gives the needed context to execute the command.</param> /// <param name="mentionedUser">Gives the user that needs to be muted.</param> /// <param name="mutePeriod">Gives the time period of the mute.</param> /// <param name="reason">Gives the reason of why the user was muted.</param> public async Task AddMute(SocketCommandContext context, SocketGuildUser mentionedUser, string mutePeriod, string reason) { var serverCache = ServerDataManager.GetServerDataByServerId(context.Guild.Id); var user = context.User as SocketGuildUser; if (user == null) { //todo: error handle return; } if (user.GuildPermissions.MuteMembers) { try { var serverSettings = serverCache.GetServerSettingsModel()["mute_role"]; var logHandler = new AdminLogHandler(); //Adds an entry in the logs await logHandler.AddLogAsync( context.Guild.Id.ToString(), Convert.ToInt64(mentionedUser.Id), mentionedUser.Username, "Mute", reason); //Gives the role to the user to mute them. await mentionedUser.AddRoleAsync( context.Guild.GetRole(Convert.ToUInt64(serverSettings.Value))); var timeInt = mutePeriod.Replace(mutePeriod.Last(), ' '); if (!int.TryParse(timeInt, out var result)) { await context.Channel.SendMessageAsync("Given time was false! Usage: !Mute user 12D reason"); return; } var muteExpireTime = DateTime.Now; switch (mutePeriod.Last()) { case 's': muteExpireTime = muteExpireTime.AddSeconds(result); break; case 'm': muteExpireTime = muteExpireTime.AddMinutes(result); break; case 'h': muteExpireTime = muteExpireTime.AddHours(result); break; case 'd': muteExpireTime = muteExpireTime.AddDays(result); break; default: await context.Channel.SendMessageAsync("Only seconds, minutes, hours or days are allowed!"); return; } var muteData = new MuteDataModel(true, mutePeriod, muteExpireTime.ToBinary()); var userData = new UserDataModel(mentionedUser.Id, mentionedUser.Username); userData.SetMuteData(muteData); //Gives the user a muted state in the database serverCache.SetUserData(Convert.ToUInt64(userData.UserId), userData); var builder = new EmbedBuilder(); builder.WithAuthor(user.Username, user.GetAvatarUrl()); builder.WithColor(255, 183, 229); builder.WithCurrentTimestamp(); builder.WithTitle("A user has been muted!"); builder.AddField("Muted User", mentionedUser.Username, true); builder.AddField("Muted User ID", mentionedUser.Id, true); builder.AddField("Reason", reason); await context.Channel.SendMessageAsync("", false, builder.Build()); } catch //Cache of ServerSettings did not contain a role to mute members { var guildRoles = context.Guild.Roles.ToList(); //Checks if the server already contains a mute role var existingMuteRole = guildRoles.Find(role => role.Name == "muted"); if (existingMuteRole != null && user.GuildPermissions.Administrator) { //Adds the role to the server settings and reruns the command await SetMuteRole(context, existingMuteRole); await AddMute(context, mentionedUser, mutePeriod, reason); } else //Server did not contain a mute role { await context.Channel.SendMessageAsync( "Could not mute the user. Please ask a administrator to set the mute role using !setmuterole @role"); } } } else { await context.Channel.SendMessageAsync("You are not allowed to mute a user!"); } }