· 6 years ago · Sep 29, 2019, 01:40 PM
1 async function messageRecieved(message) {
2 try {
3 // pass the name of the user who sent the message for stats later, expect an array of images to be returned.
4 var images = await loadImage(message.author.username);
5
6 // get the Image, and first Breed from the returned object.
7 var image = images[0];
8 var breed = image.breeds[0];
9
10 console.log('message processed', 'showing', breed)
11 // use the *** to make text bold, and * to make italic
12 message.channel.send("***" + breed.name + "*** \r *" + breed.temperament + "*", {
13 files: [image.url]
14 });
15 // if you didn't want to see the text, just send the file
16
17 } catch (error) {
18 console.log(error)
19 }
20 }
21 /**
22 * Makes a request to theDogAPI.com for a random dog image with breed info attached.
23 */
24 async function loadImage(sub_id) {
25 // you need an API key to get access to all the iamges, or see the requests you've made in the stats for your account
26 var headers = {
27 'X-API-KEY': DOG_API_KEY,
28 }
29 var query_params = {
30 'has_breeds': true, // we only want images with at least one breed data object - name, temperament etc
31 'mime_types': 'jpg,png', // we only want static images as Discord doesn't like gifs
32 'size': 'small', // get the small images as the size is prefect for Discord's 390x256 limit
33 'sub_id': sub_id, // pass the message senders username so you can see how many images each user has asked for in the stats
34 'limit': 1 // only need one
35 }
36 // convert this obejc to query string
37 let queryString = querystring.stringify(query_params);
38
39 try {
40 // construct the API Get request url
41 let _url = DOG_API_URL + `v1/images/search?${queryString}`;
42 // make the request passing the url, and headers object which contains the API_KEY
43 var response = await r2.get(_url, {
44 headers
45 }).json
46 } catch (e) {
47 console.log(e)
48 }
49 return response;
50
51 }
52
53 });