UNPKG

qq-official-bot

Version:
777 lines (776 loc) 25.7 kB
"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 event_dispatcher_1 = require("./events/event-dispatcher"); // 导入服务模块 const services_1 = require("./services"); class Bot extends client_1.Client { constructor(config) { super(config); this.fileProcessor = new message_1.FileProcessor(this); this.eventDispatcher = new event_dispatcher_1.EventDispatcher(this); // 服务实例 this.guildService = new services_1.GuildService(this); this.channelService = new services_1.ChannelService(this); this.messageService = new services_1.MessageService(this); this.memberService = new services_1.MemberService(this); this.permissionService = new services_1.PermissionService(this); this.reactionService = new services_1.ReactionService(this); this.scheduleService = new services_1.ScheduleService(this); this.threadService = new services_1.ThreadService(this); this.audioService = new services_1.AudioService(this); this.botService = new services_1.BotService(this); // 获取基础URL const baseUrl = this.config.sandbox ? 'https://sandbox.api.sgroup.qq.com' : 'https://api.sgroup.qq.com'; // 初始化重构后的组件 this.messageBuilder = new message_1.MessageBuilder(this.config.appid, baseUrl); // 初始化事件调度器 this.eventDispatcher = new event_dispatcher_1.EventDispatcher(this); 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); }); // 设置事件调度器的错误处理 this.setupEventDispatcherErrorHandling(); } /** * 设置事件调度器的错误处理 */ setupEventDispatcherErrorHandling() { this.eventDispatcher.on('error', (error) => { this.logger.error('[EVENT] 事件处理错误:', error); }); } 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) { const baseUrl = this.config.sandbox ? 'https://sandbox.api.sgroup.qq.com' : 'https://api.sgroup.qq.com'; const messageSender = new message_1.MessageSender(this, `/v2/users/${user_id}`, source); const result = await messageSender.send(message); this.logger.info(`send to User(${user_id}): ${await messageSender.getBrief(message)}`); return result; } /** * 撤回私聊消息 * @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) { const messageSender = new message_1.MessageSender(this, `/v2/groups/${group_id}`, source); const result = await messageSender.send(message); this.logger.info(`send to Group(${group_id}): ${await messageSender.getBrief(message)}`); return result; } /** * 撤回群消息 * @param group_id * @param message_id */ async recallGroupMessage(group_id, message_id) { const result = await this.request.delete(`/v2/groups/${group_id}/messages/${message_id}`); return result.status === 200; } /** * 获取子频道列表 */ async getChannelList(guild_id) { const { data: result = [] } = await this.request.get(`/guilds/${guild_id}/channels`); return result.map(({ id: channel_id, name: channel_name, ...channel }) => { return { channel_id, channel_name, ...channel }; }); } /** * 获取子频道信息 * @param channel_id */ async getChannelInfo(channel_id) { const { data: { id: _, name: channel_name, ...channel } } = await this.request.get(`/channels/${channel_id}`); return { channel_id, channel_name, ...channel }; } /** * 创建私信会话 * @param guild_id * @param user_id */ async createDirectSession(guild_id, user_id) { const { data: result } = await this.request.post(`/users/@me/dms`, { recipient_id: user_id, source_guild_id: guild_id }); return { ...result, create_time: new Date(result.create_time).getTime() / 1000 }; } /** * 发送频道私信 * @param guild_id * @param message * @param source */ async sendDirectMessage(guild_id, message, source) { const messageSender = new message_1.MessageSender(this, `/dms/${guild_id}`, source); const result = await messageSender.send(message); this.logger.info(`send to Direct(${guild_id}): ${await messageSender.getBrief(message)}`); return result; } /** * 获取频道私信 * @param guild_id * @param message_id */ async getDirectMessage(guild_id, message_id) { const { data: payload } = await this.request.get(`/dms/${guild_id}/messages/${message_id}`); return this.processPayload(payload.id, `message.direct`, payload); } /** * 撤回频道私信 * @param guild_id * @param message_id * @param hidetip */ async recallDirectMessage(guild_id, message_id, hidetip) { const result = await this.request.delete(`/dms/${guild_id}/messages/${message_id}?hidetip=${!!hidetip}`); return result.status === 200; } /** * 发送频道消息 * @param channel_id * @param message * @param source */ async sendGuildMessage(channel_id, message, source) { const messageSender = new message_1.MessageSender(this, `/channels/${channel_id}`, source); const result = await messageSender.send(message); this.logger.info(`send to Channel(${channel_id}/messages): ${await messageSender.getBrief(message)}`); return result; } /** * 撤回频道消息 * @param channel_id * @param message_id * @param hidetip */ async recallGuildMessage(channel_id, message_id, hidetip) { const result = await this.request.delete(`/channels/${channel_id}/messages/${message_id}?hidetip=${!!hidetip}`); return result.status === 200; } /** * 添加频道消息表态 * @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) { const result = await this.request.put(`/channels/${channel_id}/messages/${message_id}/reactions/${type}/${id}`); return result.status === 200; } /** * 添加频道消息表态 * @deprecated use addGuildMessageReaction instead * @param channel_id {string} 子频道id * @param message_id {string} 消息id * @param type {0|1} 表情类型 * @param id {`${number}`} 表情id */ async reactionGuildMessage(channel_id, message_id, type, id) { return this.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) { const result = await this.request.delete(`/channels/${channel_id}/messages/${message_id}/reactions/${type}/${id}`); return result.status === 204; } /** * 获取表态用户列表 * @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 formatUser = (users) => { return users.map(user => { return { user_id: user.id, user_name: user.username, avatar: user.avatar }; }); }; const getMembers = async (cookies) => { const { data: { users, cookie, is_end } } = await this.request.get(`/channels/${channel_id}/messages/${message_id}/reactions/${type}/${id}`, { params: { cookie: cookies } }); if (is_end) return formatUser(users); return [...formatUser(users), ...await getMembers(cookie)]; }; return await getMembers(); } /** 获取频道日程 * @param channel_id {string} * @param since {number} */ async getChannelSchedules(channel_id, since) { const { data } = await this.request.get(`/channels/${channel_id}/schedules`, { params: { since } }); return data; } /** * 获取日程详情 * @param channel_id * @param schedule_id */ async getChannelScheduleInfo(channel_id, schedule_id) { const { data } = await this.request.get(`/channels/${channel_id}/schedules/${schedule_id}`); return data; } /** * 创建日程 * @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); }