This repository has been archived by the owner on Jan 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathms.js
323 lines (298 loc) · 12.3 KB
/
ms.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
'use strict';
const fs = require('fs')
const Discord = require('discord.js')
const settings = JSON.parse(fs.readFileSync(`config/settings.json`, "utf8"))
//SETTINGS// -- EDIT THEM IN config/settings.json
const prefix = settings.prefix
//prefix ingame and for the bot
let dir = settings.dir
//the directory (folder) for the data base DO NOT PUT A SLASH AT THE END OF THE STRING
const webhook = eval(settings.webhook)
//VIDEO ON HOW TO CHANGE WEB HOOK SETTINGS - https://www.youtube.com/watch?v=hcWoDfsGpq8&ab_channel=CedrickAlegroso
const bottoken = settings.bottoken
//the token for your discord bot
const channelid = settings.channelid
//the channel id of the channel the webhook is in
const port = settings.port
//the port that you connect to
const color = settings.color
//the colors for things (like embeds)
const color2 = settings.color2
//the color for discord messages in game
const chatremove = settings.chatremove
//how long (im ms) till a persons spamming strikes get set to 0
const maxstrikes = settings.maxstrikes
//how many spamming strikes someone can get before they get muted
const mutetime = settings.mutetime //10000 = 10 seconds
//how long a person is muted for (in ms)
const addedtime = settings.addedtime
//the added muted time each time a person gets muted
const automessagetime = settings.automessagetime
//how often the automatic messages are sent, this can be editted in config/messages.json
////////////
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
fs.writeFile(`${dir}/db.json`, "{}", function (err) {
if (err) return console.log(err);
});
fs.writeFile(`${dir}/usedcode.json`, "{}", function (err) {
if (err) return console.log(err);
});
fs.writeFile(`${dir}/cooldown.json`, "{}", function (err) {
if (err) return console.log(err);
});
}
if (!fs.existsSync("config")){
fs.mkdirSync("config");
fs.writeFile(`config/codes.json`, "{}", function (err) {
if (err) return console.log(err);
});
fs.writeFile(`config/warps.json`, "{}", function (err) {
if (err) return console.log(err);
});
fs.writeFile(`config/redeem.json`, "{}", function (err) {
if (err) return console.log(err);
});
fs.writeFile(`config/messages.json`, '{"messages":["Make sure to followe the rules", "Donate to support the server", "Custom commands made by fluffy hyena#3238"]}', function (err) {
if (err) return console.log(err);
});
}
if (!fs.existsSync("functions")){
fs.mkdirSync("functions");
}
const db = JSON.parse(fs.readFileSync(`${dir}/db.json`, "utf8"))
const automsgs = Object.keys(JSON.parse(fs.readFileSync(`config/messages.json`, "utf8"))).map((key) => [key, JSON.parse(fs.readFileSync(`config/messages.json`, "utf8"))[key]])
const chatted = new Set();
const muted = new Set();
let messagenum = 0;
const WebSocket = require('ws')
const uuid = require('uuid');
const client = new Discord.Client()
client.login(bottoken)
client.on("ready", () => {
client.user.setPresence({
activity: { name: " Minecraft" },
status: "idle",
});
});
console.log(`Ready. In Minecraft, type /connect localhost:${port}`)
const wss = new WebSocket.Server({ port: `${port}` })
wss.on('connection', socket => {
console.log('Connected')
const sendQueue = []
const awaitedQueue = {}
/*ALL EVENTS
AgentCommand AgentCreated BlockBrocken BlockPlaced BossKilled EntitySpawned ItemAcquired ItemCrafted ItemDestroyed ItemDropped ItemUsed MobInteracted
MobKilled PlayerDied PlayerJoin PlayerLeave PlayerMessage PlayerTeleported PlayerTransform PlayerTravelled RawEvent SlashCommandExecuted
*/
let eventarray = ["PlayerMessage"]
for(let i = 0; eventarray.length > i; i++){
socket.send(JSON.stringify({
"header": {
"version": 1,
"requestId": uuid.v4(),
"messageType": "commandRequest",
"messagePurpose": "subscribe"
},
"body": {
"eventName": eventarray[i]
},
}))
console.log(`[EVENTS] Loaded ${eventarray[i]} event`)
}
socket.send(JSON.stringify({
"header": {
"version": 1,
"requestId": uuid.v4(),
"messageType": "commandRequest",
"messagePurpose": "unsubscribe"
},
"body": {
"eventName": "SlashCommandExecuted"
},
}))
//stuff for auto msgs
setInterval(()=>{
send(`tellraw @a {"rawtext":[{"text":"${automsgs[0][1][messagenum]}"}]}`)
messagenum++
if(messagenum == automsgs[0][1].length){
messagenum = 0
}
}, automessagetime)
//rest of the code that isnt auto msgs
socket.on('message', packet => {
const msg = JSON.parse(packet)
if(!msg.body.eventName == undefined) console.log(`[EVENTS] ${msg.body.eventName} event fired`)
switch(msg.body.eventName){
case 'PlayerMessage':
if(msg.body.properties.Sender == "External") return;
if(!db[msg.body.properties.Sender]){
db[msg.body.properties.Sender] = {"strikes":0,"addedtime":0}
}
if(msg.body.properties.Sender == "External"){return;}
if(!db[msg.body.properties.Sender].muted) db[msg.body.properties.Sender].muted = "false"
fs.writeFile(`${dir}/db.json`, JSON.stringify(db), (err) => {if (err) console.log(err)})
if (muted.has(msg.body.properties.Sender)) {
if(db[msg.body.properties.Sender].muted == "false"){
send(`ability "${msg.body.properties.Sender}" mute true`)
send(`tellraw "${msg.body.properties.Sender}" {"rawtext":[{"text":"§cYou have been muted automatically for spamming"}]}`)
db[msg.body.properties.Sender].muted = "true"
fs.writeFile(`${dir}/db.json`, JSON.stringify(db), (err) => {if (err) console.log(err)})
}
setTimeout(() => {
if(db[msg.body.properties.Sender].muted == "true"){
muted.delete(msg.body.properties.Sender);
send(`ability "${msg.body.properties.Sender}" mute false`)
send(`tellraw "${msg.body.properties.Sender}" {"rawtext":[{"text":"§gYou have been unmuted"}]}`)
db[msg.body.properties.Sender].addedtime = db[msg.body.properties.Sender].addedtime + addedtime
db[msg.body.properties.Sender].muted = "false"
fs.writeFile(`${dir}/db.json`, JSON.stringify(db), (err) => {if (err) console.log(err)})
}
}, mutetime+db[msg.body.properties.Sender].addedtime)
}
if (chatted.has(msg.body.properties.Sender)) {
db[msg.body.properties.Sender].strikes += 1
fs.writeFile(`${dir}/db.json`, JSON.stringify(db), (err) => {if (err) console.log(err)})
if(db[msg.body.properties.Sender].strikes >= maxstrikes - 1){
muted.add(msg.body.properties.Sender);
}
} else {
db[msg.body.properties.Sender].strikes = 0
fs.writeFile(`${dir}/db.json`, JSON.stringify(db), (err) => {if (err) console.log(err)})
chatted.add(msg.body.properties.Sender);
setTimeout(() => {
chatted.delete(msg.body.properties.Sender);
}, chatremove);
}
webhook.send(`${msg.body.properties.Message.replace(/@|(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)/g, '~~removed~~')}`, {username: `${msg.body.properties.Sender}`,});
if(!msg.body.properties.Message.startsWith(prefix)){return;}
let args = msg.body.properties.Message.slice(prefix.length).split(/ +/);
let command = args.shift().toLowerCase();
//used in the commands to see the gamemode (permission level) and if its enabled or not
function permcheck(){
let name = []
let permission = []
let enabled = []
permission.push(client.commands.map(plugins => plugins.permission));
name.push(client.commands.map(plugins => plugins.name));
enabled.push(client.commands.map(plugins => plugins.enabled));
for(let i = 0; i < name[0].length; i++){
if(enabled[0][i] == false){
send(`tellraw "${msg.body.properties.Sender}" {"rawtext":[{"text":"§cThis command is disabled"}]}`)
return "stop";
}
if(msg.body.properties.PlayerGameMode == 0 && command == name[0][i] && permission[0][i] == "creative"){
send(`tellraw "${msg.body.properties.Sender}" {"rawtext":[{"text":"§cThis command is not available in survival"}]}`)
return "stop";
} else if(msg.body.properties.PlayerGameMode == 2 && command == name[0][i] && (permission[0][i] == "creative" || permission[0][i] == "survival")){
send(`tellraw "${msg.body.properties.Sender}" {"rawtext":[{"text":"§cThis command is not available in adventure"}]}`)
return "stop";
}
}
}
const pluginsFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
client.commands = new Discord.Collection();
for(const file of pluginsFiles){
if (fs.existsSync(`./commands/${file}`)) {
let plugins = require(`./commands/${file}`);
client.commands.set(plugins.name, plugins);
}
}
if(client.commands.get(`${command}`)){
client.commands.get(`${command}`).execute(send, fs, msg, args, command, client, settings, permcheck);
}else{
send(`tellraw "${msg.body.properties.Sender}" {"rawtext":[{"text":"§cThat command doesnt exist"}]}`)
}
}
if (msg.header.messagePurpose == 'commandResponse') {
if (msg.header.requestId in awaitedQueue) {
if (msg.body.statusCode < 0)
webhook.send((msg.body.statusMessage), {username: `COMMAND ERROR`,});
delete awaitedQueue[msg.header.requestId]
}
}
let count = Math.min(100 - Object.keys(awaitedQueue).length, sendQueue.length)
for (let i = 0; i < count; i++) {
// Each time, send the first command in sendQueue, and add it to the awaitedQueue
let command = sendQueue.shift()
socket.send(JSON.stringify(command))
awaitedQueue[command.header.requestId] = command
}
})
client.on('message', message => {
if(message.channel.id == channelid){
if(message.author.bot){return;}
if (message.channel instanceof Discord.DMChannel){return;}
let args = message.content.slice(prefix.length).split(/ +/);
let command = args.shift().toLowerCase();
if(message.content.startsWith(prefix) && message.member.hasPermission("ADMINISTRATOR")){
switch(command){
case "help": message.channel.send(embed("Custom discord commands", `${prefix}rawmsg - automatically formats a tellraw command for you, just type the text\n${prefix}cfunction - use one of the custom functions`)); break;
case "rawmsg": send(rawmsg(message.content.slice(2 + command.length))); break;
case "cfunction":
if(!args[0]){
message.reply(`What custom function are you running`)
return;
}
if (fs.existsSync(`functions/${args[0]}.txt`)){
let data = fs.readFileSync(`functions/${args[0]}.txt`, "utf8")
data = data.replace(/\r/g, "").split('\n')
for (let i = 0; i < data.length; i++){
send(`${data[i]}`)
}
message.reply(`Succesfully ran custom function '${args[0]}'`)
}else{
message.reply(`That custom function does not exist`)
}
break;
default: send(message.content.slice(prefix.length)); break;
}
return;
}
let msgcontent = message.content
if(msgcontent == ""){
msgcontent = "<image>"
}
if(message.member.roles.highest.name == "@everyone"){send(`tellraw @a {"rawtext":[{"text":"${color2}[Discord] <${message .author.username}> ${msgcontent.replace(/}|{|"/g, "").split("\\").join("")}"}]}`)}else{
send(`tellraw @a {"rawtext":[{"text":"${color2}[${message.member.roles.highest.name}] <${message.author.username}> ${msgcontent.replace(/}|{|"/g, "").split("\\").join("")}"}]}`)
}
}
})
function send(cmd) {
const msg = {
"header": {
"version": 1,
"requestId": uuid.v4(),
"messagePurpose": "commandRequest",
"messageType": "commandRequest"
},
"body": {
"version": 1,
"commandLine": cmd,
"origin": {
"type": "player"
}
}
}
sendQueue.push(msg)
let count = Math.min(100 - Object.keys(awaitedQueue).length, sendQueue.length)
for (let i = 0; i < count; i++) {
if(sendQueue.length > 0){
let command = sendQueue.shift()
socket.send(JSON.stringify(command))
awaitedQueue[command.header.requestId] = command
}
}
}
function embed(title, text, footer){
let embed = new Discord.MessageEmbed()
embed.title = title
embed.description = text
embed.footer = footer
embed.color = color
return embed;
}
})
function rawmsg(text){
return `/tellraw @a {"rawtext":[{"text":"${text}"}]}`;
}