Files
mautrix-discord/registration/cmd.go
2021-11-18 02:44:21 -06:00

60 lines
1.4 KiB
Go

package registration
import (
"fmt"
"regexp"
"maunium.net/go/mautrix/appservice"
"gitlab.com/beeper/discord/config"
"gitlab.com/beeper/discord/globals"
)
type Cmd struct {
Filename string `kong:"flag,help='The filename to store the registration into',name='REGISTRATION',short='r',default='registration.yaml'"`
}
func (c *Cmd) Run(g *globals.Globals) error {
cfg, err := config.FromFile(g.Config)
if err != nil {
return err
}
registration := appservice.CreateRegistration()
// Load existing values from the config into the registration.
if err := cfg.CopyToRegistration(registration); err != nil {
return err
}
// Save the new App and Server tokens in the config.
cfg.Appservice.ASToken = registration.AppToken
cfg.Appservice.HSToken = registration.ServerToken
// Workaround for https://github.com/matrix-org/synapse/pull/5758
registration.SenderLocalpart = appservice.RandomString(32)
// Register the bot's user.
pattern := fmt.Sprintf(
"^@%s:%s$",
cfg.Appservice.Bot.Username,
cfg.Homeserver.Domain,
)
botRegex, err := regexp.Compile(pattern)
if err != nil {
return err
}
registration.Namespaces.RegisterUserIDs(botRegex, true)
// Finally save the registration and the updated config file.
if err := registration.Save(c.Filename); err != nil {
return err
}
if err := cfg.Save(g.Config); err != nil {
return err
}
return nil
}