public async Task TagGetCommandAsync(string tagName) { // check if user intended to run a different command but forgot some parameters // since this is the .tag command and other commands use .tag as their base, the bot may // redirect commands to this function, which will cause it to search for the command function // as if it were a tag - catch these and notify the user to check their syntax. // doing it this way so we can keep the base .tag command and have .tag function commands as well List <string> commandCatchFilter = new List <string>() { "add", "remove", "edit", "rename", "describe", "global", "info", "search" }; if (commandCatchFilter.Contains(tagName.ToLower())) { await ReplyAsync("You're missing some parts from that command. Check your syntax."); return; } var response = await DatabaseTags.GetTagContentsFromDatabase(Context, tagName); // if we found a response, use it if (response != null) { await ReplyAsync(response); } else { // attempt to find a tag & retry await FindTagAndRetry("get", tagName); } }
public async Task TagGetByUserCommandAsync(IUser user = null) { var results = await DatabaseTags.GetTagsByUserFromDatabase(Context, user); if (results.Any()) { EmbedBuilder embedBuilder = new EmbedBuilder(); StringBuilder stringBuilder = new StringBuilder(); foreach (var result in results) { stringBuilder.AppendLine(result); } embedBuilder.AddField("Tags", stringBuilder.ToString(), true); // build author field with mentioned user or calling user if (user != null) { embedBuilder.WithAuthor(user.Username, user.GetAvatarUrl()); } else { embedBuilder.WithAuthor(Context.User.Username, Context.User.GetAvatarUrl()); } await ReplyAsync(null, false, embedBuilder.Build()); } else { await ReplyAsync("User hasn't made any tags."); } }
public async Task TagMakeCommandAsync(string tagName = null) { // find a msg that the calling user has reacted with the right emote to IEmote starEmote = new Emoji("⭐"); var message = await EventReactionAddedService.GetMessageByReactionAdded(starEmote, Context); if (message == null) { await ReplyAsync($"You need to select a message (using an {starEmote} reaction) to use this command."); } await ReplyAsync("What do you want to name your tag?"); var userResponseTagName = await NextMessageAsync(true, true, TimeSpan.FromSeconds(30)); if (userResponseTagName == null) { await ReplyAsync("You took too long choosing a name. Try again."); return; } var success = await DatabaseTags.AddTagToDatabase(Context, userResponseTagName.Content, message.Message.Content); if (success) { await ReplyAsync($"Tag '{userResponseTagName}' added."); } else { await ReplyAsync("Couldn't add tag - a tag with this name already exists."); } }
public async Task TagGetAllCommandAsync(string extra = null) { var tags = await DatabaseTags.GetAllTagsFromDatabase(Context); if (tags.Any()) { EmbedBuilder embedBuilder = new EmbedBuilder(); StringBuilder tagsGlobalColumnbuilder = new StringBuilder(); StringBuilder tagsLocalColumnbuilder = new StringBuilder(); var iGlobal = 0; var iLocal = 0; foreach (var tag in tags) { if (tag.Global) { tagsGlobalColumnbuilder.AppendLine(tag.Name); } if (tag.Global == false) { tagsLocalColumnbuilder.AppendLine(tag.Name); } } embedBuilder.AddField("Global tags", tagsGlobalColumnbuilder.ToString(), true); embedBuilder.AddField("Local tags", tagsLocalColumnbuilder.ToString(), true); await ReplyAsync(null, false, embedBuilder.Build()); } else { await ReplyAsync("No tags found for this server."); } }
// if the user supplies a tagname that doesn't exist, search the database to see if there are // any tags containing the user-supplied text // if there are any results from the database, post a list of the results & add emoji reactions // corresponding to each item in the list // when the user selects a emoji, it will pass the corresponding tagName and any extra passed // parameters to the RetryCommandUsingFoundTag function // afterwards, save a pairing of the calling user object and the searchResults message into a // dictionary, for use by the RetryCommandUsingFoundTag function private async Task FindTagAndRetry(string functionToRetry, params string[] args) { var tagName = args[0]; var searchResponse = await DatabaseTags.SearchTagsInDatabase(Context, tagName); // if single result, just use that if (searchResponse.Count == 1) { await RetryCommandUsingFoundTag(searchResponse[0], functionToRetry, args); return; } // if the single-result check didn't catch but there are results if (searchResponse.Any()) { string[] numbers = new[] { "0⃣", "1⃣", "2⃣", "3⃣", "4⃣", "5⃣", "6⃣", "7⃣", "8⃣", "9⃣" }; var numberEmojis = new List <Emoji>(); EmbedBuilder embedBuilder = new EmbedBuilder(); StringBuilder stringBuilder = new StringBuilder(); // add the number of emojis we need to the emojis list, and build our string-list of search results for (int i = 0; i < searchResponse.Count && i < numbers.Length; i++) { numberEmojis.Add(new Emoji(numbers[i])); stringBuilder.AppendLine($"{numbers[i]} - {searchResponse[i]}"); } embedBuilder.WithDescription(stringBuilder.ToString()); embedBuilder.WithColor(Color.Blue); // build a message and add reactions to it // reactions will be watched, and the one selected will fire the HandleFindTagReactionResult method, passing // that reaction's corresponding tagname and the function passed into this parameter var messageContents = new ReactionCallbackData("Did you mean... ", embedBuilder.Build()); for (int i = 0; i < searchResponse.Count; i++) { var counter = i; messageContents.AddCallBack(numberEmojis[counter], (c, r) => RetryCommandUsingFoundTag(searchResponse[counter], functionToRetry, args)); } var message = await InlineReactionReplyAsync(messageContents); // add calling user and searchResults embed to a dict as a pair // this way we can hold multiple users' reaction messages and operate on them separately _dictFindTagUserEmbedPairs.Add(Context.User, message); } else { await ReplyAsync("I can't find any tags like what you're looking for."); } }
public async Task TagGetAllCommandAsync(string extra = null) { var tags = await DatabaseTags.GetAllTagsFromDatabase(Context); tags = tags.OrderBy(x => x.Name).ToList(); if (tags.Any()) { EmbedBuilder embedBuilder = new EmbedBuilder(); StringBuilder tagsGlobalColumnbuilder = new StringBuilder(); StringBuilder tagsLocalColumnbuilder = new StringBuilder(); foreach (var tag in tags) { if (tag.Global) // global { tagsGlobalColumnbuilder.AppendLine(tag.Name); } if (tag.Global == false) // local { tagsLocalColumnbuilder.AppendLine(tag.Name); } } // if a builder has nothing in it (no tags), fill it with something if (tagsGlobalColumnbuilder.Length == 0) { tagsGlobalColumnbuilder.AppendLine("No global tags :c"); } if (tagsLocalColumnbuilder.Length == 0) { tagsLocalColumnbuilder.AppendLine("No local tags :c"); } embedBuilder.AddField("Global tags", tagsGlobalColumnbuilder.ToString(), true); embedBuilder.AddField("Local tags", tagsLocalColumnbuilder.ToString(), true); await ReplyAsync(null, false, embedBuilder.Build()); } else { await ReplyAsync("No tags found for this server."); } }
public async Task TagGetInfoCommandAsync(string tagName) { var tag = await DatabaseTags.GetTagInfoFromDatabase(Context, tagName); if (tag != null) { EmbedBuilder embedBuilder = new EmbedBuilder(); // get author info var author = DiscordSocketClient.GetUser((ulong)tag.AuthorId); embedBuilder.Title = $"Tag: {tagName}"; // if the author of the tag still exists in the server, use their name // otherwise, just display unknown, no need to handle this if (author != null) { embedBuilder.AddField("Owner", author, true); } else { embedBuilder.AddField("Owner", "Unknown", true); } // if description is set, display it if (tag.Description.Length > 0) { embedBuilder.AddField("Description", tag.Description); } embedBuilder.AddField("Uses", tag.Uses, true); embedBuilder.AddField("Global", tag.Global, true); embedBuilder.WithFooter("Tag created at"); embedBuilder.WithTimestamp(tag.DateAdded); embedBuilder.WithColor(Color.Blue); await ReplyAsync(null, false, embedBuilder.Build()); } else { // attempt to find a tag & retry await FindTagAndRetry("info", tagName); } }
public async Task TagDescribeCommandAsync(string tagName, [Remainder] string description) { var result = await DatabaseTags.EditTagInDatabase(Context, tagName, "description", description); if (result == 2) { await ReplyAsync($"Tag '{tagName}' description set."); } else if (result == 1) { await ReplyAsync("Couldn't set tag's description - you are not the author of that tag."); } else if (result == 0) { // attempt to find a tag & retry await FindTagAndRetry("describe", tagName, description); } }
public async Task TagRenameCommandAsync(string tagName, string newName) { var result = await DatabaseTags.EditTagInDatabase(Context, tagName, "name", newName); if (result == 2) { await ReplyAsync($"Tag '{tagName}' renamed to '{newName}'."); } else if (result == 1) { await ReplyAsync("Couldn't rename tag - you are not the author of that tag."); } else if (result == 0) { // attempt to find a tag & retry await FindTagAndRetry("rename", tagName, newName); } }
public async Task TagEditCommandAsync(string tagName, [Remainder] string newContent) { var result = await DatabaseTags.EditTagInDatabase(Context, tagName, "text", newContent); if (result == 2) { await ReplyAsync($"Tag '{tagName}' edited."); } else if (result == 1) { await ReplyAsync("Couldn't edit tag - you are not the author of that tag."); } else if (result == 0) { // attempt to find a tag & retry await FindTagAndRetry("edit", tagName, newContent); } }
public async Task TagRemoveCommandAsync(string tagName) { var result = await DatabaseTags.RemoveTagFromDatabase(Context, tagName); if (result == 2) { await ReplyAsync($"Tag '{tagName}' deleted."); } else if (result == 1) { await ReplyAsync("Couldn't remove tag - you are not the author of that tag."); } else if (result == 0) { // attempt to find a tag & retry await FindTagAndRetry("remove", tagName); } }
public async Task TagSearchCommandAsync(string search) { var results = await DatabaseTags.SearchTagsInDatabase(Context, search); if (results != null) { EmbedBuilder embedBuilder = new EmbedBuilder(); StringBuilder stringBuilder = new StringBuilder(); foreach (var result in results) { stringBuilder.AppendLine(result); } embedBuilder.AddField("Tags", stringBuilder.ToString(), true); await ReplyAsync(null, false, embedBuilder.Build()); } else { await ReplyAsync("No results found."); } }
public async Task TagAddCommandAsync(string tagName, [Remainder] string content = null) { bool messageContainsAttachment = Context.Message.Attachments.Any(); if (content == null && !messageContainsAttachment) { await ReplyAsync( "You did not enter anything to put inside the tag. The syntax is `.tag add name content`."); return; } StringBuilder tagContents = new StringBuilder(); if (content != null) { tagContents.AppendLine(content); } if (messageContainsAttachment) { foreach (var attachment in Context.Message.Attachments) { tagContents.AppendLine(attachment.Url); } } var success = await DatabaseTags.AddTagToDatabase(Context, tagName, tagContents.ToString()); if (success) { await ReplyAsync($"Tag '{tagName}' added."); } else { await ReplyAsync("Couldn't add tag - a tag with this name already exists."); } }
public async Task TagGlobalCommandAsync(string tagName, string flag) { bool global; if (flag == "true" || flag == "yes") { global = true; } else if (flag == "false" || flag == "no") { global = false; } else { await ReplyAsync($"Invalid flag '{flag}' entered. Command accepts true/yes and false/no."); return; } var result = await DatabaseTags.EditTagInDatabase(Context, tagName, "global", global); if (result == 2) { await ReplyAsync($"Tag '{tagName}' global status set to '{flag}'."); } else if (result == 1) { await ReplyAsync("Couldn't set tag's global status - you are not the author of that tag."); } else if (result == 0) { // attempt to find a tag & retry await FindTagAndRetry("global", tagName, flag); } }