Skip to content

Commit

Permalink
imp: 重构 SetGroupCardName() (#370)
Browse files Browse the repository at this point in the history
feat: 实现 `Dodo` 平台的 `QuitGroup()` 和 `SetGroupCardName()`
  • Loading branch information
Szzrain authored Oct 26, 2023
1 parent 0376982 commit 74cc86c
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 34 deletions.
6 changes: 3 additions & 3 deletions dice/ext_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func SetPlayerGroupCardByTemplate(ctx *MsgContext, tmpl string) (string, error)
return text, ErrGroupCardOverlong
}

ctx.EndPoint.Adapter.SetGroupCardName(ctx.Group.GroupID, ctx.Player.UserID, text)
ctx.EndPoint.Adapter.SetGroupCardName(ctx, text)
return text, nil
}

Expand Down Expand Up @@ -563,15 +563,15 @@ func RegisterBuiltinExtLog(self *Dice) {
ctx.Player.Name = ctx.Player.Name[len("ob"):]
ctx.Player.UpdatedAtTime = time.Now().Unix()
}
ctx.EndPoint.Adapter.SetGroupCardName(ctx.Group.GroupID, ctx.Player.UserID, ctx.Player.Name)
ctx.EndPoint.Adapter.SetGroupCardName(ctx, ctx.Player.Name)
text := DiceFormatTmpl(ctx, "日志:OB_关闭")
ReplyToSender(ctx, msg, text)
default:
if !strings.HasPrefix(strings.ToLower(ctx.Player.Name), "ob") {
ctx.Player.Name = "ob" + ctx.Player.Name
ctx.Player.UpdatedAtTime = time.Now().Unix()
}
ctx.EndPoint.Adapter.SetGroupCardName(ctx.Group.GroupID, ctx.Player.UserID, ctx.Player.Name)
ctx.EndPoint.Adapter.SetGroupCardName(ctx, ctx.Player.Name)
text := DiceFormatTmpl(ctx, "日志:OB_开启")
ReplyToSender(ctx, msg, text)
}
Expand Down
2 changes: 1 addition & 1 deletion dice/platform_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type PlatformAdapter interface {

SendToPerson(ctx *MsgContext, uid string, text string, flag string)
SendToGroup(ctx *MsgContext, uid string, text string, flag string)
SetGroupCardName(groupID string, userID string, name string)
SetGroupCardName(ctx *MsgContext, name string)

SendFileToPerson(ctx *MsgContext, uid string, path string, flag string)
SendFileToGroup(ctx *MsgContext, uid string, path string, flag string)
Expand Down
2 changes: 1 addition & 1 deletion dice/platform_adapter_dingtalk.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (pa *PlatformAdapterDingTalk) SendToGroup(ctx *MsgContext, uid string, text
}, flag)
}

func (pa *PlatformAdapterDingTalk) SetGroupCardName(groupID string, userID string, name string) {
func (pa *PlatformAdapterDingTalk) SetGroupCardName(ctx *MsgContext, name string) {
}

func (pa *PlatformAdapterDingTalk) SendFileToPerson(ctx *MsgContext, uid string, path string, flag string) {
Expand Down
13 changes: 4 additions & 9 deletions dice/platform_adapter_discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,16 +440,11 @@ func (pa *PlatformAdapterDiscord) QuitGroup(_ *MsgContext, id string) {
}

// SetGroupCardName 没有改变用户在某个频道中昵称的功能,一旦更改就是整个服务器范围内都改
func (pa *PlatformAdapterDiscord) SetGroupCardName(groupID string, userID string, name string) {
ch, err := pa.IntentSession.Channel(ExtractDiscordChannelID(groupID))
func (pa *PlatformAdapterDiscord) SetGroupCardName(ctx *MsgContext, name string) {
guildID := ctx.Group.GuildID
err := pa.IntentSession.GuildMemberNickname(guildID, ExtractDiscordUserID(ctx.Player.UserID), name)
if err != nil {
pa.Session.Parent.Logger.Errorf("获取Discord频道#%s信息时出错:%s", groupID, err.Error())
return
}
guildID := ch.GuildID
err = pa.IntentSession.GuildMemberNickname(guildID, ExtractDiscordUserID(userID), name)
if err != nil {
pa.Session.Parent.Logger.Errorf("修改用户#%s在Discord服务器%s(来源频道#%s)的昵称时出错:%s", userID, guildID, groupID, err.Error())
pa.Session.Parent.Logger.Errorf("修改用户#%s在Discord服务器%s(来源频道#%s)的昵称时出错:%s", ctx.Player.UserID, guildID, ctx.Group.GroupID, err.Error())
}
}

Expand Down
20 changes: 18 additions & 2 deletions dice/platform_adapter_dodo.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,22 @@ func ExtractDodoGroupID(id string) string {
}
return id
}
func (pa *PlatformAdapterDodo) QuitGroup(_ *MsgContext, _ string) {}
func (pa *PlatformAdapterDodo) QuitGroup(ctx *MsgContext, groupId string) {
_, err := pa.Client.SetBotIslandLeave(context.Background(), &model.SetBotLeaveIslandReq{
IslandSourceId: ctx.Group.GuildID,
})
if err != nil {
pa.Session.Parent.Logger.Errorf("Dodo退群失败:%v", err)
}
}

func (pa *PlatformAdapterDodo) SetGroupCardName(_ string, _ string, _ string) {}
func (pa *PlatformAdapterDodo) SetGroupCardName(ctx *MsgContext, name string) {
_, err := pa.Client.SetMemberNick(context.Background(), &model.SetMemberNickReq{
IslandSourceId: ctx.Group.GuildID,
DodoSourceId: ExtractDodoUserID(ctx.Player.UserID),
NickName: name,
})
if err != nil {
pa.Session.Parent.Logger.Errorf("Dodo设置群名片失败:%v", err)
}
}
6 changes: 3 additions & 3 deletions dice/platform_adapter_gocq_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,12 +458,12 @@ func (pa *PlatformAdapterGocq) GetStrangerInfo(userID int64) *OnebotUserInfo {
}
}

func (pa *PlatformAdapterGocq) SetGroupCardName(groupID string, userID string, name string) {
groupIDRaw, idType := pa.mustExtractID(groupID)
func (pa *PlatformAdapterGocq) SetGroupCardName(ctx *MsgContext, name string) {
groupIDRaw, idType := pa.mustExtractID(ctx.Group.GroupID)
if idType != QQUidGroup {
return
}
userIDRaw, idType := pa.mustExtractID(userID)
userIDRaw, idType := pa.mustExtractID(ctx.Player.UserID)
if idType != QQUidPerson {
return
}
Expand Down
2 changes: 1 addition & 1 deletion dice/platform_adapter_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (pa *PlatformAdapterHTTP) SendFileToGroup(ctx *MsgContext, uid string, path

func (pa *PlatformAdapterHTTP) QuitGroup(_ *MsgContext, _ string) {}

func (pa *PlatformAdapterHTTP) SetGroupCardName(_ string, _ string, _ string) {}
func (pa *PlatformAdapterHTTP) SetGroupCardName(_ *MsgContext, _ string) {}

func (pa *PlatformAdapterHTTP) MemberBan(_ string, _ string, _ int64) {}

Expand Down
22 changes: 12 additions & 10 deletions dice/platform_adapter_kook.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,13 @@ func ExtractKookChannelID(id string) string {
return id
}

func ExtractKookGuildID(id string) string {
if strings.HasPrefix(id, "KOOK-Guild:") {
return id[len("KOOK-Guild:"):]
}
return id
}

func (pa *PlatformAdapterKook) QuitGroup(_ *MsgContext, groupID string) {
channel, err := pa.IntentSession.ChannelView(ExtractKookChannelID(groupID))
if err != nil {
Expand All @@ -653,19 +660,14 @@ func (pa *PlatformAdapterKook) QuitGroup(_ *MsgContext, groupID string) {
}
}

func (pa *PlatformAdapterKook) SetGroupCardName(groupID string, userID string, name string) {
func (pa *PlatformAdapterKook) SetGroupCardName(ctx *MsgContext, name string) {
nick := &kook.GuildNickname{}
channel, err := pa.IntentSession.ChannelView(ExtractKookChannelID(groupID))
if err != nil {
pa.Session.Parent.Logger.Errorf("获取Kook频道信息#%s时出错:%s", groupID, err.Error())
return
}
nick.GuildID = channel.GuildID
nick.GuildID = ExtractKookGuildID(ctx.Group.GuildID)
nick.Nickname = name
nick.UserID = ExtractKookUserID(userID)
err = pa.IntentSession.GuildNickname(nick)
nick.UserID = ExtractKookUserID(ctx.Player.UserID)
err := pa.IntentSession.GuildNickname(nick)
if err != nil {
pa.Session.Parent.Logger.Errorf("修改Kook用户#%s在服务器#%s(来源频道#%s)的昵称时出错:%s", userID, channel.GuildID, groupID, err.Error())
pa.Session.Parent.Logger.Errorf("修改Kook用户#%s在服务器#%s(来源频道#%s)的昵称时出错:%s", ctx.Player.UserID, ctx.Group.GuildID, ctx.Group.GroupID, err.Error())
return
}
}
Expand Down
2 changes: 1 addition & 1 deletion dice/platform_adapter_minecraft.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,4 @@ func (pa *PlatformAdapterMinecraft) MemberKick(_ string, _ string) {}

func (pa *PlatformAdapterMinecraft) QuitGroup(_ *MsgContext, _ string) {}

func (pa *PlatformAdapterMinecraft) SetGroupCardName(_ string, _ string, _ string) {}
func (pa *PlatformAdapterMinecraft) SetGroupCardName(_ *MsgContext, _ string) {}
2 changes: 1 addition & 1 deletion dice/platform_adapter_red.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ func (pa *PlatformAdapterRed) mustExtractId(id string) (int64, RedChatType) {
return 0, 0
}

func (pa *PlatformAdapterRed) SetGroupCardName(groupId string, userId string, name string) {
func (pa *PlatformAdapterRed) SetGroupCardName(ctx *MsgContext, name string) {
log := pa.Session.Parent.Logger
log.Warn("red: 尚未实现该功能")
}
Expand Down
2 changes: 1 addition & 1 deletion dice/platform_adapter_telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,4 +496,4 @@ func (pa *PlatformAdapterTelegram) QuitGroup(_ *MsgContext, id string) {
}

// SetGroupCardName 没有这个接口 不实现
func (pa *PlatformAdapterTelegram) SetGroupCardName(_ string, _ string, _ string) {}
func (pa *PlatformAdapterTelegram) SetGroupCardName(_ *MsgContext, _ string) {}
2 changes: 1 addition & 1 deletion dice/platform_adapter_walleq.go
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ func (pa *PlatformAdapterWalleQ) QuitGroup(_ *MsgContext, id string) {
socketSendText(pa.Socket, string(a))
}

func (pa *PlatformAdapterWalleQ) SetGroupCardName(_ string, _ string, _ string) {
func (pa *PlatformAdapterWalleQ) SetGroupCardName(_ *MsgContext, _ string) {
// wq 暂无该扩展 api
}

Expand Down

0 comments on commit 74cc86c

Please sign in to comment.