/// <summary> /// Finds the <see cref="SocketGuildUser"/> in the given <see cref="string"/> text and returns it in the list. /// <para></para> /// This method returns the best available option that has a word score of above .66. Otherwise, the method returns <see cref="null"/>. /// </summary> /// <param name="message"></param> /// <returns>User found from the message.</returns> /// Arcy public static SocketGuildUser GetUser(string text, float threshold = 0.5f) { // Check through all users and determine the best user found SocketGuildUser bestUser = null; float bestScore = 0; foreach (SocketGuildUser user in ServerData.Server.Users) { // Check if username or nickname contains the name, or if it contains user ID if (user.Username.ToLower().Contains(text.ToLower())) { float score = UtilStringComparison.CompareWordScore(user.Username.ToLower(), text.ToLower()); if (score > bestScore) { bestUser = user; bestScore = score; score = 0; } } if (user.Nickname != null && user.Nickname.Contains(text.ToLower())) { float score = UtilStringComparison.CompareWordScore(user.Nickname.ToLower(), text.ToLower()); if (score > bestScore) { bestUser = user; bestScore = score; score = 0; } } // User ID most likely would not be put in by mistake. Return that user if (text.Contains(user.Id.ToString())) { return(user); } } // Check if match is reasonably close if (bestScore > threshold) { return(bestUser); } else { return(null); } }
public async Task AddTitleAsync([Remainder] string tn = "") { if (string.IsNullOrWhiteSpace(tn)) { TitleEmbed te = new TitleEmbed(Context); await te.Display(); return; } int?select = null; foreach (int id in AchievementManager.NodeMap.Keys) { if (UtilStringComparison.CompareWordScore(tn, AchievementManager.GetTitle(id).Name) >= 0.66) { select = id; break; } } if (select == null) { TitleEmbed te = new TitleEmbed(Context); await te.Display(); return; } if (UserDataManager.HasTitle(BotUtils.GetGUser(Context), select.Value)) { UserDataManager.EquipTitle(BotUtils.GetGUser(Context), select.Value); await ReplyAsync(BotUtils.KamtroText($"The {AchievementManager.GetTitle(select.Value).Name} title has been set!")); } else { await ReplyAsync(BotUtils.KamtroText($"You don't have the {AchievementManager.GetTitle(select.Value).Name} title")); } }
public async Task AddRoleAsync([Remainder] string message = "") { SocketGuildUser user = BotUtils.GetGUser(Context); // Find if user entered a role if (string.IsNullOrWhiteSpace(message)) { SocketGuildUser _user = BotUtils.GetGUser(Context); RoleEmbed embed = new RoleEmbed(Context, _user); await embed.Display(Context.Channel); //ulong id = Context.Message.Author.Id; //EventQueueManager.AddEvent(embed); } else { // Check all roles - Arcy foreach (SocketRole role in ServerData.AllRoles) { // Find if the message matches the role closely enough - Arcy if (UtilStringComparison.CompareWordScore(message, role.Name) >= 0.66) { /// ALREADY HAS ROLE // Check if user already has the role - Arcy if (user.HasRole(role)) { // First person response if DMs - Arcy if (Context.Channel is IDMChannel) { await ReplyAsync(BotUtils.KamtroText($"You already have the {CultureInfo.CurrentCulture.TextInfo.ToTitleCase(role.Name.ToLower())} role.")); return; } // Third person response elsewhere - Arcy else { // Reply using nickname - Arcy if (user.Nickname != null) { await ReplyAsync(BotUtils.KamtroText($"{user.Nickname} already has the {CultureInfo.CurrentCulture.TextInfo.ToTitleCase(role.Name.ToLower())} role.")); return; } // Reply using username - Arcy else { await ReplyAsync(BotUtils.KamtroText($"{user.Username} already has the {CultureInfo.CurrentCulture.TextInfo.ToTitleCase(role.Name.ToLower())} role.")); return; } } } /// ADDING ROLE // Check if it is a modifiable role - Arcy else if (ServerData.ModifiableRoles.Contains(role)) { // Catch instance that the role is higher in heirarchy than bot role - Arcy if (role.Position >= ServerData.KamtroBotRole.Position) { await ReplyAsync(BotUtils.KamtroText($"Uh oh! I cannot manage that role! Please contact {ServerData.PrimaryContactUser.Username}#{ServerData.PrimaryContactUser.Discriminator} and let them know about this!")); return; } else { // Add the role! Woohoo! - Arcy await user.AddRoleAsync(role); // First person response if DMs - Arcy if (Context.Channel is IDMChannel) { await ReplyAsync(BotUtils.KamtroText($"You now have the {CultureInfo.CurrentCulture.TextInfo.ToTitleCase(role.Name.ToLower())} role.")); return; } // Third person response elsewhere - Arcy else { // Reply using nickname - Arcy if (user.Nickname != null) { await ReplyAsync(BotUtils.KamtroText($"{user.Nickname} now has the {CultureInfo.CurrentCulture.TextInfo.ToTitleCase(role.Name.ToLower())} role.")); return; } // Reply using username - Arcy else { await ReplyAsync(BotUtils.KamtroText($"{user.Username} now has the {CultureInfo.CurrentCulture.TextInfo.ToTitleCase(role.Name.ToLower())} role.")); return; } } } } /// NOT MODIFIABLE // Not modifiable. Respond with the role not being able to be added/removed - Arcy else { // Extra flavor for adding mod/admin roles - Arcy if (ServerData.ModeratorRoles.Contains(role) || role.Permissions.Administrator) { // First person response if DMs - Arcy if (Context.Channel is IDMChannel) { await ReplyAsync(BotUtils.KamtroText($"Nice try.")); return; } // Third person response elsewhere - Arcy else { // Reply using nickname - Arcy if (user.Nickname != null) { await ReplyAsync(BotUtils.KamtroText($"Nice try, {user.Nickname}.")); return; } // Reply using username - Arcy else { await ReplyAsync(BotUtils.KamtroText($"Nice try, {user.Username}.")); return; } } } // And more flavor for the bot role - Arcy else if (role == ServerData.KamtroBotRole) { await ReplyAsync(BotUtils.KamtroText($"There can only be one.")); return; } else { await ReplyAsync(BotUtils.KamtroText($"The {CultureInfo.CurrentCulture.TextInfo.ToTitleCase(role.Name.ToLower())} role cannot be added.")); return; } } } } /// ROLE CANNOT BE FOUND await ReplyAsync(BotUtils.KamtroText($"I do not recognize that role. Please make sure you are spelling the role correctly. (Copy-Paste should always work!)")); } }
public async Task RemoveRoleAsync([Remainder] string message = "") { SocketGuildUser user = BotUtils.GetGUser(Context); KLog.Debug($"RM - Modifiable Roles: {(ServerData.ModifiableRoles == null ? "NULL" : ServerData.ModifiableRoles.ToString())}"); KLog.Debug($"RM - All Roles: {(ServerData.AllRoles == null ? "NULL" : ServerData.AllRoles.ToString())}"); KLog.Debug($"RM - User: {user}"); // Find if user entered a role if (string.IsNullOrWhiteSpace(message)) { SocketGuildUser _user = BotUtils.GetGUser(Context); RoleEmbed embed = new RoleEmbed(Context, _user); await embed.Display(Context.Channel); ulong id = Context.Message.Author.Id; if (EventQueueManager.EventQueue.ContainsKey(id)) { // If the user is in the queue EventQueueManager.EventQueue[id].Add(new EventQueueNode(embed)); // Add the action to their list } else { // otherwise EventQueueManager.EventQueue.Add(id, new List <EventQueueNode>()); // Create the list EventQueueManager.EventQueue[id].Add(new EventQueueNode(embed)); // And add the action to their list } } else { // Check all roles - Arcy foreach (SocketRole role in ServerData.AllRoles) { // Find if the message matches the role closely enough - Arcy if (UtilStringComparison.CompareWordScore(message, role.Name) >= 0.66) { // ALREADY DOESN'T HAVE ROLE // Check if user already doesn't have the role - Arcy if (!user.HasRole(role)) { // First person response if DMs - Arcy if (Context.Channel is IDMChannel) { await ReplyAsync(BotUtils.KamtroText($"You already do not have the {CultureInfo.CurrentCulture.TextInfo.ToTitleCase(role.Name.ToLower())} role.")); return; } // Third person response elsewhere - Arcy else { // Reply using nickname - Arcy if (user.Nickname != null) { await ReplyAsync(BotUtils.KamtroText($"{user.Nickname} already does not have the {CultureInfo.CurrentCulture.TextInfo.ToTitleCase(role.Name.ToLower())} role.")); return; } // Reply using username - Arcy else { await ReplyAsync(BotUtils.KamtroText($"{user.Username} already does not have the {CultureInfo.CurrentCulture.TextInfo.ToTitleCase(role.Name.ToLower())} role.")); return; } } } /// REMOVING ROLE // Check if it is a modifiable role - Arcy else if (ServerData.ModifiableRoles.Contains(role)) { // Catch instance that the role is higher in heirarchy than bot role - Arcy KLog.Debug($"ROLE: {(role == null ? "NULL" : role.ToString())}"); KLog.Debug($"BOT ROLE: {(ServerData.KamtroBotRole == null ? "NULL" : ServerData.KamtroBotRole.ToString())}"); if (role.Position >= ServerData.KamtroBotRole.Position) { await ReplyAsync(BotUtils.KamtroText($"Uh oh! I cannot manage that role! Please contact Arcy or Carbon and let them know about this!")); break; } else { // Remove the role! Woohoo! - Arcy await user.RemoveRoleAsync(role); // First person response if DMs - Arcy if (Context.Channel is IDMChannel) { await ReplyAsync(BotUtils.KamtroText($"You no longer have the {CultureInfo.CurrentCulture.TextInfo.ToTitleCase(role.Name.ToLower())} role.")); return; } // Third person response elsewhere - Arcy else { // Reply using nickname - Arcy if (user.Nickname != null) { await ReplyAsync(BotUtils.KamtroText($"{user.Nickname} no longer has the {CultureInfo.CurrentCulture.TextInfo.ToTitleCase(role.Name.ToLower())} role.")); return; } // Reply using username - Arcy else { await ReplyAsync(BotUtils.KamtroText($"{user.Username} no longer has the {CultureInfo.CurrentCulture.TextInfo.ToTitleCase(role.Name.ToLower())} role.")); return; } } } } /// NOT MODIFIABLE // Not modifiable. Respond with the role not being able to be added/removed - Arcy else { // Extra flavor for removing mod roles - Arcy if (ServerData.ModeratorRoles.Contains(role)) { // First person response if DMs - Arcy if (Context.Channel is IDMChannel) { await ReplyAsync(BotUtils.KamtroText($"Just ask Kamex or Retro.")); return; } // Third person response elsewhere - Arcy else { // Reply using nickname - Arcy if (user.Nickname != null) { await ReplyAsync(BotUtils.KamtroText($"Just ask Kamex or Retro, {user.Nickname}.")); return; } // Reply using username - Arcy else { await ReplyAsync(BotUtils.KamtroText($"Just ask Kamex or Retro, {user.Username}.")); return; } } } else { await ReplyAsync(BotUtils.KamtroText($"The {CultureInfo.CurrentCulture.TextInfo.ToTitleCase(role.Name.ToLower())} role cannot be removed.")); return; } } } } /// ROLE CANNOT BE FOUND await ReplyAsync(BotUtils.KamtroText($"I do not recognize that role. Please make sure you are spelling the role correctly. (Copy-Paste should always work!)")); } }
public async Task MessageChannelAsync([Remainder] string args = "") { if (!ServerData.HasPermissionLevel(BotUtils.GetGUser(Context), ServerData.PermissionLevel.ADMIN)) { return; } if (string.IsNullOrWhiteSpace(args)) { await ReplyAsync(BotUtils.KamtroText("You need to say a message!")); return; } SocketTextChannel target = null; // Check if a channel name was specified string[] arrayCheck = Context.Message.Content.Split(' '); string channelToCheck; // Check if a channel was even in the message if (arrayCheck.Length > 1) { channelToCheck = arrayCheck[1]; // Check if channel is mentioned in DMs if (Context.Channel is IDMChannel && channelToCheck.StartsWith("<#")) { target = ServerData.Server.GetTextChannel(ulong.Parse(channelToCheck.Substring(2, channelToCheck.Length - 3))); } else { // Check for name matches foreach (SocketTextChannel textChannel in ServerData.Server.TextChannels) { if (UtilStringComparison.CompareWordScore(channelToCheck, textChannel.Name) > 0.66) { target = textChannel; break; } } } } else if (Context.Message.MentionedChannels.Count < 1) { await ReplyAsync(BotUtils.KamtroText("You need to specify the channel!")); return; } if (target == null) { try { target = Context.Message.MentionedChannels.ElementAt(0) as SocketTextChannel; } catch (Exception) { } } if (target == null) { await ReplyAsync(BotUtils.KamtroText("You need to specify a text channel!")); return; } List <string> msgl = args.Split(' ').ToList(); msgl.RemoveAt(0); if (msgl.Count <= 0) { await ReplyAsync(BotUtils.KamtroText("You need to say a message!")); return; } string msg = string.Join(" ", msgl.ToArray()); await target.SendMessageAsync(BotUtils.KamtroText(msg)); await Context.Message.AddReactionAsync(ReactionHandler.CHECK_EM); KLog.Info($"Kamtro Message Sent in #{target.Name} from [{BotUtils.GetFullUsername(Context.User)}]: {msg}"); }