-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
109 lines (89 loc) · 2.68 KB
/
index.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
const Discord = require("discord.js")
const fetch = require("node-fetch")
const keepAlive = require("./server")
const Database = require("@replit/database")
const db = new Database()
const client = new Discord.Client()
const sadWords = ["sad", "depressed", "unhappy", "angry"]
const starterEncouragements = [
"Cheer up!",
"Hang in there.",
"You are a great person / bot!"
]
db.get("encouragements").then(encouragements => {
if (!encouragements || encouragements.length < 1) {
db.set("encouragements", starterEncouragements)
}
})
db.get("responding").then(value => {
if (value == null) {
db.set("responding", true)
}
})
function updateEncouragements(encouragingMessage) {
db.get("encouragements").then(encouragements => {
encouragements.push([encouragingMessage])
db.set("encouragements", encouragements)
})
}
function deleteEncouragement(index) {
db.get("encouragements").then(encouragements => {
if (encouragements.length > index) {
encouragements.splice(index, 1)
db.set("encouragements", encouragements)
}
})
}
function getQuote() {
return fetch("https://zenquotes.io/api/random")
.then(res => {
return res.json()
})
.then(data => {
return data[0]["q"] + " -" + data[0]["a"]
})
}
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`)
})
client.on("message", msg => {
if (msg.author.bot) return
if (msg.content === "$inspire") {
getQuote().then(quote => msg.channel.send(quote))
}
db.get("responding").then(responding =>{
if (responding && sadWords.some(word => msg.content.includes(word))) {
db.get("encouragements").then(encouragements => {
const encouragement = encouragements[Math.floor(Math.random() * encouragements.length)]
msg.reply(encouragement)
})
}
})
if (msg.content.startsWith("$new")) {
encouragingMessage = msg.content.split("$new ")[1]
updateEncouragements(encouragingMessage)
msg.channel.send("New encouraging message added.")
}
if (msg.content.startsWith("$del")) {
index = parseInt(msg.content.split("$del ")[1])
deleteEncouragement(index)
msg.channel.send("Encouraging message deleted.")
}
if (msg.content.startsWith("$list")) {
db.get("encouragements").then(encouragements => {
msg.channel.send(encouragements)
})
}
if (msg.content.startsWith("$responding")) {
value = msg.content.split("$responding ")[1]
if (value.toLowerCase() == "true") {
db.set("responding", true)
msg.channel.send("Responding is on.")
} else {
db.set("responding", false)
msg.channel.send("Responding is off.")
}
}
})
keepAlive()
client.login(process.env.TOKEN)