-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand_create.js
180 lines (156 loc) · 7.14 KB
/
command_create.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
module.exports = {
"Command": class {
constructor(name, properties, callback) {
this.name = name;
this.props = Object.assign({
"min_args": 0,
"max_args": Infinity,
"permissions": [],
"channels": ["text", "group", "dm"],
"help": "Использование: `.%{name}%{args}`; каналы: %{channels}; права: %{rights}; альтернативные названия: %{alters}",
"alters": []
}, properties);
this.callback = callback;
}
isCommand(str) {
var alters = this.props.alters;
for (let v in alters)
if (str.toLowerCase().startsWith("." + v))
return true;
return str.toLowerCase().startsWith("." + this.name);
}
getArgs(content) {
let arr = content.split(' ');
arr.shift();
return arr;
}
error(channel, er) {
channel.sendEmbed({
"title": "Ошибка команды",
"color": 15732486,
"description": "**Описание ошибки**: " + er + '\n**Команда**: ' + this.handleHelp()
});
}
execute(msg, client) {
let content = msg.content;
if (!this.isCommand(content))
return;
let author = msg.author;
let channel = msg.channel;
let member = channel.type === 'text' ? msg.member : null;
let args = this.getArgs(content);
if (!this.props.channels.includes(channel.type))
return this.error(channel, "Команда не предназначена для этого типа канала");
if (args.length < this.props.min_args || args.length > this.props.max_args)
return this.error(channel, "Неверное количество аргументов");
if (typeof this.props.permissions[0] !== 'undefined' && !member.hasPermissions(this.props.permissions, false))
return this.error(channel, "Недостаточно прав для выполнения этой команды");
return this.callback({
channel: channel,
args: args,
author: msg.author,
content: content,
member: member,
client: client,
self: this,
msg: msg,
guild: msg.guild
});
}
handleHelp() {
const Channels = {
"dm": "Личные сообщения",
"text": "Текстовые каналы",
"group": "Групповые сообщения"
};
const Rights = {
"CREATE_INSTANT_INVITE": "Создание приглашений",
"KICK_MEMBERS": "Кик",
"BAN_MEMBERS": "Бан",
"ADMINISTRATOR": "Администратор",
"MANAGE_CHANNELS": "Управление каналами",
"MANAGE_GUILD": "Управление сервером",
"ADD_REACTIONS": "Добавление реакций",
"READ_MESSAGES": "Чтение сообщений",
"SEND_MESSAGES": "Отправка сообщений",
"SEND_TTS_MESSAGES": "Отправка tts-сообщений",
"MANAGE_MESSAGES": "Управление сообщениями",
"EMBED_LINKS": "Встравиание ссылок",
"ATTACH_FILES": "Прикрепление файлов",
"READ_MESSAGE_HISTORY": "Чтение истории сообщений",
"MENTION_EVERYONE": "Упомянуть всех",
"EXTERNAL_EMOJIS": "Использование внешних Эмодзи",
"CONNECT": "Присоединение к голосовому чату",
"SPEAK": "Говорить в голосовом чате",
"MUTE_MEMBERS": "Отключить голос участникам",
"DEAFEN_MEMBERS": "Отключить звук участникам",
"MOVE_MEMBERS": "Пемещение участников в голосовом чате",
"USE_VAD": "Режим по активации голосом",
"CHANGE_NICKNAME": "Изменение ника",
"MANAGE_NICKNAMES": "Изменение никнейма у других",
"MANAGE_ROLES_OR_PERMISSIONS": "Управление правами",
"MANAGE_WEBHOOKS": "Управлление Webhook'ами",
"MANAGE_EMOJIS": "Управление Эмодзи"
}
let help = this.props.help;
if (help.search(/\%\{name\}/) !== -1)
help = help.replace("%{name}", this.name);
if (help.search(/\%\{args\}/) !== -1) {
let args = ""
for (let i = 1; i <= this.props.min_args; i++) {
args += " <обязательный аргумент" + i + ">";
}
if (this.props.max_args > this.props.min_args)
args += " [необязательные аргументы]";
help = help.replace("%{args}", args);
}
if (help.search(/\%\{channels\}/) !== -1) {
let channels = [];
this.props.channels.forEach(type => {
channels.push(Channels[type]);
});
help = help.replace("%{channels}", channels.join(', '));
}
if (help.search(/\%\{rights\}/) !== -1) {
let rights = [];
this.props.permissions.forEach(right => {
rights.push(Rights[right]);
});
help = help.replace("%{rights}", rights.join(', '));
}
if (help.search(/\%\{alters\}/) !== -1)
help = help.replace("%{alters}", this.props.alters.join(", "));
return help;
}
},
"Commands": class {
constructor() {
this.commands = [];
this.help = {};
this.alters = [];
this.commandnames = [];
}
execute(msg, client) {
this.commands.forEach(c => {
c.execute(msg, client);
});
}
handleHelp() {
var help = [];
this.commandnames.forEach(el => {
if (typeof this.alters[el] === 'undefined')
help.push(this.help[el]);
});
return help.join(';\n');
}
push(command) {
this.commands.push(command);
this.commandnames.push(command.name);
this.help[command.name] = command.handleHelp()
command.props.alters.forEach(al => {
this.help[al] = command.handleHelp();
this.alters.push()
});
}
}
};