-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
150 lines (144 loc) · 6.63 KB
/
index.js
File metadata and controls
150 lines (144 loc) · 6.63 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
var discord = require('discord.js');
var fs = require('fs');
var client = new discord.Client();
var commands = [];
var env = fs.readFileSync('./token.json.env', 'utf-8');
env = JSON.parse(env);
function refreshCommands() {
//Read all names of the files in the ./commands/ foloder
let out = fs.readdirSync('./commands/');
commands = [];
for (i in out) {
if (out[i].match(/.+\.js/)) { //if file has a .js extension
let x = require('./commands/' + out[i]); //require its command data
if (x.name && x.main && x.regex) { // if file has all required data
commands.push(x); //Add it to command list
}
}
}
return commands; //debug: returns new command list
}
client.on('ready', () => {
refreshCommands();
console.log('Starting...');
console.log('Currently detected commands:');
client.user.setPresence({
game: {
name: 'for /syntax',
type: 'WATCHING'
},
status: 'online'
})
for (i in commands) {
console.log(' /' + commands[i].main);
}
});
client.on('message', message => {
if (!message.author.bot) {
syntaxCommand(message)
}
});
/**
* Parses a Discord message for the /syntax command.
* @param {discord.Message} message
*/
function syntaxCommand(message) {
if (message.content.match(/^\/syntax.*/)) { //if message is the /syntax command
if (message.content.match(/^\/syntax$/)) { //if /syntax command is by itself
let embed = { //intial info embed
title: 'Minecraft Command Syntax Bot',
footer: {
text: 'Made by Sulfrix#7440. Say hi to him!',
icon_url: 'https://sulfrix.github.io/Newavatar.png'
},
description: 'Checks if a MC command has the correct syntax. **Work in progress!**',
fields: [{
name: 'Usage:',
value: '```/syntax [MC Command without /]```'
}]
};
message.channel.send('Hello!', {
embed
});
}
if (message.content.match(/^\/syntax .+/)) { //if syntax command has more arguments
let checkCommand = message.content.match(/(?<=^\/syntax ).+/)[0] //sets a string var to message content after /syntax command
let x
for (i in commands) { //loop through all available commands
if (checkCommand.match(commands[i].regex)) { //if command matches with indexed command data
x = checkCommand.match(commands[i].regex)[0] //set it to the match data
if (x == checkCommand) { //if match and inital command are equal, user puts in correct command
let embed = { //Embed if user is correct on syntax, no difference from regex
"author": {
"name": "Minecraft Command Syntax Bot",
"icon_url": "https://sulfrix.github.io/syntaxBotIcon.png"
},
"title": "Syntax Result",
"footer": {
"text": "Made by Sulfrix#7440. Say hi to him!",
"icon_url": "https://sulfrix.github.io/Newavatar.png"
},
"description": "**This is correct syntax!**",
"fields": [{
"name": "Command:",
"value": `\`\`\`${x}\`\`\``
}]
};
message.channel.send("I checked the syntax of that command...", {
embed
});
break
} else {
let embed = { //Embed for if user is slightly off with their command (incorrect syntax, regex still detects it)
"author": {
"name": "Minecraft Command Syntax Bot",
"icon_url": "https://sulfrix.github.io/syntaxBotIcon.png"
},
"title": "Syntax Result",
"footer": {
"text": "Made by Sulfrix#7440. Say hi to him!",
"icon_url": "https://sulfrix.github.io/Newavatar.png"
},
"description": "The syntax is a little off, but I still detected it!",
"fields": [{
"name": "Your Command:",
"value": `\`\`\`${checkCommand}\`\`\``
},
{
"name": "Correct Command:",
"value": `\`\`\`${x}\`\`\``
}
]
};
message.channel.send("I checked the syntax of that command...", {
embed
});
break
}
}
}
if (!x) { //if no correct command was found
let embed = { //embed for no command found
"author": {
"name": "Minecraft Command Syntax Bot",
"icon_url": "https://sulfrix.github.io/syntaxBotIcon.png"
},
"title": "Syntax Result",
"footer": {
"text": "Made by Sulfrix#7440. Say hi to him!",
"icon_url": "https://sulfrix.github.io/Newavatar.png"
},
"description": "I couldn't detect this command. Check spaces, player names, etc.",
"fields": [{
"name": "Your Command:",
"value": `\`\`\`${checkCommand}\`\`\``
}]
};
message.channel.send("What?", {
embed
});
}
}
}
}
client.login(env.token); //start the bot