qq-official-bot
Version:
694 lines (693 loc) • 22 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Bot = void 0;
exports.defineConfig = defineConfig;
exports.createBot = createBot;
const client_1 = require("./client");
const constants_1 = require("./constants");
// 导入重构后的消息系统
const message_1 = require("./message");
// 导入服务模块
const services_1 = require("./services");
class Bot extends client_1.Client {
constructor(config) {
super(config);
this.fileProcessor = new message_1.FileProcessor(this.request);
// 服务实例
this.guildService = new services_1.GuildService(this.request);
this.channelService = new services_1.ChannelService(this.request);
this.memberService = new services_1.MemberService(this.request);
this.permissionService = new services_1.PermissionService(this.request);
this.reactionService = new services_1.ReactionService(this.request);
this.scheduleService = new services_1.ScheduleService(this.request);
this.threadService = new services_1.ThreadService(this.request);
this.audioService = new services_1.AudioService(this.request);
this.botService = new services_1.BotService(this.request);
// 获取基础URL
this.messageService = new services_1.MessageService(this.request, this.config.appid);
const nodeVersion = parseInt(process.version.slice(1));
if (nodeVersion < 16) {
this.logger.warn(`你的node版本(${process.version}) <16,可能会出现不可预测的错误,请升级node版本,为确保服务正常运行,请升级node版本`);
}
process.on("uncaughtException", e => {
this.logger.debug(e.stack);
});
process.on("unhandledRejection", e => {
this.logger.debug(e instanceof Error ? e.stack : e);
});
}
get middleware() {
if (this.config.mode !== 'middleware')
throw new Error('receiver mode is not middleware');
return this.receiver.handler.middleware();
}
/**
* 获取机器人信息
*/
async getSelfInfo() {
return this.botService.getSelfInfo();
}
/**
* 获取频道角色权限信息
* @param channel_id 频道id
* @param role_id 角色id
*/
async getChannelPermissionOfRole(channel_id, role_id) {
return this.permissionService.getChannelRolePermission(channel_id, role_id);
}
/**
* 设置频道公告
* @param guild_id
* @param channel_id
* @param message_id
*/
async setChannelAnnounce(guild_id, channel_id, message_id) {
const { data: result } = await this.request.post(`/guilds/${guild_id}/announces`, {
message_id,
channel_id
});
return result;
}
/**
* 更新频道角色权限
* @param channel_id
* @param role_id
* @param permission
*/
async updateChannelPermissionOfRole(channel_id, role_id, permission) {
const result = await this.request.put(`/channels/${channel_id}/roles/${role_id}/permissions`, permission);
return result.status === 204;
}
/**
* 获取频道用户权限
* @param channel_id
* @param member_id
*/
async getChannelMemberPermission(channel_id, member_id) {
const { data: result } = await this.request.get(`/channels/${channel_id}/members/${member_id}/permissions`);
return result;
}
/**
* 更新频道用户权限
* @param channel_id
* @param member_id
* @param permission
*/
async updateChannelMemberPermission(channel_id, member_id, permission) {
const result = await this.request.put(`/channels/${channel_id}/members/${member_id}/permissions`, permission);
return result.status === 204;
}
/**
* 获取频道置顶消息id列表
* @param channel_id
*/
async getChannelPins(channel_id) {
const { data: { message_ids = [] } = {} } = await this.request.get(`/channels/${channel_id}/pins`);
return message_ids;
}
/**
* 置顶频道消息
* @param channel_id
* @param message_id
*/
async pinChannelMessage(channel_id, message_id) {
const { data: result } = await this.request.post(`/channels/${channel_id}/pins/${message_id}`);
return result;
}
/**
* 取消置顶频道消息
* @param channel_id
* @param message_id
*/
async unPinChannelMessage(channel_id, message_id) {
const result = await this.request.delete(`/channels/${channel_id}/pins/${message_id}`);
return result.status === 204;
}
/**
* 创建子频道
* @param guild_id
* @param channelInfo
*/
async createChannel(guild_id, channelInfo) {
const { data: result } = await this.request.post(`/guilds/${guild_id}/channels`, channelInfo);
return result;
}
/**
* 修改子频道
* @param channel_id
* @param updateInfo
*/
async updateChannel(channel_id, updateInfo) {
const { data: result } = await this.request.patch(`/channels/${channel_id}`, updateInfo);
return result;
}
/**
* 删除子频道
* @param channel_id
*/
async deleteChannel(channel_id) {
const result = await this.request.delete(`/channels/${channel_id}`);
return result.status === 200;
}
/**
* 获取频道角色列表
* @param guild_id
*/
async getGuildRoles(guild_id) {
const { data: { roles = [] } = {} } = await this.request.get(`/guilds/${guild_id}/roles`);
return roles;
}
/**
* 创建频道角色
* @param guild_id
* @param role
*/
async creatGuildRole(guild_id, role) {
const { data: result } = await this.request.post(`/guilds/${guild_id}/roles`, role);
return result.role;
}
/**
* 修改频道角色
* @param guild_id
* @param role_id
* @param updateInfo
*/
async updateGuildRole(guild_id, role_id, updateInfo) {
const { data: result } = await this.request.patch(`/guilds/${guild_id}/roles/${role_id}`, updateInfo);
return result.role;
}
/**
* 删除频道角色
* @param role_id
*/
async deleteGuildRole(role_id) {
const result = await this.request.delete(`/guilds/{guild_id}/roles/${role_id}`);
return result.status === 204;
}
/**
* 获取频道可访问API类别
* @param guild_id
*/
async getGuildAccessApis(guild_id) {
const { data: result } = await this.request.get(`/guilds/${guild_id}/api_permission`);
return result.apis || [];
}
/**
* 申请频道API
* @param guild_id
* @param channel_id
* @param apiInfo
* @param desc
*/
async applyGuildAccess(guild_id, channel_id, apiInfo, desc) {
const { data: result } = await this.request.post(`/guilds/${guild_id}/api_permission/demand`, {
channel_id,
api_identify: apiInfo,
desc,
});
return result;
}
/**
* 取消频道禁言
* @param guild_id
*/
async unMuteGuild(guild_id) {
return this.muteGuild(guild_id, 0, 0);
}
/**
* 频道禁言
* @param guild_id
* @param seconds
* @param end_time
*/
async muteGuild(guild_id, seconds, end_time) {
const result = await this.request.put(`/guilds/${guild_id}/mute`, {
mute_seconds: `${seconds}`,
mute_end_timestamp: `${end_time}`
});
return result.status === 204;
}
/**
* 批量取消频道成员禁言
* @param guild_id
* @param member_ids
*/
async unMuteGuildMembers(guild_id, member_ids) {
return this.muteGuildMembers(guild_id, member_ids, 0, 0);
}
/**
* 批量禁言频道成员
* @param guild_id
* @param member_ids
* @param seconds
* @param end_time
*/
async muteGuildMembers(guild_id, member_ids, seconds, end_time) {
const result = await this.request.put(`/guilds/${guild_id}/mute`, {
mute_seconds: `${seconds}`,
mute_end_timestamp: `${end_time}`,
user_ids: member_ids
});
return result.status === 200;
}
async addGuildMemberRoles(guild_id, channel_id, member_id, role_id) {
const result = await this.request.put(`/guilds/${guild_id}/members/${member_id}/roles/${role_id}`, { id: channel_id });
return result.status === 204;
}
/**
* 移除频道成员角色
* @param guild_id
* @param channel_id
* @param member_id
* @param role_id
*/
async removeGuildMemberRoles(guild_id, channel_id, member_id, role_id) {
const result = await this.request.delete(`/guilds/${guild_id}/members/${member_id}/roles/${role_id}`, { data: { id: channel_id } });
return result.status === 204;
}
/**
* 踢出频道成员
* @param guild_id
* @param member_id
* @param clean
* @param blacklist
*/
async kickGuildMember(guild_id, member_id, clean = 0, blacklist) {
const result = await this.request.delete(`/guilds/${guild_id}/members/${member_id}`, {
data: {
add_blacklist: blacklist,
delete_message_days: clean
}
});
return result.status === 204;
}
/**
* 取消频道成员禁言
* @param guild_id
* @param member_id
*/
async unMuteGuildMember(guild_id, member_id) {
return this.muteGuildMember(guild_id, member_id, 0, 0);
}
/**
* 禁言频道成员
* @param guild_id
* @param member_id
* @param seconds
* @param end_time
*/
async muteGuildMember(guild_id, member_id, seconds, end_time) {
const result = await this.request.put(`/guilds/${guild_id}/members/${member_id}/mute`, {
mute_seconds: `${seconds}`,
mute_end_timestamp: `${end_time}`
});
return result.status === 204;
}
/**
* 获取频道列表
*/
async getGuildList() {
const _getGuildList = async (after = undefined) => {
const res = await this.request.get('/users/@me/guilds', {
params: {
after
}
}).catch(() => ({ data: [] })); // 私域不支持获取频道列表,做个兼容
if (!res.data?.length)
return [];
const result = (res.data || []).map(g => {
const { id: guild_id, name: guild_name, joined_at, ...guild } = g;
return {
guild_id,
guild_name,
join_time: new Date(joined_at).getTime() / 1000,
...guild
};
});
const last = result[result.length - 1];
return [...result, ...await _getGuildList(last.guild_id)];
};
return await _getGuildList();
}
/**
* 获取频道信息
* @param guild_id
*/
async getGuildInfo(guild_id) {
const { data: { id: _, name: guild_name, joined_at, ...guild } } = await this.request.get(`/guilds/${guild_id}`);
return {
guild_id,
guild_name,
join_time: new Date(joined_at).getTime() / 1000,
...guild
};
}
/**
* 获取子频道消息
* @param channel_id {string} 子频道id
* @param message_id {string} 消息id
*/
async getGuildMessage(channel_id, message_id) {
const { data: payload } = await this.request.get(`/channels/${channel_id}/messages/${message_id}`);
return this.processPayload(payload.id, `message.guild`, payload);
}
/**
* 获取频道成员列表
* @param guild_id
*/
async getGuildMemberList(guild_id) {
const _getGuildMemberList = async (after = undefined) => {
const res = await this.request.get(`/guilds/${guild_id}/members`, {
params: {
after,
limit: 100
}
}).catch(() => ({ data: [] })); // 公域没有权限,做个兼容
if (!res.data?.length)
return [];
const result = (res.data || []).map(m => {
const { user: { id: member_id, ...member }, roles, joined_at, nick } = m;
return {
member_id,
card: nick,
roles,
...member,
join_time: new Date(joined_at).getTime() / 1000,
};
});
const last = result[result.length - 1];
return [...result, ...await _getGuildMemberList(last.member_id)];
};
return await _getGuildMemberList();
}
/**
* 获取频道成员信息
* @param guild_id
* @param member_id
*/
async getGuildMemberInfo(guild_id, member_id) {
const { data: { user: { id: _, ...member }, roles, joined_at, nick } } = await this.request.get(`/guilds/${guild_id}/members/${member_id}`);
return {
member_id,
card: nick,
roles,
...member,
join_time: new Date(joined_at).getTime() / 1000,
};
}
/**
* 获取群成员列表
* @param group_id
*/
async getGroupMemberList(group_id) {
throw constants_1.UnsupportedMethodError;
}
/**
* 获取群成员信息
* @param group_id
* @param member_id
*/
async getGroupMemberInfo(group_id, member_id) {
throw constants_1.UnsupportedMethodError;
}
/**
* 获取好友列表
*/
async getFriendList() {
throw constants_1.UnsupportedMethodError;
}
/**
* 获取好友信息
* @param friend_id
*/
async getFriendInfo(friend_id) {
throw constants_1.UnsupportedMethodError;
}
/**
* 发送私聊信息
* @param user_id
* @param message
* @param source
*/
async sendPrivateMessage(user_id, message, source) {
return this.messageService.sendPrivateMessage(user_id, message, source);
}
/**
* 撤回私聊消息
* @param user_id
* @param message_id
*/
async recallPrivateMessage(user_id, message_id) {
const result = await this.request.delete(`/v2/users/${user_id}/messages/${message_id}`);
return result.status === 200;
}
/**
* 发送群消息
* @param group_id
* @param message
* @param source
*/
async sendGroupMessage(group_id, message, source) {
return this.messageService.sendGroupMessage(group_id, message, source);
}
/**
* 撤回群消息
* @param group_id
* @param message_id
*/
async recallGroupMessage(group_id, message_id) {
return this.messageService.recallGroupMessage(group_id, message_id);
}
/**
* 获取子频道列表
*/
async getChannelList(guild_id) {
return this.channelService.getList(guild_id);
}
/**
* 获取子频道信息
* @param channel_id
*/
async getChannelInfo(channel_id) {
return this.channelService.getInfo(channel_id);
}
/**
* 创建私信会话
* @param guild_id
* @param user_id
*/
async createDirectSession(guild_id, user_id) {
return this.messageService.createDirectSession(guild_id, user_id);
}
/**
* 发送频道私信
* @param guild_id
* @param message
* @param source
*/
async sendDirectMessage(guild_id, message, source) {
return this.messageService.sendDirectMessage(guild_id, message, source);
}
/**
* 获取频道私信
* @param guild_id
* @param message_id
*/
async getDirectMessage(guild_id, message_id) {
return this.messageService.getDirectMessage(guild_id, message_id);
}
/**
* 撤回频道私信
* @param guild_id
* @param message_id
* @param hidetip
*/
async recallDirectMessage(guild_id, message_id, hidetip) {
return this.messageService.recallDirectMessage(guild_id, message_id, hidetip);
}
/**
* 发送频道消息
* @param channel_id
* @param message
* @param source
*/
async sendGuildMessage(channel_id, message, source) {
return this.messageService.sendGuildMessage(channel_id, message, source);
}
/**
* 撤回频道消息
* @param channel_id
* @param message_id
* @param hidetip
*/
async recallGuildMessage(channel_id, message_id, hidetip) {
return this.messageService.recallGuildMessage(channel_id, message_id, hidetip);
}
/**
* 添加频道消息表态
* @param channel_id {string} 子频道id
* @param message_id {string} 消息id
* @param type {0|1} 表情类型
* @param id {`${number}`} 表情id
*/
async addGuildMessageReaction(channel_id, message_id, type, id) {
return this.reactionService.addGuildMessageReaction(channel_id, message_id, type, id);
}
/**
* 删除频道消息表态
* @param channel_id {string} 子频道id
* @param message_id {string} 消息id
* @param type {EmojiType} 表情类型
* @param id {`${number}`} 表情id
*/
async deleteGuildMessageReaction(channel_id, message_id, type, id) {
return this.reactionService.deleteGuildMessageReaction(channel_id, message_id, type, id);
}
/**
* 获取表态用户列表
* @param channel_id {string} 子频道id
* @param message_id {string} 消息id
* @param type {0|1} 表情类型
* @param id {`${number}`} 表情id
*/
async getGuildMessageReactionMembers(channel_id, message_id, type, id) {
const users = await this.reactionService.getGuildMessageReactionMembers(channel_id, message_id, type, id);
return users.map(user => ({
user_id: user.id,
user_name: user.username,
avatar: user.avatar
}));
}
/** 获取频道日程
* @param channel_id {string}
* @param since {number}
*/
async getChannelSchedules(channel_id, since) {
return this.scheduleService.getChannelSchedules(channel_id, since);
}
/**
* 获取日程详情
* @param channel_id
* @param schedule_id
*/
async getChannelScheduleInfo(channel_id, schedule_id) {
return this.scheduleService.getChannelSchedule(channel_id, schedule_id);
}
/**
* 创建日程
* @param channel_id
* @param schedule
*/
async createChannelSchedule(channel_id, schedule) {
const { data } = await this.request.post(`/channels/${channel_id}/schedules`, schedule);
return data;
}
/**
* 修改日程
* @param channel_id
* @param schedule_id
* @param schedule
*/
async updateChannelSchedule(channel_id, schedule_id, schedule) {
const { data } = await this.request.patch(`/channels/${channel_id}/schedules/${schedule_id}`, schedule);
return data;
}
/**
* 删除日程
* @param channel_id
* @param schedule_id
*/
async deleteChannelSchedule(channel_id, schedule_id) {
const { data } = await this.request.delete(`/channels/${channel_id}/schedules/${schedule_id}`);
return data;
}
/**
* 音频控制
* @param channel_id
* @param audio_control
*/
async controlChannelAudio(channel_id, audio_control) {
const result = await this.request.post(`/channels/${channel_id}/audio`, audio_control);
return result.status === 200;
}
/**
* 上麦
* @param channel_id
*/
async setOnlineMic(channel_id) {
const result = await this.request.put(`/channels/${channel_id}/mic`);
return result.status === 200;
}
/**
* 下麦
* @param channel_id
*/
async setOfflineMic(channel_id) {
const result = await this.request.delete(`/channels/${channel_id}/mic`);
return result.status === 204;
}
/**
* 获取频道帖子列表
* @param channel_id
*/
async getChannelThreads(channel_id) {
const { data } = await this.request.get(`/channels/${channel_id}/threads`);
return data;
}
/**
* 获取频道帖子详情
* @param channel_id
* @param thread_id
*/
async getChannelThreadInfo(channel_id, thread_id) {
const { data } = await this.request.get(`/channels/${channel_id}/threads/${thread_id}`);
return data;
}
/**
* 创建频道帖子
* @param channel_id
* @param title
* @param content
* @param format {1|2|3|4}
*/
async publishThread(channel_id, title, content, format = 3) {
const { data } = await this.request.post(`/channels/${channel_id}/threads`, {
title,
content,
format
});
return data;
}
/**
* 删除频道帖子
* @param channel_id
* @param thread_id
*/
async deleteThread(channel_id, thread_id) {
const result = await this.request.delete(`/channels/${channel_id}/threads/${thread_id}`);
return result.status === 204;
}
/**
* 回应操作
* @param action_id {string} 操作id
* @param code {number}
*/
async replyAction(action_id, code = 0) {
const result = await this.request.put(`/interactions/${action_id}`, { code });
return result.status === 200;
}
async start() {
await this.sessionManager.start();
return this;
}
async stop() {
await this.sessionManager.stop();
}
}
exports.Bot = Bot;
function defineConfig(config) {
return config;
}
function createBot(config) {
return new Bot(config);
}