1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-discord.git synced 2024-12-22 12:26:52 +00:00

added config

This commit is contained in:
diamondburned 2021-01-01 20:07:11 -08:00
parent ef4cb53185
commit 73b0d3f39c
4 changed files with 156 additions and 5 deletions

View file

@ -3,6 +3,7 @@ package discord
import (
"github.com/diamondburned/cchat"
"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/services"
"github.com/diamondburned/cchat/text"
@ -34,3 +35,7 @@ func (Service) AsIconer() cchat.Iconer {
func (Service) AsSessionRestorer() cchat.SessionRestorer {
return session.Restorer
}
func (Service) AsConfigurator() cchat.Configurator {
return config.World
}

View file

@ -7,6 +7,7 @@ import (
"github.com/diamondburned/cchat"
"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/config"
)
type TypingIndicator struct {
@ -18,6 +19,10 @@ func NewTyping(ch shared.Channel) cchat.TypingIndicator {
}
func (ti TypingIndicator) Typing() error {
if !config.BroadcastTyping() {
return nil
}
return ti.State.Typing(ti.ID)
}

View file

@ -3,15 +3,27 @@ package send
import (
"github.com/diamondburned/arikawa/v2/api"
"github.com/diamondburned/arikawa/v2/discord"
"github.com/diamondburned/arikawa/v2/utils/json/option"
"github.com/diamondburned/arikawa/v2/utils/sendpart"
"github.com/diamondburned/cchat"
"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/config"
"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 {
var send = api.SendMessageData{Content: msg.Content()}
var send = api.SendMessageData{
Content: msg.Content(),
}
if attacher := msg.AsAttacher(); attacher != nil {
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 {
id, err := discord.ParseSnowflake(replier.ReplyingTo())
if err == nil {
if err != nil {
return send
}
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
}
}

View file

@ -0,0 +1,119 @@
package config
import (
"fmt"
"strconv"
"sync"
"github.com/diamondburned/cchat"
"github.com/pkg/errors"
)
var World = &registry{
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
}