-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
234 lines (189 loc) · 6.92 KB
/
server.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
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
// set up ======================================================================
// get all the tools we need
const fs = require("fs");
const Discord = require("discord.js");
const Players = require("./app/modules/players.js");
const LABYRINTH = require("./app/modules/LABYRINTH.js");
const { prefix, token } = require("./config/config.json");
// configuration ===============================================================
const client = new Discord.Client();
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync("./commands");
const cooldowns = new Discord.Collection();
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
// set a new item in the Collection
// with the key as the command name and the value as the exported module
client.commands.set(command.name, command);
}
const players = new Players();
async function play(usr, mods) {
try {
//and the game begins
let player = players.get(usr.id);
let pos = player.position;
let location = LABYRINTH[pos[0]][pos[1]];
//jumpscares
if (location.hasOwnProperty("scare")) {
if (player.scares.indexOf(location.scare.name) === -1) {
let response = await usr.send(location.scare.text, {
files: [
{ attachment: "./assets/img/dungeon/jumpscares/" + location.scare.img, name: location.scare.text }
]
});
player.scared(location.scare.name);
await response.delete(3500);
}
}
let actions = "";
//lookaround
actions += " - `lookaround`\n";
//move
if (location.hasOwnProperty("directions")) {
location.directions.forEach(d => {
//if it is not locked
if (typeof d.locked === "undefined") {
actions += " - `move " + d.name + "`\n";
}
//if it is locked we check if player can proceed
else {
if (d.locked === "equipment") {
if (d.requires.every(elem => player.equipment.indexOf(elem) > -1)) {
//all items aquired, player allowed to proceed
actions += " - `move " + d.name + "`\n";
} //otherwise player does not have all nesessary items
}
if (d.locked === "action") {
if (d.requires.every(elem => player.actions.indexOf(elem) > -1)) {
//all actions done, player allowed to proceed
actions += " - `move " + d.name + "`\n";
} //otherwise player did not go through all nesessary actions
}
}
});
}
//interact
if (location.hasOwnProperty("interactions")) {
location.interactions.forEach(i => {
actions += " - `interact " + i.item + "`\n";
});
}
//use
actions += " - `use [item name]`\n";
//status
actions += " - `status`\n";
let image;
if (location.hasOwnProperty("img")) {
image = "./assets/img/dungeon/" + location.img;
}
let embed = {
"title": "⚔ Labyrinth",
"description": location.description,
"color": 16751104,
"footer": {
"text": "Sometimes for an unknown reason Discord delays sending messages (even though the server has done all the needed calculations for the game). Please, be patient if a new location screen is loading for too long. Thank you!"
},
"image": {
"url": "attachment://image.jpg"
},
"fields": [
{
"name": "🥖 Available actions",
"value": actions
}
]
};
if (typeof mods === "object") {
mods.forEach(m => {
embed.fields.unshift(m);
});
}
client.users.get(usr.id).send({
embed,
files: [
{ attachment: image, name: "image.jpg" }
]
});
} catch(e) { console.error(e); }
}
// launch ======================================================================
client.on("ready", () => {
//client.user.setUsername('Ancient Guardian');
//client.user.setAvatar('./assets/img/botavatars/shadow.jpg');
console.log(`Logged in as ${client.user.tag}!`);
});
client.on("message", message=> {
(async function() {
if (message.author.bot === true) return;
//if (message.channel.id !== "499278926959345669") return;
if (message.isMentioned(client.user)) {
let player = players.get(message.author.id);
if (player === undefined || player.length == 0) {
//tell everyone that someone's accepted the challenge
message.channel.send(`${message.author.username} feels something is happening... The body... It is dissapearing. You don't see your arms anymore. Everything happened so fast, you couldn't even make a sound. ${message.author.username} just was here, not anymore...`);
//active_channels.forEach((el) => {
// client.channels.get(el).send('Someone accepted the challenge. The price of not finishing it is players soul. Be careful in your decisions, there is no way back.');
//});
//add player to the game
player = players.add(message.author.id);
play(message.author);
} else {
message.author.send("Your game is already running. You can continue it anytime!");
}
}
})();
});
client.on("message", message => {
//If the message either doesn't start with the prefix or was sent by a bot, exit early.
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const commandName = args.shift().toLowerCase();
const command = client.commands.get(commandName)
|| client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
if (!command) return;
if (command.guildOnly && message.channel.type !== "text") {
return message.reply("I can't execute that command inside DMs!");
} else if (command.dmOnly && message.channel.type !== "dm") {
return message.reply("This command is only available in DMs!");
}
if (command.args && !args.length) {
let reply = `You didn't provide any arguments, ${message.author}!`;
if (command.usage) {
reply += `\nThe proper usage would be: \`${prefix}${command.name} ${command.usage}\``;
}
return message.channel.send(reply);
}
if (!cooldowns.has(command.name)) {
cooldowns.set(command.name, new Discord.Collection());
}
//if not admin set cooldown
if(message.author.id != 115961932598476800) {
const now = Date.now();
const timestamps = cooldowns.get(command.name);
const cooldownAmount = (command.cooldown || 3) * 1000;
if (!timestamps.has(message.author.id)) {
timestamps.set(message.author.id, now);
setTimeout(() => timestamps.delete(message.author.id), cooldownAmount);
}
else {
const expirationTime = timestamps.get(message.author.id) + cooldownAmount;
if (now < expirationTime) {
const timeLeft = (expirationTime - now) / 1000;
return message.reply(`please wait ${timeLeft.toFixed(1)} more second(s) before reusing the \`${command.name}\` command.`);
}
if(message.author.id != 115961932598476800) {
timestamps.set(message.author.id, now);
setTimeout(() => timestamps.delete(message.author.id), cooldownAmount);
}
}
}
try {
let mods = command.execute(message, args, players);
play(message.author, mods);
}
catch (error) {
console.error(error);
message.reply("There was an error trying to execute that command!");
}
});
client.login(token);