'Listen for only an image in discord.net

I have an issue in which i'm looking for our discord bot to only look for images received and ignore any text typed. From the guides I have read, I have yet to come across any that hasn't required a command.

I have tried to use a command with no command within the string, however it doesn't build as it doesn't contain a parameter.

Does anyone have any ideas how I can just listen for an image only?

Below is an example of my code.

    private async Task _client_MessageReceived(SocketMessage arg)
    {
        var message = arg as SocketUserMessage;
        var context = new SocketCommandContext(_client, message);
        if (message.Author.IsBot) return;

        int argPos = 0;
            if (message.HasStringPrefix("!", ref argPos) || message.Attachments.Count > 0)
            {
                var result = await _commands.ExecuteAsync(context, argPos, _services);
                
                if (!result.IsSuccess) Console.WriteLine(result.ErrorReason);
            }
            else
                await message.DeleteAsync();
    }

        [Command("")]
    public async Task Photo()
    {
        var attachments = Context.Message.Attachments;

        WebClient myWebClient = new WebClient();

        string file = attachments.ElementAt(0).Filename;
        string url = attachments.ElementAt(0).Url;

        myWebClient.DownloadFile(url, @"mydirect");

        _ = Task.Run(async () =>
        {
            AWS.AWS.Get_kv_map(@"mydirect");
        });
    }


Solution 1:[1]

my sugestion is to check if message.Attachments != 0 ==> do your thing, with that you can check if it has any Attachments is on it and after that you can check if its ending on an .jpg or .png or so.

example:

if(message.Attachments.Count != 0){
var image attachements = message.Attachments.Where(x => 
x.Filename.EndsWith(".jpg") || x.Filename.EndsWith(".png") || 
x.Filename.EndsWith(".gif")); // or what you want as "image"

if(image.Any()){
// do your stuff from your method Photo() here or just call here your method your decision
}else{
// ignore or whatever you want to do it with it
}

I hope it helped and good luck on your project :D

Solution 2:[2]

var image = message.Attachments.Where(x => x.Filename.EndsWith("*.png") ...);

This code will be one of the great solution but If you want to check that's real image then.

First, download image.

Second check magic number.

https://en.wikipedia.org/wiki/List_of_file_signatures This wikipedia link has list of magic number, for example, MZ means PE file.

If byte of file not starts with MZ, windows will deny of execution.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Dayo