· 6 years ago · Sep 29, 2019, 03:36 PM
1const Discord = require("discord.js");
2const client = new Discord.Client();
3const config = require("./config.json");
4const querystring = require('querystring');
5const r2 = require('r2');
6const DOG_API_URL = "https://api.thedogapi.com/";
7const DOG_API_KEY = config.DOG_API_KEY; // get a free key from - https://thedogapi.com/signup
8const CAT_API_URL = "https://api.thecatapi.com/";
9const CAT_API_KEY = config.CAT_API_KEY; // get a free key from - https://thedogapi.com/signup
10
11client.on("ready", () => {
12 console.log("I am ready!");
13});
14
15client.on("message", (message) => {
16
17 if(message.author.bot) return;
18 // This is where we'll put our code.
19 if (message.content.indexOf(config.prefix) !== 0) return;
20
21 const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
22 const command = args.shift().toLowerCase();
23
24 if (command === 'ping') {
25 message.channel.send("this joke is old");
26 } else
27 if (command === 'havish') {
28 message.channel.send("<@" + "226096269901299713" + "> is one cool dude :sunglasses:")
29 }
30 if (command === 'owneraccess') {
31 if (message.author.id == config.ownerID) {
32 message.channel.send("<@" + config.ownerID + "> is the most awesome person")
33 } else {
34 message.channel.send("You suck.")
35}
36 }
37 if (command === "asl") {
38 let [age, sex, location] = args;
39 message.reply(`Hello ${message.author.username}, I see you're a ${age} year old ${sex} from ${location}. Wanna date?`);
40 }
41 if (command === "say") {
42 let text = args.join(" ");
43 message.delete();
44 message.channel.send(text);
45 }
46 if (command === "kick") {
47 let member = message.mentions.members.first();
48 let reason = args.slice(1).join(" ");
49 member.kick(reason);
50 }
51 if (command === 'woof') {
52 messageRecievedWoof(message);
53 }
54 if (command === 'meow') {
55 messageRecievedMeow(message);
56 }
57 if (command === 'help') {
58 message.channel.send("**!ping:** Displays an old trick \n**!havish:** Praises the lord \n**!owneraccess:** A trick that only works for the owner \n**!asl:** Asks you out *(Usage: !asl [age] [sex] [loaction])* \n**!say:** Says what you want and deletes your message *(Usage: !say [blah blah blah])* \n**!kick:** Kicks the user *(Usage: !kick [@user] [reason])* \n**!woof:** Sends an image of a dog \n**!meow:** Sends an image of a cat")
59 }
60
61 client.on('error', data => {
62 console.log('error', data);
63 // attempt reconnection x times, after x seconds, exponential backoff
64 });
65
66 /**
67 * Called whenever a message is posted into the same channel as the Bot
68 */
69 async function messageRecievedWoof(message) {
70 try {
71 // pass the name of the user who sent the message for stats later, expect an array of images to be returned.
72 var images = await loadImageWoof(message.author.username);
73
74 // get the Image, and first Breed from the returned object.
75 var image = images[0];
76 var breed = image.breeds[0];
77
78 console.log('message processed', 'showing', breed)
79 // use the *** to make text bold, and * to make italic
80 message.channel.send("***" + breed.name + "*** \r *" + breed.temperament + "*", {
81 files: [image.url]
82 });
83 // if you didn't want to see the text, just send the file
84
85 } catch (error) {
86 console.log(error)
87 }
88 }
89 async function messageRecievedMeow(message) {
90 try {
91 // pass the name of the user who sent the message for stats later, expect an array of images to be returned.
92 var images = await loadImageMeow(message.author.username);
93
94 // get the Image, and first Breed from the returned object.
95 var image = images[0];
96 var breed = image.breeds[0];
97
98 console.log('message processed', 'showing', breed)
99 // use the *** to make text bold, and * to make italic
100 message.channel.send("***" + breed.name + "*** \r *" + breed.temperament + "*", {
101 files: [image.url]
102 });
103 // if you didn't want to see the text, just send the file
104
105 } catch (error) {
106 console.log(error)
107 }
108 }
109 /**
110 * Makes a request to theDogAPI.com for a random dog image with breed info attached.
111 */
112 async function loadImageWoof(sub_id) {
113 // 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
114 var headers = {
115 'X-API-KEY': DOG_API_KEY,
116 }
117 var query_params = {
118 'has_breeds': true, // we only want images with at least one breed data object - name, temperament etc
119 'mime_types': 'jpg,png', // we only want static images as Discord doesn't like gifs
120 'size': 'small', // get the small images as the size is prefect for Discord's 390x256 limit
121 'sub_id': sub_id, // pass the message senders username so you can see how many images each user has asked for in the stats
122 'limit': 1 // only need one
123 }
124 // convert this obejc to query string
125 let queryString = querystring.stringify(query_params);
126
127 try {
128 // construct the API Get request url
129 let _url = DOG_API_URL + `v1/images/search?${queryString}`;
130 // make the request passing the url, and headers object which contains the API_KEY
131 var response = await r2.get(_url, {
132 headers
133 }).json
134 } catch (e) {
135 console.log(e)
136 }
137 return response;
138
139 }
140 /**
141 * Makes a request to theDogAPI.com for a random dog image with breed info attached.
142 */
143 async function loadImageMeow(sub_id) {
144 // 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
145 var headers = {
146 'X-API-KEY': CAT_API_KEY,
147 }
148 var query_params = {
149 'has_breeds': true, // we only want images with at least one breed data object - name, temperament etc
150 'mime_types': 'jpg,png', // we only want static images as Discord doesn't like gifs
151 'size': 'small', // get the small images as the size is prefect for Discord's 390x256 limit
152 'sub_id': sub_id, // pass the message senders username so you can see how many images each user has asked for in the stats
153 'limit': 1 // only need one
154 }
155 // convert this obejc to query string
156 let queryString = querystring.stringify(query_params);
157
158 try {
159 // construct the API Get request url
160 let _url = CAT_API_URL + `v1/images/search?${queryString}`;
161 // make the request passing the url, and headers object which contains the API_KEY
162 var response = await r2.get(_url, {
163 headers
164 }).json
165 } catch (e) {
166 console.log(e)
167 }
168 return response;
169
170 }
171 });
172
173
174client.login(config.token);