#Attach an Image to a command

1 messages · Page 1 of 1 (latest)

vernal coral
#

Hey, I was curious How could I add an Image Attachment into ths?

            builder.ConfigureEventHandlers
            (
                fetchcard => fetchcard.HandleMessageCreated(async (s, e) =>
                {
                    string message = e.Message.Content;
                    MatchCollection matches = Regex.Matches(message, @"\[\[(.*?)\]\]");
                    List<string> extractedContents = new List<string>();

                    foreach (Match match in matches)
                    {
                        extractedContents.Add(match.Groups[1].Value.Trim()); // Trim extracted content
                    }

                    if (extractedContents.Count > 0)
                    {
                        string output = "The Outputs are as follows:";
                        foreach (string content in extractedContents)
                        {
                            logger.Info(content);
                            // Compare with trimmed content and case-insensitive
                            if (NotionEnd.CtiCardNames.Contains(content, StringComparer.OrdinalIgnoreCase))
                            {
                                output += "\ntrue\n";
                                var carddictinaryvalue = NotionEnd.CtiCards.GetValueOrDefault(content);
                                var pagefetchresults = await NotionPageFetcher.FetchPageInfo(content, "card");
                                var textnimages = await FetchImageAndParagraph.FetchTextandImage(pagefetchresults.textBlocks, pagefetchresults.imageUrls);
                                output += textnimages.textBlocks;
                                
                            }
                            else
                            {
                                output += "\nfalse";
                            }
                        }
                        await e.Message.RespondAsync(output);
                    }
                })
            );
timid inlet
#

use DiscordMessageBuilder

#

it'll let you pass the message content you already have as well as images

vernal coral
#

👍

vernal coral
timid inlet
vernal coral
#

I dont know how to call;

upbeat tiger
#

its a eventhandler for messagecreated

#

also as the todo explains we highly recommend using our command lib (make sure to use DSP.Commands instead of DSP.CommandsNext (the first one is the successor))

vernal coral
#

Yeah but Im pretty positive thats not where the issue is

// Create the client
DiscordClient discord = new(new DiscordConfiguration
{
    Token = token,
    Intents = DiscordIntents.AllUnprivileged | DiscordIntents.MessageContents
});
``` This is in the example

and this is my setup

DiscordClientBuilder builder = DiscordClientBuilder.CreateDefault(KamWerksID, DiscordIntents.AllUnprivileged | DiscordIntents.MessageContents);

upbeat tiger
#

what version are you using?

vernal coral
#

the latest as reccomend on the wiki

upbeat tiger
#

this part doesnt work anymore discord.MessageCreated += CommandInvokerAsync; but you have used the new event handler registration already. You can add the method in the ConfigureEventHandlers

vernal coral
#

Got it will look into it ^^

#

So that helped with a piece of it now gotta get the output working again :D

#

Got closer the log outputs back just gotta get message output working

vernal coral
#

So how would I attach the image without knowing the path of it?

#

[or without it being a string]

timid inlet
#

you need a stream of some sorts

#

there's an overload that takes an arbitrary stream

#

(you do need to provide a filename though, since it needs a name when uploading; or you can provide a filestream which already has the name afaik)

vernal coral
#

Got it guess ill need to dig into how tf to get the stream from an image in a notion image block

vernal coral
#

Decided to just get a link from the github end not figuring out how to get the url from notions end

vernal coral
timid inlet
#

well yeah you cant download into a filestream

vernal coral
#

Ah so I need to locally download upload than clean the cache?

timid inlet
#

you can download it into memory and pass that

vernal coral
#

Well I know the files downloading just when im trying to get it, it goes on a blip

var imageurl = $"https://raw.githubusercontent.com/SaxbyMod/NotionAssets/refs/heads/main/Formats/{textnimages.textBlocks[1].TrimStart().Replace(" ", "%20").Replace("\n", "")}/Portraits/{textnimages.textBlocks[3].TrimStart().Replace(" ", "%20").Replace("\n", "")}.png";
var file = await downloadFiles(imageurl);
Assembly assembly = Assembly.GetExecutingAssembly();
var path = "..//" + assembly;
var pathnew = Path.GetFullPath(path);
var filepath = Path.Combine(pathnew, content + ".png");
var fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);
messageBuilder.AddFile(fs);
logger.Info(imageurl);
#

For ref my download request

private static async Task<string> downloadFiles(string url)
{
    string file = System.IO.Path.GetFileName(url);
    WebClient cln = new WebClient();
    cln.DownloadFileTaskAsync(url, file);
    return file;
}
timid inlet
vernal coral
#

Sorry, for me being a Nuisance guess Im just looking in the wrong places lemme go take a dive there

timid inlet
#

for future reference, you should generally not use WebClient

#

and make sure to put your HttpClient into a static field or the likes, it shouldn't be allocated every time

vernal coral
#

So what I now have is this;

HttpClient httpClient = new HttpClient();
var file = await httpClient.GetStreamAsync(imageurl);
var newfile = file as FileStream;
messageBuilder.AddFile(newfile);
``` Judging by what I see it should function however I do not know how to convert it into a filestream as needed by addfile.... and sorry for not understanding this stuff its one of the first times im messing with web requests [specifically for files].
timid inlet
#

you don't need a FileStream

#

you just need A Stream

#

the stream you get is totally fine

vernal coral
#

Really becuase VS is screaming;

timid inlet
#

you need a different overload, yes

vernal coral
#

Got it

#

well thats still not sending :D

#

also that error seems to relate to this;

#

so im thinking im doing something wrong somewhere just not finding it

timid inlet
#

well, what's your code

vernal coral
timid inlet
#

this looks like you're just always returning

#

before ever hitting this particular message

vernal coral
#

Well that for sure has to return null or its on a infinite loop lmao, also dont think that bits causing it anyhow

timid inlet
#

well, you're sending the message right below returning

vernal coral
#

Ah

timid inlet
#

this does not look ideal.

vernal coral
#

Yeah just realized for some reason ive been ignoring the fact that that lines been on the ignore lol gonna try bringing it above return message = output

#

nevermind that causes another thing to scream :D

timid inlet
#

stacktrace?

vernal coral
#

well got somewhere I now have the image but not the text :D