public async Task ProfileParser([Remainder] string args = null) { // Check whitelist: if (!Whitelist.IsServerOnList(Context.Guild.Id)) { // Ignore if not on list: return; } using (Context.Channel.EnterTypingState()) { // Get IG account: InstagramProcessor instagram = new InstagramProcessor(InstagramProcessor.AccountFinder.GetIGAccount()); string url = "https://instagram.com/" + args.Replace(" ", "/"); // Process profile: InstagramProcessorResponse response = await instagram.PostRouter(url, (int)Context.Guild.PremiumTier, 1); // Check for failed post: if (!response.success) { await Context.Message.ReplyAsync(response.error); return; } // If not a profile for some reason, treat otherwise: if (!response.onlyAccountData) { await Responder(url, Context); return; } IGEmbedBuilder embed = new IGEmbedBuilder(response, Context.User.Username); IGComponentBuilder component = new IGComponentBuilder(response, Context.User.Id); await Context.Message.ReplyAsync(embed : embed.AutoSelector(), allowedMentions : AllowedMentions.None, components : component.AutoSelector()); //Attempt to remove any automatic embeds: DiscordTools.SuppressEmbeds(Context); } }
/// <summary> /// Handle an executed component /// Used for buttons, drop downs, etc. /// </summary> /// <param name="arg1"></param> /// <param name="arg2"></param> /// <param name="arg3"></param> /// <returns></returns> private Task ComponentCommandExecuted(ComponentCommandInfo arg1, Discord.IInteractionContext arg2, Discord.Interactions.IResult arg3) { if (!arg3.IsSuccess) { // Defer if not already done: try { arg2.Interaction.DeferAsync(true).GetAwaiter().GetResult(); } catch { // ignore } switch (arg3.Error) { case InteractionCommandError.UnmetPrecondition: // Check for userperm error: if (arg3.ErrorReason.Contains("UserPerm")) { arg2.Interaction.FollowupAsync("You do not have permission to execute this command.", ephemeral: true); break; } arg2.Interaction.FollowupAsync("Action Failed\n" + arg3.ErrorReason, ephemeral: true); break; case InteractionCommandError.UnknownCommand: arg2.Interaction.FollowupAsync("Unknown action. It may have been recently removed or changed.", ephemeral: true); break; case InteractionCommandError.BadArgs: arg2.Interaction.FollowupAsync("The provided values are invalid. (BadArgs)", ephemeral: true); break; case InteractionCommandError.Exception: //notify owner if desired: if (arg3.ErrorReason.Contains("Invalid Form Body")) { arg2.Interaction.FollowupAsync("Invalid form body. Please check to ensure that all of your parameters are correct.", ephemeral: true); break; } if (notifyOwnerOnError && !string.IsNullOrEmpty(_config["OwnerID"])) { string error = Format.Bold("Error:") + "\n" + Format.Code(arg3.ErrorReason) + "\n\n" + Format.Bold("Command:") + "\n" + Format.BlockQuote(arg1.Name + " " + DiscordTools.SlashParamToString(arg2)); if (error.Length > 2000) { error = error.Substring(0, 2000); } UserExtensions.SendMessageAsync(_client.GetUser(ulong.Parse(_config["OwnerID"])), error); } arg2.Interaction.FollowupAsync("Sorry, Something went wrong...", ephemeral: true); break; case InteractionCommandError.Unsuccessful: //notify owner if desired: if (notifyOwnerOnError && !string.IsNullOrEmpty(_config["OwnerID"])) { string error = Format.Bold("Error:") + "\n" + Format.Code(arg3.ErrorReason) + "\n\n" + Format.Bold("Command:") + "\n" + Format.BlockQuote(arg1.Name + " " + DiscordTools.SlashParamToString(arg2)); if (error.Length > 2000) { error = error.Substring(0, 2000); } UserExtensions.SendMessageAsync(_client.GetUser(ulong.Parse(_config["OwnerID"])), error); } arg2.Interaction.FollowupAsync("Sorry, Something went wrong...", ephemeral: true); break; default: //notify owner if desired: if (notifyOwnerOnError && !string.IsNullOrEmpty(_config["OwnerID"])) { string error = Format.Bold("Error:") + "\n" + Format.Code(arg3.ErrorReason) + "\n\n" + Format.Bold("Command:") + "\n" + Format.BlockQuote(arg1.Name + " " + DiscordTools.SlashParamToString(arg2)); if (error.Length > 2000) { error = error.Substring(0, 2000); } UserExtensions.SendMessageAsync(_client.GetUser(ulong.Parse(_config["OwnerID"])), error); } arg2.Interaction.FollowupAsync("Sorry, Something went wrong..."); break; } } return(Task.CompletedTask); }
/// <summary> /// Centralized method to handle all Instagram links and respond to text based messages (No slash commands). /// </summary> /// <param name="url">The Instagram URL of the content</param> /// <param name="context">The discord context of the message</param> /// <returns></returns> private static async Task Responder(string url, ICommandContext context) { // Check whitelist: if (!Whitelist.IsServerOnList(context.Guild.Id)) { // Ignore if not on list: return; } using (context.Channel.EnterTypingState()) { // Get IG account: InstagramProcessor instagram = new InstagramProcessor(InstagramProcessor.AccountFinder.GetIGAccount()); //Process Post: InstagramProcessorResponse response = await instagram.PostRouter(url, (int)context.Guild.PremiumTier, 1); //Check for failed post: if (!response.success) { await context.Message.ReplyAsync(response.error); return; } // Embed builder: IGEmbedBuilder embed = new IGEmbedBuilder(response, context.User.Username); IGComponentBuilder component = new IGComponentBuilder(response, context.User.Id); if (response.isVideo) { if (response.stream != null) { //Response with stream: using (Stream stream = new MemoryStream(response.stream)) { FileAttachment attachment = new FileAttachment(stream, "IGMedia.mp4", "An Instagram Video."); await context.Message.Channel.SendFileAsync(attachment, embed : embed.AutoSelector(), components : component.AutoSelector()); } } else { //Response without stream: await context.Message.ReplyAsync(response.contentURL.ToString(), embed : embed.AutoSelector(), allowedMentions : AllowedMentions.None, components : component.AutoSelector()); } } else { if (response.stream != null) { using (Stream stream = new MemoryStream(response.stream)) { FileAttachment attachment = new FileAttachment(stream, "IGMedia.jpg", "An Instagram Image."); await context.Channel.SendFileAsync(attachment, embed : embed.AutoSelector(), components : component.AutoSelector()); } } else { await context.Message.ReplyAsync(embed : embed.AutoSelector(), allowedMentions : AllowedMentions.None, components : component.AutoSelector()); } } //Try to remove the embeds on the command post: DiscordTools.SuppressEmbeds(context); } }