nodevk-ts
Version:
Simple Node.js module that allows you to interact with the VKontakte API.
120 lines (119 loc) • 4.82 kB
JavaScript
import API, { InvokeMethodException } from "./api.js";
import NodeVK from "./../NodeVK.js";
var GroupClosed;
(function (GroupClosed) {
GroupClosed[GroupClosed["Open"] = 0] = "Open";
GroupClosed[GroupClosed["Closed"] = 1] = "Closed";
GroupClosed[GroupClosed["Private"] = 2] = "Private";
})(GroupClosed || (GroupClosed = {}));
export default class MessagesAPI extends API {
constructor() {
super(...arguments);
this.api_name = "messages";
}
async send(peers_id, message, attachments, params = {}) {
let method = this.api_name + ".send";
if (!this.checkValid("group", "user"))
throw new InvokeMethodException(method, this.type);
if (message)
params.message = message;
if (attachments)
params.attachment = attachments;
if (params.random_id == null)
params.random_id = 0;
if (params.intent == null)
params.intent = "default";
if (Array.isArray(peers_id)) {
if (!this.checkValid("group"))
throw new InvokeMethodException(method, this.type, 'with param "peer_ids"');
params.peer_ids = peers_id;
}
else if (typeof peers_id == "string") {
params.domain = peers_id;
}
else {
params.peer_id = peers_id;
}
if (typeof params.forward_messages == "number")
params.forward_messages = [params.forward_messages];
return await this.call(method, params);
}
async edit(peer_id, message_id, message, attachments, params = {}) {
let method = this.api_name + ".edit";
if (!this.checkValid("group", "user"))
throw new InvokeMethodException(method, this.type);
if (NodeVK.isChat(peer_id)) {
params.conversation_message_id = message_id;
}
else {
params.message_id = message_id;
}
if (message)
params.message = message;
if (attachments)
params.attachment = attachments;
return await this.call(method, params);
}
async delete(message_id, spam = false, delete_for_all = false, group_id) {
let method = this.api_name + ".delete";
if (!this.checkValid("group", "user"))
throw new InvokeMethodException(method, this.type);
if (typeof message_id == "number")
message_id = [message_id];
let params = {
message_ids: message_id,
spam: spam,
delete_for_all: delete_for_all
};
if (group_id)
params.group_id = group_id;
return await this.call(method, params);
}
async getConversationMembers(peer_id, params = {}) {
let method = this.api_name + ".getConversationMembers";
if (!this.checkValid("group", "user"))
throw new InvokeMethodException(method, this.type);
params.peer_id = peer_id;
return await this.call(method, params);
}
async removeChatUser(chat_id, member_id) {
let method = this.api_name + ".removeChatUser";
if (!this.checkValid("group", "user"))
throw new InvokeMethodException(method, this.type);
const params = {};
params.chat_id = NodeVK.getChatID(chat_id);
params.member_id = member_id;
return await this.call(method, params);
}
async removeChatUserFromPeer(peer_id, member_id) {
let method = this.api_name + ".removeChatUser";
if (!this.checkValid("group", "user"))
throw new InvokeMethodException(method, this.type);
if (!NodeVK.isChat(peer_id))
throw new RangeError("Isn't chat ID.");
const params = {};
params.chat_id = NodeVK.getChatID(peer_id);
params.member_id = member_id;
return await this.call(method, params);
}
async editChat(chat_id, title) {
let method = this.api_name + ".editChat";
if (!this.checkValid("group", "user"))
throw new InvokeMethodException(method, this.type);
const params = {};
params.chat_id = NodeVK.getChatID(chat_id);
params.title = title;
return await this.call(method, params);
}
async editChatFromPeer(peer_id, title) {
let method = this.api_name + ".editChat";
if (!this.checkValid("group", "user"))
throw new InvokeMethodException(method, this.type);
if (!NodeVK.isChat(peer_id))
throw new RangeError("Isn't chat ID.");
const params = {};
params.chat_id = NodeVK.getChatID(peer_id);
params.title = title;
return await this.call(method, params);
}
}