UNPKG

qq-official-bot

Version:
222 lines (221 loc) 6.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GuildService = void 0; class GuildService { constructor(bot) { this.bot = bot; } /** * 获取频道列表 */ async getList() { try { const result = await this._getGuildList(); return { success: true, data: result }; } catch (error) { return { success: false, error: { code: error.status || 500, message: error.message } }; } } /** * 获取频道信息 */ async getInfo(guildId) { try { const { data: { id: _, name: guild_name, joined_at, ...guild } } = await this.bot.request.get(`/guilds/${guildId}`); const result = { guild_id: guildId, guild_name, join_time: new Date(joined_at).getTime() / 1000, ...guild }; return { success: true, data: result }; } catch (error) { return { success: false, error: { code: error.status || 500, message: error.message } }; } } /** * 频道禁言 */ async mute(guildId, seconds, endTime) { try { const result = await this.bot.request.put(`/guilds/${guildId}/mute`, { mute_seconds: `${seconds}`, mute_end_timestamp: `${endTime || 0}` }); return { success: true, data: result.status === 204 }; } catch (error) { return { success: false, error: { code: error.status || 500, message: error.message } }; } } /** * 取消频道禁言 */ async unmute(guildId) { return this.mute(guildId, 0, 0); } /** * 获取频道角色列表 */ async getRoles(guildId) { try { const { data: { roles = [] } = {} } = await this.bot.request.get(`/guilds/${guildId}/roles`); return { success: true, data: roles }; } catch (error) { return { success: false, error: { code: error.status || 500, message: error.message } }; } } /** * 创建频道角色 */ async createRole(guildId, role) { try { const { data: { role: result } } = await this.bot.request.post(`/guilds/${guildId}/roles`, role); return { success: true, data: result }; } catch (error) { return { success: false, error: { code: error.status || 500, message: error.message } }; } } /** * 更新频道角色 */ async updateRole(guildId, roleId, updateInfo) { try { const { data: { role: result } } = await this.bot.request.patch(`/guilds/${guildId}/roles/${roleId}`, updateInfo); return { success: true, data: result }; } catch (error) { return { success: false, error: { code: error.status || 500, message: error.message } }; } } /** * 删除频道角色 */ async deleteRole(roleId) { try { const result = await this.bot.request.delete(`/guilds/{guild_id}/roles/${roleId}`); return { success: true, data: result.status === 204 }; } catch (error) { return { success: false, error: { code: error.status || 500, message: error.message } }; } } /** * 获取频道可访问API类别 */ async getAccessApis(guildId) { try { const { data: { apis = [] } } = await this.bot.request.get(`/guilds/${guildId}/api_permission`); return { success: true, data: apis }; } catch (error) { return { success: false, error: { code: error.status || 500, message: error.message } }; } } /** * 申请频道API权限 */ async applyAccess(guildId, channelId, apiInfo, desc) { try { const { data: result } = await this.bot.request.post(`/guilds/${guildId}/api_permission/demand`, { channel_id: channelId, api_identify: apiInfo, desc, }); return { success: true, data: result }; } catch (error) { return { success: false, error: { code: error.status || 500, message: error.message } }; } } /** * 私有方法:获取频道列表的实现 */ async _getGuildList(after) { const res = await this.bot.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]; if (result.length === 100) { // 如果返回了100条,可能还有更多 const nextResults = await this._getGuildList(last.guild_id); return [...result, ...nextResults]; } return result; } } exports.GuildService = GuildService;