Skip to content
This repository was archived by the owner on Feb 22, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"net/http"

"github.com/bwmarrin/discordgo"
"go.mongodb.org/mongo-driver/bson/primitive"
)

Expand Down Expand Up @@ -35,21 +36,33 @@ func (req Request[T]) ToRaw() Request[json.RawMessage] {
type OperationName string

const (
OperationNameSyncUser = "SYNC_USER"
OperationNameSyncUser = "SYNC_USER"
OperationNameSendMessage = "SEND_MESSAGE"
)

type (
MessageSend = discordgo.MessageSend
)

type RequestPayload interface {
json.RawMessage | RequestPayloadSyncUser
json.RawMessage | RequestPayloadSyncUser | RequestPayloadSendMessage
}

type RequestPayloadSyncUser struct {
UserID primitive.ObjectID `json:"user_id"`
Revoke bool `json:"revoke"`
}

type RequestPayloadSendMessage struct {
Channel string `json:"channel"`
Message MessageSend `json:"message"`
Webhook bool `json:"webhook"`
}

type Instance interface {
SyncUser(userID primitive.ObjectID) (*http.Response, error)
RevokeUser(userID primitive.ObjectID) (*http.Response, error)
SendMessage(channel string, message MessageSend, webhook bool) (*http.Response, error)
}

type cdInst struct {
Expand Down Expand Up @@ -94,3 +107,15 @@ func (inst *cdInst) RevokeUser(userID primitive.ObjectID) (*http.Response, error
},
}.ToRaw())
}

// SendMessage implements Instance
func (inst *cdInst) SendMessage(channel string, message discordgo.MessageSend, webhook bool) (*http.Response, error) {
return inst.request(Request[RequestPayloadSendMessage]{
Operation: OperationNameSendMessage,
Data: RequestPayloadSendMessage{
Channel: channel,
Message: message,
Webhook: webhook,
},
}.ToRaw())
}
6 changes: 4 additions & 2 deletions internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/fasthttp/router"
"github.com/seventv/common/utils"
"github.com/seventv/compactdisc"
"github.com/seventv/compactdisc/internal/api/handler"
"github.com/seventv/compactdisc/internal/api/commands"
"github.com/seventv/compactdisc/internal/global"
"github.com/valyala/fasthttp"
"go.uber.org/zap"
Expand Down Expand Up @@ -44,7 +44,9 @@ func Start(gctx global.Context) (<-chan uint8, error) {

switch body.Operation {
case compactdisc.OperationNameSyncUser:
err = handler.SyncUser(gctx, ctx, compactdisc.ConvertRequest[compactdisc.RequestPayloadSyncUser](body))
err = commands.SyncUser(gctx, ctx, compactdisc.ConvertRequest[compactdisc.RequestPayloadSyncUser](body))
case compactdisc.OperationNameSendMessage:
err = commands.SendMessage(gctx, ctx, compactdisc.ConvertRequest[compactdisc.RequestPayloadSendMessage](body))
}

if err != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/api/commands/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package commands
59 changes: 59 additions & 0 deletions internal/api/commands/post_message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package commands

import (
"context"

"github.com/bwmarrin/discordgo"
"github.com/seventv/compactdisc"
"github.com/seventv/compactdisc/internal/global"
"go.uber.org/zap"
)

func SendMessage(gctx global.Context, ctx context.Context, req compactdisc.Request[compactdisc.RequestPayloadSendMessage]) error {
channelID, ok := gctx.Config().Discord.Channels[req.Data.Channel]
if !ok {
return nil
}

var webhook *discordgo.Webhook

if req.Data.Webhook {
hooks, _ := gctx.Inst().Discord.Session().ChannelWebhooks(channelID)
if len(hooks) > 0 {
webhook = hooks[0]
}
}

z := zap.S().Named("api/SendMessage").With(
"channel_id", channelID,
)

var (
msg *discordgo.Message
err error
)

if webhook != nil {
msg, err = gctx.Inst().Discord.Session().WebhookExecute(webhook.ID, webhook.Token, false, &discordgo.WebhookParams{
Content: req.Data.Message.Content,
Username: gctx.Inst().Discord.Identity().Username,
AvatarURL: gctx.Inst().Discord.Identity().AvatarURL("128"),
TTS: req.Data.Message.TTS,
Files: req.Data.Message.Files,
Components: req.Data.Message.Components,
Embeds: req.Data.Message.Embeds,
AllowedMentions: req.Data.Message.AllowedMentions,
})
} else {
msg, err = gctx.Inst().Discord.Session().ChannelMessageSendComplex(channelID, &req.Data.Message)
}

if err != nil {
z.Errorw("failed to send message", "error", err)
return err
}

z.With("message_id", msg.ID).Info("message sent")

return nil
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package handler
package commands

import (
"context"
Expand Down
1 change: 0 additions & 1 deletion internal/api/handler/handler.go

This file was deleted.

7 changes: 4 additions & 3 deletions internal/configure/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,10 @@ type Config struct {
} `mapstructure:"k8s" json:"k8s"`

Discord struct {
GuildID string `mapstructure:"guild_id" json:"guild_id"`
DefaultRoleId string `mapstructure:"default_role_id" json:"default_role_id"`
Token string `mapstructure:"token" json:"token"`
GuildID string `mapstructure:"guild_id" json:"guild_id"`
DefaultRoleId string `mapstructure:"default_role_id" json:"default_role_id"`
Token string `mapstructure:"token" json:"token"`
Channels map[string]string `mapstructure:"channels" json:"channels"`
} `mapstructure:"discord" json:"discord"`

Redis struct {
Expand Down
4 changes: 4 additions & 0 deletions k8s/production.template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ data:
addr: "0.0.0.0"
port: 3000

discord:
channels:
activity_feed: "817375925271527449"

health:
enabled: true
bind: 0.0.0.0:9200
4 changes: 4 additions & 0 deletions k8s/staging.template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ data:
addr: "0.0.0.0"
port: 3000

discord:
channels:
activity_feed: "817375925271527449"

health:
enabled: true
bind: 0.0.0.0:9200