commands.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package bridge
  2. import (
  3. "fmt"
  4. "github.com/alecthomas/kong"
  5. "maunium.net/go/mautrix/appservice"
  6. "maunium.net/go/mautrix/event"
  7. "maunium.net/go/mautrix/format"
  8. "maunium.net/go/mautrix/id"
  9. "gitlab.com/beeper/discord/consts"
  10. "gitlab.com/beeper/discord/version"
  11. )
  12. type globals struct {
  13. context *kong.Context
  14. bridge *Bridge
  15. bot *appservice.IntentAPI
  16. portal *Portal
  17. handler *commandHandler
  18. roomID id.RoomID
  19. user *User
  20. replyTo id.EventID
  21. }
  22. func (g *globals) reply(msg string) {
  23. content := format.RenderMarkdown(msg, true, false)
  24. content.MsgType = event.MsgNotice
  25. intent := g.bot
  26. if g.portal != nil && g.portal.IsPrivateChat() {
  27. intent = g.portal.MainIntent()
  28. }
  29. _, err := intent.SendMessageEvent(g.roomID, event.EventMessage, content)
  30. if err != nil {
  31. g.handler.log.Warnfln("Failed to reply to command from %q: %v", g.user.MXID, err)
  32. }
  33. }
  34. type commands struct {
  35. globals
  36. Help helpCmd `kong:"cmd,help='Displays this message.'"`
  37. Version versionCmd `kong:"cmd,help='Displays the version of the bridge.'"`
  38. }
  39. type helpCmd struct {
  40. Command []string `kong:"arg,optional,help='The command to get help on.'"`
  41. }
  42. func (c *helpCmd) Run(g *globals) error {
  43. ctx, err := kong.Trace(g.context.Kong, c.Command)
  44. if err != nil {
  45. return err
  46. }
  47. if ctx.Error != nil {
  48. return err
  49. }
  50. err = ctx.PrintUsage(true)
  51. if err != nil {
  52. return err
  53. }
  54. fmt.Fprintln(g.context.Stdout)
  55. return nil
  56. }
  57. type versionCmd struct{}
  58. func (c *versionCmd) Run(g *globals) error {
  59. fmt.Fprintln(g.context.Stdout, consts.Name, version.String)
  60. return nil
  61. }