Skip to content

Commit

Permalink
fix: add more api
Browse files Browse the repository at this point in the history
  • Loading branch information
lc-cn committed Jul 16, 2024
1 parent 3beeee7 commit 47a989b
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 15 deletions.
57 changes: 53 additions & 4 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import {ChannelMember} from "@/entries/channelMember";
import {EventMap} from "@/event";
import {Quotable, Sendable} from "@/elements";
type MemberMap=Map<string,User.Info>
type BlackMap=Map<string,Guild.BlackInfo>
export class Client extends BaseClient {
guilds:Map<string,Guild.Info>=new Map<string, Guild.Info>()
blacklist:Map<string,BlackMap>=new Map<string, BlackMap>()
channels:Map<string,Channel.Info>=new Map<string, Channel.Info>()
guildMembers:Map<string,MemberMap>=new Map<string, MemberMap>()
channelMembers:Map<string,MemberMap>=new Map<string, MemberMap>()
Expand All @@ -28,7 +30,21 @@ export class Client extends BaseClient {
this.logger.debug(e.stack)
})
}

get black_user_nums(){
return [...this.blacklist.values()].reduce((a,b)=>a+b.size,0)
}
async setOnline(){
const result = await this.request.post('/v3/user/online')
return result['code']===0
}
async setOffline(){
const result = await this.request.post('/v3/user/offline')
return result['code']===0
}
async getOnlineStatus():Promise<{online:boolean,online_os:string[]}>{
const {data}=await this.request.get('/v3/user/get-online-status')
return data
}
/**
* 获取频道列表
*/
Expand All @@ -48,7 +64,14 @@ export class Client extends BaseClient {
}
return await _getGuildList() as Guild.Info[]
}

async getBlacklist(guild_id:string):Promise<Guild.BlackInfo[]>{
const {data:{items}}=await this.request.get('/v3/blacklist/list',{
params:{
guild_id
}
})
return items
}
/**
* 获取频道信息
* @param guild_id
Expand Down Expand Up @@ -108,6 +131,24 @@ export class Client extends BaseClient {
async sendChannelMsg(channel_id:string,message:Sendable,quote?:Quotable){
return this.pickChannel(channel_id).sendMsg(message,quote)
}
async getPrivateMsg(user_id:string,message_id:string){
return this.pickUser(user_id).getMsg(message_id)
}
async getChannelMsg(channel_id:string,message_id:string){
return this.pickChannel(channel_id).getMsg(message_id)
}
async recallPrivateMsg(user_id:string,message_id:string){
return this.pickUser(user_id).recallMsg(message_id)
}
async recallChannelMsg(channel_id:string,message_is:string){
return this.pickChannel(channel_id).recallMsg(message_is)
}
async getPrivateChatHistory(user_id:string,message_id?:string,len:number=50){
return this.pickUser(user_id).getChatHistory(message_id,len)
}
async getChannelChatHistory(channel_id:string,message_id?:string,len:number=50){
return this.pickChannel(channel_id).getChatHistory(message_id,len)
}
async #initChannels(guild_id:string){
const channels = await this.getChannelList(guild_id)
for(const channel of channels){
Expand All @@ -129,18 +170,26 @@ export class Client extends BaseClient {
this.users.set(user.id,user)
}
}
async #initBlacklist(guild_id:string){
this.blacklist.set(guild_id,new Map<string, Guild.BlackInfo>())
const blacklist = await this.getBlacklist(guild_id)
for(const temp of blacklist){
this.blacklist.get(guild_id)?.set(temp.user_id,temp)
}
}
async init(){
const userInfo=await this.getSelfInfo()
this.self_id=userInfo.id
this.nickname=userInfo.nickname
this.logger.info(`welcome ${this.nickname}, 正在加载资源...`)
const guilds=await this.getGuildList()
for(const guildInfo of guilds){
this.guilds.set(guildInfo.id,guildInfo)
await this.#initChannels(guildInfo.id)
await this.#initUsers(guildInfo.id)
this.guilds.set(guildInfo.id,guildInfo)
await this.#initBlacklist(guildInfo.id)
}
this.logger.info(`加载了${this.guilds.size}个服务器,共计${this.channels.size}个频道,${this.users.size}个用户`)
this.logger.info(`加载了${this.guilds.size}个服务器,共计${this.channels.size}个频道,${this.users.size}个用户,${this.blacklist.size}个黑名单用户`)
}
async connect() {
await this.receiver.connect()
Expand Down
34 changes: 24 additions & 10 deletions src/entries/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,6 @@ export class Channel extends Contact{
Channel.map.set(data,this)
this.info=data
}
async getMsg(message_id: string): Promise<Message> {
const {data}=await this.c.request.get('/v3/message/view',{
params:{
msg_id:message_id
}
})
return ChannelMessageEvent.fromDetail(this.c,this.info.id,data)
}

async delete(){
const result=await this.c.request.post('/v3/channel/delete',{
channel_id:this.info.id
Expand All @@ -37,13 +28,36 @@ export class Channel extends Contact{
this.c.channelMembers.delete(this.info.id)
Channel.map.delete(this.info)
}
async addUsers(user_ids:string[]){
const result=await this.c.request.post('/v3/channel/move-user',{
channel_id:this.info.id,
user_ids
})
return result['code']===0
}
async getMsg(message_id: string): Promise<Message> {
const {data}=await this.c.request.get('/v3/message/view',{
params:{
msg_id:message_id
}
})
return ChannelMessageEvent.fromDetail(this.c,this.info.id,data)
}
async getUserList():Promise<User.Info[]>{
const {data}=await this.c.request.get('/v3/channel/user-list',{
params:{
channel_id:this.info.id
}
})
return data
}

/**
* 获取指定消息之前的聊天历史
* @param message_id {string} 消息id 不传则查询最新消息
* @param len {number} 获取的聊天历史长度 默认50条
*/
async getChatHistory(message_id?:string,len:number=50){
async getChatHistory(message_id?:string,len:number=50):Promise<Message[]>{
const result= await this.c.request.post('/v3/message/list',{
target_id:this.info.id,
msg_id:message_id,
Expand Down
6 changes: 6 additions & 0 deletions src/entries/channelMember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ export class ChannelMember extends User{
constructor(c:Client,public channel_id:string,info:User.Info) {
super(c,info);
}
get channel(){
return this.c.pickChannel(this.channel_id)
}
async move(channel_id:string){
return this.channel.addUsers([this.info.id])
}
}
export namespace ChannelMember{
export const map:WeakMap<User.Info,ChannelMember>=new WeakMap<User.Info, ChannelMember>()
Expand Down
44 changes: 44 additions & 0 deletions src/entries/guild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {Contact} from "@/entries/contact";
import {Client, Message, Quotable, Sendable} from "@";
import {NotifyType, UnsupportedMethodError} from "@/constans";
import {Channel} from "@/entries/channel";
import {Role} from "@/entries/role";
import {User} from "@/entries/user";

export class Guild extends Contact{
constructor(c:Client,public info:Guild.Info) {
Expand All @@ -22,6 +24,42 @@ export class Guild extends Contact{
if(result['code']===0) return this.c.guilds.delete(this.info.id)
throw new Error(result['message'])
}
async getRoleList():Promise<Role.Info[]>{
const _getRoleList=async (page:number=1,page_size=100)=>{
const {data:{items=[],meta={page:1,total_page:1}}}=await this.c.request.get('/v3/guild-role/list',{
params:{
guild_id:this.info.id,
page
}
})
if(meta.total_page<=page) return items
return items.concat(await _getRoleList(page+1,page_size))
}
return await _getRoleList()
}
async createRole(name:string):Promise<Role.Info>{
const {data}=await this.c.request.post('/v3/guild-role/create',{
guild_id:this.info.id,
name
})
return data
}
async updateRole(role_id:string,modifyInfo:Partial<Omit<Role.Info, 'id'>>){
const {data}=await this.c.request.post('/v3/guild-role/update',{
guild_id:this.info.id,
role_id,
...modifyInfo
})
return data
}
async deleteRole(role_id:string){
const result=await this.c.request.post('/v3/guild-role/delete',{
guild_id:this.info.id,
role_id
})
if(result['code']===0) return true
throw new Error(result['message'])
}
async kick(user_id:string){
const result=await this.c.request.post('/v3/guild/kickout',{
guild_id:this.info.id,
Expand Down Expand Up @@ -131,4 +169,10 @@ export namespace Guild {
number: number
member_limit: number
}
export interface BlackInfo{
user_id:string
created_time:number
remark:string
user:User.Info
}
}
41 changes: 41 additions & 0 deletions src/entries/guildMember.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,47 @@ export class GuildMember extends User{
get guild(){
return this.c.pickGuild(this.guild_id)
}
async setNickname(nickname:string){
const result = await this.c.request.post('/v3/guild/nickname', {
guild_id:this.guild_id,
nickname,
user_id:this.info.id
})
return result['code']===0
}
async grant(role_id:string){
const result = await this.c.request.post('/v3/guild/role', {
guild_id:this.guild_id,
role_id,
user_id:this.info.id
})
return result['code']===0
}
async revoke(role_id:string){
const result = await this.c.request.post('/v3/guild/role', {
guild_id:this.guild_id,
role_id,
user_id:this.info.id
})
return result['code']===0
}
async addToBlackList(remark?:string,del_msg_days=0){
if(this.c.blacklist.get(this.guild_id)?.has(this.info.id)) throw new Error(`用户(${this.info.id}) 已在黑名单中`)
const result = await this.c.request.post('/v3/blacklist/create', {
guild_id:this.guild_id,
user_id:this.info.id,
remark,
del_msg_days
})
return result['code']===0
}
async removeFromBlackList(){
const result = await this.c.request.post('/v3/blacklist/delete', {
guild_id:this.guild_id,
user_id:this.info.id
})
return result['code']===0
}
kick(){
return this.guild.kick(this.info.id)
}
Expand Down
30 changes: 30 additions & 0 deletions src/entries/role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,34 @@ export namespace Role{
mentionable:number
permissions:number
}
export enum Permission{
Admin=1,
ManageGuild,
ViewAdminLog=4,
CreateGuildInvite=8,
ManageInvite=16,
ManageChannel=32,
KickUser=64,
BanUser=128,
ManageCustomFace=256,
UpdateGuildName=512,
ManageRole=1024,
ViewContentOrVoiceChannel=2048,
PublishMsg=4096,
ManageMsg=8192,
UploadFile=16384,
VoiceLink=32768,
ManageVoice=65536,
AtAll=131072,
AddReaction=262144,
FollowReaction=524288,
PassiveLinkVoiceChannel=1048576,
PressKeyTalk=2097152,
FreeTally=4194304,
Talk=8388608,
MuteGuild=16777216,
CloseGuildWheat=33554432,
UpdateOtherUserNickname=67108864,
PlayMusic=134217728,
}
}
2 changes: 1 addition & 1 deletion src/entries/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class User extends Contact{
* @param message_id {string} 消息id 不传则查询最新消息
* @param len {number} 获取的聊天历史长度 默认50条
*/
async getChatHistory(message_id?:string,len:number=50){
async getChatHistory(message_id?:string,len:number=50):Promise<Message[]>{
const result= await this.c.request.post('/v3/direct-message/list',{
target_id:this.info.id,
msg_id:message_id,
Expand Down

0 comments on commit 47a989b

Please sign in to comment.