From c309a23d5fb512f9e7f45c75e5e121688fd461cb Mon Sep 17 00:00:00 2001 From: SanaeFox <36219542+Hoshinonyaruko@users.noreply.github.com> Date: Sun, 19 Nov 2023 14:15:47 +0800 Subject: [PATCH] Beta41 (#142) * beta41 * api add --- handlers/get_guild_channel_list.go | 40 +++++++++++++++++++++++++++--- handlers/get_guild_list.go | 31 ++++++++++++++++------- silk/silk.go | 2 +- 3 files changed, 59 insertions(+), 14 deletions(-) diff --git a/handlers/get_guild_channel_list.go b/handlers/get_guild_channel_list.go index da268b7c..40f8c26f 100644 --- a/handlers/get_guild_channel_list.go +++ b/handlers/get_guild_channel_list.go @@ -1,6 +1,8 @@ package handlers import ( + "context" + "github.com/hoshinonyaruko/gensokyo/callapi" "github.com/hoshinonyaruko/gensokyo/mylog" "github.com/tencent-connect/botgo/openapi" @@ -19,21 +21,51 @@ func init() { } func getGuildChannelList(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.OpenAPI, message callapi.ActionMessage) { - var response GuildChannelListResponse - response.Data = make([]interface{}, 0) // No data at the moment, but can be populated in the future + // 解析请求参数 + guildID := message.Params.GuildID + + // 根据请求参数调用API + channels, err := api.Channels(context.TODO(), guildID) + if err != nil { + // 如果发生错误,记录日志并返回null + mylog.Printf("Error fetching channels: %v", err) + client.SendMessage(map[string]interface{}{"data": nil}) + return + } + + // 构建响应数据 + for _, channel := range channels { + channelInfo := map[string]interface{}{ + "owner_guild_id": guildID, + "channel_id": channel.ID, + "channel_type": channel.Type, + "channel_name": channel.Name, + "create_time": 0, // Default value as actual value is not available + "creator_tiny_id": channel.OwnerID, + "talk_permission": channel.Permissions, + "visible_type": channel.Position, + "current_slow_mode": 0, // Default value as actual value is not available + } + + // Append the channel information to the response data + response.Data = append(response.Data, channelInfo) + } + + // Set other fields of the response response.Message = "" response.RetCode = 0 response.Status = "ok" response.Echo = message.Echo - // Convert the members slice to a map + // Convert the response to a map for sending outputMap := structToMap(response) mylog.Printf("get_guild_channel_list: %s", outputMap) - err := client.SendMessage(outputMap) //发回去 + // Send the response + err = client.SendMessage(outputMap) if err != nil { mylog.Printf("Error sending message via client: %v", err) } diff --git a/handlers/get_guild_list.go b/handlers/get_guild_list.go index 0bedb799..aca24185 100644 --- a/handlers/get_guild_list.go +++ b/handlers/get_guild_list.go @@ -1,8 +1,11 @@ package handlers import ( + "context" + "github.com/hoshinonyaruko/gensokyo/callapi" "github.com/hoshinonyaruko/gensokyo/mylog" + "github.com/tencent-connect/botgo/dto" "github.com/tencent-connect/botgo/openapi" ) @@ -25,32 +28,42 @@ func init() { } func getGuildList(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.OpenAPI, message callapi.ActionMessage) { - var response GuildListResponse - // Assuming 'a' is some value you want to loop till. - a := 1 // Replace with appropriate value + // 创建一个 GuildPager 实例,设置 limit 为 10 + pager := dto.GuildPager{Limit: "50", After: "0"} // 默认从0开始,取50个 + + // 调用 API 获取群组列表 + guilds, err := api.MeGuilds(context.Background(), &pager) + if err != nil { + mylog.Printf("Error fetching guilds: %v", err) + return + } - for i := 1; i <= a; i++ { + // 将获取的群组数据添加到 response 中 + for _, guild := range guilds { guildData := GuildData{ - GuildID: "0", - GuildName: "868858989", - GuildDisplayID: "868858989", + GuildID: guild.ID, + GuildName: guild.Name, + GuildDisplayID: guild.ID, // 或其他合适的字段 + // ... 其他需要的字段 } response.Data = append(response.Data, guildData) } + // 设置 response 的其他属性 response.Message = "" response.RetCode = 0 response.Status = "ok" response.Echo = message.Echo - // Convert the members slice to a map + // Convert the response to a map (if needed) outputMap := structToMap(response) mylog.Printf("getGuildList(频道): %+v\n", outputMap) - err := client.SendMessage(outputMap) + // 发送消息 + err = client.SendMessage(outputMap) if err != nil { mylog.Printf("Error sending message via client: %v", err) } diff --git a/silk/silk.go b/silk/silk.go index 7a37bc98..03bc7f8f 100644 --- a/silk/silk.go +++ b/silk/silk.go @@ -239,7 +239,7 @@ func encode(record []byte, tempName string) (silkWav []byte) { } // 使用临时文件执行silk_codec - cmd = exec.Command(tmpFile.Name(), "pts", "-i", pcmPath, "-o", silkPath, "-s", "24000") + cmd = exec.Command(tmpFile.Name(), "pts", "-i", pcmPath, "-o", silkPath, "-s", strconv.Itoa(sampleRate)) if err := cmd.Run(); err != nil { mylog.Errorf("silk encode error: %v", err) return nil