UNPKG

qq-official-bot

Version:
213 lines (212 loc) 6.46 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MessageService = void 0; const message_1 = require("../message"); class MessageService { constructor(bot) { this.bot = bot; } /** * 获取子频道消息 */ async getGuildMessage(channelId, messageId) { try { const { data: result } = await this.bot.request.get(`/channels/${channelId}/messages/${messageId}`); return { success: true, data: result }; } catch (error) { return { success: false, error: { code: error.status || 500, message: error.message } }; } } /** * 发送频道消息 */ async sendGuildMessage(channelId, message, source) { try { const messageSender = new message_1.MessageSender(this.bot, `/channels/${channelId}`, source); const result = await messageSender.send(message); this.bot.logger.info(`send to Channel(${channelId}): ${await messageSender.getBrief(message)}`); return { success: true, data: result }; } catch (error) { return { success: false, error: { code: error.status || 500, message: error.message } }; } } /** * 撤回频道消息 */ async recallGuildMessage(channelId, messageId, hideWarning = false) { try { const result = await this.bot.request.delete(`/channels/${channelId}/messages/${messageId}`, { data: hideWarning ? { hidetip: true } : undefined }); return { success: true, data: result.status === 200 }; } catch (error) { return { success: false, error: { code: error.status || 500, message: error.message } }; } } /** * 创建私信会话 */ async createDirectSession(guildId, userId) { try { const { data: result } = await this.bot.request.post(`/users/@me/dms`, { recipient_id: userId, source_guild_id: guildId }); return { success: true, data: result }; } catch (error) { return { success: false, error: { code: error.status || 500, message: error.message } }; } } /** * 发送频道私信 */ async sendDirectMessage(guildId, message, source) { try { const messageSender = new message_1.MessageSender(this.bot, `/dms/${guildId}`, source); const result = await messageSender.send(message); this.bot.logger.info(`send to DM(${guildId}): ${await messageSender.getBrief(message)}`); return { success: true, data: result }; } catch (error) { return { success: false, error: { code: error.status || 500, message: error.message } }; } } /** * 获取频道私信 */ async getDirectMessage(guildId, messageId) { try { const { data: result } = await this.bot.request.get(`/dms/${guildId}/messages/${messageId}`); return { success: true, data: result }; } catch (error) { return { success: false, error: { code: error.status || 500, message: error.message } }; } } /** * 发送私聊消息 */ async sendPrivateMessage(userId, message, source) { try { const messageSender = new message_1.MessageSender(this.bot, `/v2/users/${userId}`, source); const result = await messageSender.send(message); this.bot.logger.info(`send to User(${userId}): ${await messageSender.getBrief(message)}`); return { success: true, data: result }; } catch (error) { return { success: false, error: { code: error.status || 500, message: error.message } }; } } /** * 撤回私聊消息 */ async recallPrivateMessage(userId, messageId) { try { const result = await this.bot.request.delete(`/v2/users/${userId}/messages/${messageId}`); return { success: true, data: result.status === 200 }; } catch (error) { return { success: false, error: { code: error.status || 500, message: error.message } }; } } /** * 发送群消息 */ async sendGroupMessage(groupId, message, source) { try { const messageSender = new message_1.MessageSender(this.bot, `/v2/groups/${groupId}`, source); const result = await messageSender.send(message); this.bot.logger.info(`send to Group(${groupId}): ${await messageSender.getBrief(message)}`); return { success: true, data: result }; } catch (error) { return { success: false, error: { code: error.status || 500, message: error.message } }; } } /** * 撤回群消息 */ async recallGroupMessage(groupId, messageId) { try { const result = await this.bot.request.delete(`/v2/groups/${groupId}/messages/${messageId}`); return { success: true, data: result.status === 200 }; } catch (error) { return { success: false, error: { code: error.status || 500, message: error.message } }; } } } exports.MessageService = MessageService;