forked from xAlex-B/alexs-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.js
More file actions
193 lines (156 loc) · 5.09 KB
/
Copy pathhandler.js
File metadata and controls
193 lines (156 loc) · 5.09 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
const fs=require("fs");
var Command = require( "./Classes/Command.js" );
class Handler {
constructor( bot, options ) {
this.bot=bot;
this.dPrefix = options.dPrefix;
this.obPrefix = options.obPrefix;
this.gPrefixes = options.guildPrefixes;
this.cmdusage = 0;
if ( !this.gPrefixes )
this.gPrefixes = {};
this.commands={};
bot.on("messageCreate",(msg)=>{
if ( msg.author.bot ) return;
if ( !msg.guild.IsPrivate() && !msg.channel.permissionsOf( bot.user.id ).json.sendMessages ) return;
var pr = this.dPrefix;
if ( msg.guild.id in this.gPrefixes )
pr = this.gPrefixes[ msg.guild.id ];
if ( !msg.content.startsWith( pr ) && !msg.content.startsWith( `${bot.user.mention}` ) ) return;
var rawargs = msg.content.split( " " );
var args = msg.content.replace( rawargs[0], "" ).match( /"(?:\\"|\\\\|[^"])*"|\S+/gm );
args = args || []; // FUCK YOU // thanks for the shortcut randon
if ( rawargs[0] == `${bot.user.mention}` ) {
rawargs[0] = `${bot.user.mention} ${args[0]}`;
delete rawargs[1];
for ( let i in rawargs ) {
if ( i != 0 ) {
rawargs[i-1] = rawargs[i];
delete rawargs[i];
}
}
}
for (let cmd in this.commands) {
cmd=this.commands[cmd];
if (typeof(cmd.payload)!="function")
throw new Error(`Command payload for "${cmd.name}" isn't a function.`);
for ( let cmdname of cmd.names ) {
if ( rawargs[0] == pr + cmdname || rawargs[0] == `${bot.user.mention} ${cmdname}` ) {
if ( rawargs[1] != undefined && rawargs[1] != null ) {
for ( let sub in cmd.subcommands ) {
sub = cmd.subcommands[ sub ];
for ( let subname of sub.names ) {
if ( subname == rawargs[1] ) {
this.cmdusage += 1;
if ( bot.privateChannels.get( msg.guild.id ) ) {
console.log( `DM: ${msg.author.id}: ${msg.author.username}#${msg.author.discriminator}: ${msg.cleanContent}` );
} else {
console.log( `${msg.guild.name}: ${msg.channel.name}: ${msg.author.username}#${msg.author.discriminator}: ${msg.cleanContent}` );
}
delete args[0];
for ( let i in args ) {
if ( i != 0 ) {
args[i-1] = args[i];
delete args[i];
}
}
var qs = "";
for ( let s in args ) {
if ( s == 0 ) {
qs += args[ s ];
} else {
qs += " " + args[ s ];
}
}
var reply=sub.payload(msg,args,qs);
if (reply!=undefined&&reply!=null) {
bot.createMessage(msg.channel.id,reply).then( m=> {
console.log( "Responded: " + m.cleanContent );
} );
}
return;
}
}
}
}
this.cmdusage += 1;
if ( bot.privateChannels.get( msg.guild.id ) ) {
console.log( `DM: ${msg.author.id}: ${msg.author.username}#${msg.author.discriminator}: ${msg.cleanContent}` );
} else {
console.log( `${msg.guild.name}: ${msg.channel.name}: ${msg.author.username}#${msg.author.discriminator}: ${msg.cleanContent}` );
}
var qs = "";
/*for ( let i in args ) {
if ( i != 0 ) {
args[i-1] = args[i];
delete args[i];
}
}*/
for ( let i in args ) {
console.log( i + ": " + args[ i ] );
}
for ( let s in args ) {
if ( s == 0 ) {
qs += args[ s ];
} else {
qs += " " + args[ s ];
}
}
var reply=cmd.payload(msg,args,qs);
if (reply!=undefined&&reply!=null) {
bot.createMessage(msg.channel.id,reply).then( m=>{
console.log( "Responded: " + m.cleanContent );
} );
}
}
}
}
});
}
register( name, payload, options ) {
if (!name) throw new Error("No name for command.");
if (!payload) throw new Error(`No payload for command "${name}".`);
if (typeof(payload)!="function") throw new Error(`Command payload for "${name}" isn't a function.`);
return this.commands[name] = new Command( name, payload, options );
}
unregister(name){
if (!name) throw new Error("No name for command.");
if (!this.commands[name]) throw new Error(`Command "${name}" doesn't exist.`);
delete this.commands[name];
}
loadcommands(){
var files=fs.readdirSync("./cmds");
for (let file of files){
require(`../cmds/${file}`);
}
}
reloadcommands(){
var files=fs.readdirSync("./cmds");
for (let file of files){
delete require.cache[ require.resolve( `../cmds/${file}` ) ];
require(`../cmds/${file}`);
}
}
setDefaultPrefix( prefix ) {
this.dPrefix = prefix;
return this.prefix;
}
unsetDefaultPrefix() {
this.dPrefix = undefined;
}
setObligatoryPrefix( prefix ) {
this.obPrefix = prefix;
return this.obPrefix;
}
unsetObligatoryPrefix() {
this.obPrefix = undefined;
}
setGuildPrefix( guildId, prefix ) {
this.gPrefixes[ guildId ] = prefix;
}
unsetGuildPrefix( guildId ) {
if ( guildId in this.gPrefixes )
delete this.gPrefixes[ guildId ];
}
}
module.exports=Handler;