mirror of
https://github.com/diamondburned/cchat-discord.git
synced 2024-12-22 20:36:45 +00:00
added config
This commit is contained in:
parent
ef4cb53185
commit
73b0d3f39c
|
@ -3,6 +3,7 @@ package discord
|
||||||
import (
|
import (
|
||||||
"github.com/diamondburned/cchat"
|
"github.com/diamondburned/cchat"
|
||||||
"github.com/diamondburned/cchat-discord/internal/discord/authenticate"
|
"github.com/diamondburned/cchat-discord/internal/discord/authenticate"
|
||||||
|
"github.com/diamondburned/cchat-discord/internal/discord/config"
|
||||||
"github.com/diamondburned/cchat-discord/internal/discord/session"
|
"github.com/diamondburned/cchat-discord/internal/discord/session"
|
||||||
"github.com/diamondburned/cchat/services"
|
"github.com/diamondburned/cchat/services"
|
||||||
"github.com/diamondburned/cchat/text"
|
"github.com/diamondburned/cchat/text"
|
||||||
|
@ -34,3 +35,7 @@ func (Service) AsIconer() cchat.Iconer {
|
||||||
func (Service) AsSessionRestorer() cchat.SessionRestorer {
|
func (Service) AsSessionRestorer() cchat.SessionRestorer {
|
||||||
return session.Restorer
|
return session.Restorer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (Service) AsConfigurator() cchat.Configurator {
|
||||||
|
return config.World
|
||||||
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"github.com/diamondburned/cchat"
|
"github.com/diamondburned/cchat"
|
||||||
"github.com/diamondburned/cchat-discord/internal/discord/channel/shared"
|
"github.com/diamondburned/cchat-discord/internal/discord/channel/shared"
|
||||||
"github.com/diamondburned/cchat-discord/internal/discord/channel/typer"
|
"github.com/diamondburned/cchat-discord/internal/discord/channel/typer"
|
||||||
|
"github.com/diamondburned/cchat-discord/internal/discord/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TypingIndicator struct {
|
type TypingIndicator struct {
|
||||||
|
@ -18,6 +19,10 @@ func NewTyping(ch shared.Channel) cchat.TypingIndicator {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ti TypingIndicator) Typing() error {
|
func (ti TypingIndicator) Typing() error {
|
||||||
|
if !config.BroadcastTyping() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
return ti.State.Typing(ti.ID)
|
return ti.State.Typing(ti.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,15 +3,27 @@ package send
|
||||||
import (
|
import (
|
||||||
"github.com/diamondburned/arikawa/v2/api"
|
"github.com/diamondburned/arikawa/v2/api"
|
||||||
"github.com/diamondburned/arikawa/v2/discord"
|
"github.com/diamondburned/arikawa/v2/discord"
|
||||||
|
"github.com/diamondburned/arikawa/v2/utils/json/option"
|
||||||
"github.com/diamondburned/arikawa/v2/utils/sendpart"
|
"github.com/diamondburned/arikawa/v2/utils/sendpart"
|
||||||
"github.com/diamondburned/cchat"
|
"github.com/diamondburned/cchat"
|
||||||
"github.com/diamondburned/cchat-discord/internal/discord/channel/message/send/complete"
|
"github.com/diamondburned/cchat-discord/internal/discord/channel/message/send/complete"
|
||||||
"github.com/diamondburned/cchat-discord/internal/discord/channel/shared"
|
"github.com/diamondburned/cchat-discord/internal/discord/channel/shared"
|
||||||
|
"github.com/diamondburned/cchat-discord/internal/discord/config"
|
||||||
"github.com/diamondburned/cchat-discord/internal/discord/state"
|
"github.com/diamondburned/cchat-discord/internal/discord/state"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
allowAllMention = []api.AllowedMentionType{
|
||||||
|
api.AllowEveryoneMention,
|
||||||
|
api.AllowRoleMention,
|
||||||
|
api.AllowUserMention,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
func WrapMessage(s *state.Instance, msg cchat.SendableMessage) api.SendMessageData {
|
func WrapMessage(s *state.Instance, msg cchat.SendableMessage) api.SendMessageData {
|
||||||
var send = api.SendMessageData{Content: msg.Content()}
|
var send = api.SendMessageData{
|
||||||
|
Content: msg.Content(),
|
||||||
|
}
|
||||||
|
|
||||||
if attacher := msg.AsAttacher(); attacher != nil {
|
if attacher := msg.AsAttacher(); attacher != nil {
|
||||||
send.Files = addAttachments(attacher.Attachments())
|
send.Files = addAttachments(attacher.Attachments())
|
||||||
|
@ -23,10 +35,20 @@ func WrapMessage(s *state.Instance, msg cchat.SendableMessage) api.SendMessageDa
|
||||||
|
|
||||||
if replier := msg.AsReplier(); replier != nil {
|
if replier := msg.AsReplier(); replier != nil {
|
||||||
id, err := discord.ParseSnowflake(replier.ReplyingTo())
|
id, err := discord.ParseSnowflake(replier.ReplyingTo())
|
||||||
if err == nil {
|
if err != nil {
|
||||||
send.Reference = &discord.MessageReference{
|
return send
|
||||||
MessageID: discord.MessageID(id),
|
}
|
||||||
}
|
|
||||||
|
send.Reference = &discord.MessageReference{
|
||||||
|
MessageID: discord.MessageID(id),
|
||||||
|
}
|
||||||
|
send.AllowedMentions = &api.AllowedMentions{
|
||||||
|
Parse: allowAllMention,
|
||||||
|
RepliedUser: option.False,
|
||||||
|
}
|
||||||
|
|
||||||
|
if config.MentionOnReply() {
|
||||||
|
send.AllowedMentions.RepliedUser = option.True
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
119
internal/discord/config/config.go
Normal file
119
internal/discord/config/config.go
Normal file
|
@ -0,0 +1,119 @@
|
||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/diamondburned/cchat"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
var World = ®istry{
|
||||||
|
configs: []config{
|
||||||
|
{"Mention on Reply", true},
|
||||||
|
{"Broadcast Typing", true},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// MentionOnReply returns true if message replies should mention users.
|
||||||
|
func MentionOnReply() bool {
|
||||||
|
return World.get(0).(bool)
|
||||||
|
}
|
||||||
|
|
||||||
|
// BroadcastTyping returns true if typing events should be broadcasted.
|
||||||
|
func BroadcastTyping() bool {
|
||||||
|
return World.get(1).(bool)
|
||||||
|
}
|
||||||
|
|
||||||
|
type config struct {
|
||||||
|
Name string
|
||||||
|
Value interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c config) Marshal(dst map[string]string) error {
|
||||||
|
switch v := c.Value.(type) {
|
||||||
|
case bool:
|
||||||
|
dst[c.Name] = strconv.FormatBool(v)
|
||||||
|
case string:
|
||||||
|
dst[c.Name] = v
|
||||||
|
default:
|
||||||
|
return cchat.ErrInvalidConfigAtField{
|
||||||
|
Key: c.Name,
|
||||||
|
Err: fmt.Errorf("unknown type %T", c.Value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *config) Unmarshal(src map[string]string) (err error) {
|
||||||
|
strVal, ok := src[c.Name]
|
||||||
|
if !ok {
|
||||||
|
return cchat.ErrInvalidConfigAtField{
|
||||||
|
Key: c.Name, Err: errors.New("missing field"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var v interface{}
|
||||||
|
|
||||||
|
switch c.Value.(type) {
|
||||||
|
case bool:
|
||||||
|
v, err = strconv.ParseBool(strVal)
|
||||||
|
case string:
|
||||||
|
v = strVal
|
||||||
|
default:
|
||||||
|
err = fmt.Errorf("unknown type %T", c.Value)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return cchat.ErrInvalidConfigAtField{
|
||||||
|
Key: c.Name,
|
||||||
|
Err: err,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Value = v
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type registry struct {
|
||||||
|
mutex sync.RWMutex
|
||||||
|
configs []config
|
||||||
|
}
|
||||||
|
|
||||||
|
func (reg *registry) get(i int) interface{} {
|
||||||
|
reg.mutex.RLock()
|
||||||
|
defer reg.mutex.RUnlock()
|
||||||
|
|
||||||
|
return reg.configs[i].Value
|
||||||
|
}
|
||||||
|
|
||||||
|
func (reg *registry) Configuration() (map[string]string, error) {
|
||||||
|
reg.mutex.RLock()
|
||||||
|
defer reg.mutex.RUnlock()
|
||||||
|
|
||||||
|
var configMap = map[string]string{}
|
||||||
|
|
||||||
|
for _, config := range reg.configs {
|
||||||
|
if err := config.Marshal(configMap); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return configMap, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (reg *registry) SetConfiguration(cfgMap map[string]string) error {
|
||||||
|
reg.mutex.Lock()
|
||||||
|
defer reg.mutex.Unlock()
|
||||||
|
|
||||||
|
for i := range reg.configs {
|
||||||
|
// reference the config inside the slice
|
||||||
|
if err := reg.configs[i].Unmarshal(cfgMap); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
Loading…
Reference in a new issue