-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
234 lines (214 loc) · 6.42 KB
/
index.js
File metadata and controls
234 lines (214 loc) · 6.42 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Require the necessary discord.js classes
import { Client, GatewayIntentBits, REST, Routes } from "discord.js";
import fetch from "node-fetch";
import Database from "@replit/database";
import keepAlive from "./server.js";
const token = process.env["token"];
const guildId = process.env["guildId"];
const clientId = process.env["clientId"];
const db = new Database();
// Create a new client instance
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});
// Arrays of words
const sadWords = [
"angry",
"crestfallen",
"dejected",
"despairing",
"depressed",
"disheartened",
"dismal",
"dispirited",
"downcast",
"gloomy",
"glum",
"heartbroken",
"hopeless",
"mournful",
"sad",
"sorrowful",
"unhappy",
"woeful",
];
const starterEncouragements = [
"Cheer up!",
"Hang in there.",
"You are a great person!",
];
// db.delete("encouragements");
// Function to get the encouragements from database or set default
db.get("encouragements").then((encouragements) => {
// console.log(encouragements);
if (!encouragements.ok || encouragements.length < 1) {
db.set("encouragements", starterEncouragements);
}
});
// Function to update encouragements
const updateEncouragements = async (encouragingMessage) => {
try {
const encouragements = await db.get("encouragements");
let encouragementsArr = encouragements.value;
// console.log(encouragements);
// console.log(encouragementsArr);
encouragementsArr.push(encouragingMessage);
// console.log(encouragementsArr);
await db.set("encouragements", encouragementsArr);
// console.log(encouragements);
} catch (error) {
console.log(error);
}
};
// Function to delete encouragements
const deleteEncouragement = async (index) => {
try {
const encouragements = await db.get("encouragements");
let encouragementsArr = encouragements.value;
// console.log(encouragements);
// console.log(encouragementsArr);
if (encouragementsArr.length > index) {
encouragementsArr.splice(index, 1);
db.set("encouragements", encouragementsArr);
}
// console.log(encouragements);
} catch (error) {
console.log(error);
}
};
// Fetch the data from "zenquotes"
function getQuote() {
try {
return fetch("https://zenquotes.io/api/random")
.then((res) => {
return res.json();
})
.then((data) => {
return data[0]["q"] + " -" + data[0]["a"];
});
} catch (error) {
console.log(error);
}
}
// Set the Slash Commands
const commands = [
{
name: "ping",
description: "Replies with Pong!",
},
{
name: "server",
description: "Replies with Server name",
},
];
// Construct and prepare an instance of the REST module
const rest = new REST({ version: "10" }).setToken(token);
// Deploy the commands
(async () => {
try {
console.log("Started refreshing application (/) commands.");
await rest.put(Routes.applicationGuildCommands(clientId, guildId), {
body: commands,
});
console.log("Successfully reloaded application (/) commands.");
} catch (error) {
console.error(error);
}
})();
// Log in to Discord with your client's token
client.on("ready", async () => {
console.log(`Logged in as ${client.user.tag}!`);
});
// A listener for the event from the slashcommand
client.on("interactionCreate", async (interaction) => {
if (interaction.commandName === "ping" && interaction.isCommand()) {
await interaction.reply("pong!!!");
} else if (interaction.commandName === "server") {
await interaction.reply(
`Server name: ${interaction.guild.name}\nTotal members: ${interaction.guild.memberCount}`,
);
}
});
// A listener for the event from the message
client.on("messageCreate", async (msg) => {
switch (msg.content) {
case "ping": // ping message
await msg.reply("pong!!!");
break;
case "$server": // $server message
await msg.reply(
`Server name: ${msg.guild.name}\nTotal members: ${msg.guild.memberCount}`,
);
break;
case "$inspire": // $inspire message
getQuote()
.then()
.then((quote) => msg.channel.send(quote));
break;
case "$list": // $list message
db.get("encouragements").then(async (encouragements) => {
encouragements.value.forEach(async (encouragement) => {
await msg.channel.send(encouragement);
});
});
break;
default:
// sad keyword included message (check respond status)
db.get("responding").then((responding) => {
if (
responding.value &&
sadWords.some((word) => msg.content.includes(word))
) {
db.get("encouragements").then((encouragements) => {
const encouragement =
encouragements.value[
Math.floor(Math.random() * encouragements.value.length)
];
msg.reply(encouragement);
});
}
});
// Start with $new message (add new encouragement)
if (msg.content.startsWith("$new")) {
let encouragingMessage = msg.content.split("$new ")[1];
updateEncouragements(encouragingMessage);
await msg.channel.send("New encouraging message added.");
}
// Start with $del message (delete encouragement)
else if (msg.content.startsWith("$del")) {
let index = parseInt(msg.content.split("$del ")[1]);
deleteEncouragement(index - 1);
await msg.channel.send(
`the encouraging message number ${index} deleted.`,
);
}
break;
}
// Responding to sad words status management
if (msg.content.startsWith("$responding")) {
let value = msg.content.split("$responding ")[1];
if (value.toLowerCase() === "true") {
db.set("responding", true);
await msg.channel.send("Responding is on.");
} else if (value.toLowerCase() === "false") {
db.set("responding", false);
await msg.channel.send("Responding is off.");
} else if (value.toLowerCase() === "check") {
const status = await db.get("responding");
console.log(await db.get("responding"));
console.log(status.value);
await msg.channel.send("Responding is " + status.value);
console.log(db.get("responding"));
} else {
await msg.channel.send("Please enter true or false.");
}
}
});
// Start the server
keepAlive();
// Log in to Discord with client's token
client.login(token);