-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
57 lines (51 loc) · 2.2 KB
/
index.js
File metadata and controls
57 lines (51 loc) · 2.2 KB
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
// Extract fetch from the module
const fetch = require("node-fetch")
const TOKEN = require('config.json').TOKEN;
const prefix = "\\"
// Extract the required classes from the discord.js module
const { Client, MessageEmbed } = require('discord.js');
// Create an instance of a Discord client
const client = new Client();
/**
* The ready event is vital, it means that only _after_ this will your bot start reacting to information
* received from Discord
*/
client.on('ready', () => {
console.log('I am ready!');
});
client.on('message', async message => {
// If the message starts with "t "
if (message.content.startsWith(`${prefix}`)) {
// Delete the message
message.delete()
// Remove the ", " from the text
let word = message.content.replace(`${prefix}`, "")
// Extract info from the word
let wordQuestion = word.split(" | ")[0]
let wordIndex = word.split(" | ")[1] - 1
// Search for the meaning of the ${word}
const search = await fetch(`https://api.urbandictionary.com/v0/define?term=${wordQuestion}`)
.then(meaning => meaning.json())
// Take the definition from the search
let definition = search.list[wordIndex?wordIndex:0].definition
// Take the example from the search
let example = search.list[wordIndex?wordIndex:0].example
// We can create embeds using the MessageEmbed constructor
const embed = new MessageEmbed()
// Set the author of the field
.setAuthor(message.author.username, message.author.avatarURL())
// Set the title of the fieldS
.setTitle(wordQuestion)
// Set the color of the embed
.setColor(0xf1c40f)
// Set the main content of the embed
.setDescription(definition?
`${definition}\n${example?
`\n**Example**\n${example}`:""}`:
`No definitions could be found for: ${wordQuestion}`);
// Send the embed to the same channel as the message
message.channel.send(embed);
}
});
// Log our bot in using the token from https://discord.com/developers/applications
client.login(TOKEN);