-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommands.js
160 lines (144 loc) · 5.85 KB
/
commands.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const { generateEmbedMessage, trim } = require('./helpers.js');
const axios = require('axios');
const cheerio = require('cheerio');
const got = require('got');
let enableModCommands = false;
const adultChannelName = 'adult-only-chat';
module.exports = {
dailyTasks: function (client) {
sendDailyDilbert(client);
},
matchCommand: function (Discord, config, logger, message, command, args) {
switch(command) {
case 'help':
commandHelp(Discord, config, logger, message, command, args);
break;
case 'ud':
if (message.channel.name === adultChannelName) {
commandUrbanDictionary(Discord, config, logger, message, command, args);
}
else {
const channel = message.client.channels.cache.find((channel) => channel.name === adultChannelName);
message.channel.send(`This command is restricted to the <#${channel.id}> channel.`);
}
break;
case 'enablemod':
commandModEnable(Discord, config, logger, message, command, args);
break;
case 'disablemod':
commandModDisable(Discord, config, logger, message, command, args);
break;
case 'examplemod':
if(enableModCommands) {
commandModExample(Discord, config, logger, message, command, args);
}
else {
message.channel.send(`That command is currently disabled. To find out more visit ${config.github_url}`);
}
break;
default:
message.channel.send(`That command is not one I know yet - but you could add it! To find out more visit ${config.github_url}`);
}
},
};
function commandHelp (Discord, config, logger, message, command, args) {
const helpMessage = `
The commands available to you are:
\`${config.prefix}ud <query>\` - Search Urban Dictionary
\`${config.prefix}help <command>\` - For help with a specific command add the command as an argument
`;
// Specify specific command help
if (args.length > 0) {
switch (args[0]) {
case 'ud':
message.channel.send(`Usage: \`${config.prefix}ud <query>\` *Example: ${config.prefix}ud yeet*`);
break;
default:
message.channel.send(`That command is not one I know yet - but you could add it! To find out more visit ${config.github_url}`);
}
}
else {
message.channel.send(helpMessage);
}
}
function commandUrbanDictionary (Discord, config, logger, message, command, args) {
if (!args.length) {
//If the command is used incorrectly without arguments
return message.channel.send(`You didn't provide any arguments, ${message.author}!\nCorrect Usage: \`${config.prefix}ud <query>\``);
}
else if (args[0] != "") {
const searchQuery = args[0];
// Send a GET request to urban dictionary including the search query
axios({
method: 'get',
url: 'http://api.urbandictionary.com/v0/define',
headers: {"Content-Type": "application/json"},
params: {"term": searchQuery}
})
.then(function(response) {
const answers = response.data.list;
if (answers.length > 0) {
try {
// Set up rich embed for the command to return the API response, add the first responses URL with an example of the usage and rating
const embed = generateEmbedMessage(Discord, logger, message, answers[0].word, trim(Discord, logger, answers[0].definition, 1024));
embed.setURL(answers[0].permalink)
.addFields(
{ name: 'Example', value: trim(Discord, logger, answers[0].example, 1024) },
{ name: 'Rating', value: `:thumbsup: ${answers[0].thumbs_up} :thumbsdown: ${answers[0].thumbs_down}` }
);
if (answers.length > 1) {
// If more than 1 definition is found, add additional fields to show the definitions of up to another 3 results
embed.addField('Other Definitions', 'Below are some that didn\t quite cut the mustard.')
for (let i = 1; i < answers.length && i < 4; i++) {
const ele = answers[i];
embed.addField(`${i}. `, `[${ele.word}](${ele.permalink})\r\n${trim(Discord, logger, ele.definition, 1024)}`)
}
}
message.channel.send(embed);
}
catch (error) {
logger.error('Failed to send embed message', error);
message.channel.send('There was a problem, sorry!');
}
}
else {
// If the API returns no results for the specified query
message.channel.send(`There are no results for *${args[0]}*`)
}
})
.catch(function (error) {
logger.error('Failed to make GET request to Urban Dictionary API', error);
});
}
}
function commandModEnable (Discord, config, logger, message, command, args) {
enableModCommands = true;
}
function commandModDisable (Discord, config, logger, message, command, args) {
enableModCommands = false;
}
function commandModExample (Discord, config, logger, message, command, args) {
// This would be a Mod command that can be enabled and disabled using the enableModCommands variable
}
async function sendDailyDilbert(client) {
let comicImageUrl;
// Scrape today's dilbert comic from the website
try {
const dilbertUrl = 'https://dilbert.com/';
// Need to match the first instance of class img-comic and return the src property, which is the comic image
const response = await got(dilbertUrl);
const $ = cheerio.load(response.body);
comicImageUrl = $('.img-comic:first').attr('src');
} catch (error) {
logger.error('Failed to get the dilbert comic image url', error);
}
if (comicImageUrl.length > 0) {
// Find the channel and send the comic
try {
const channel = client.channels.cache.find((channel) => channel.name === 'daily-dilbert');
channel.send(comicImageUrl);
} catch (error) {
logger.error('Failed to find the daily dilbert channel and send a message', error);
}
}
}