discordo.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. package main
  2. import (
  3. "fmt"
  4. "sort"
  5. "strings"
  6. "github.com/atotto/clipboard"
  7. "github.com/bwmarrin/discordgo"
  8. "github.com/gdamore/tcell/v2"
  9. "github.com/gen2brain/beeep"
  10. "github.com/rigormorrtiss/discordo/ui"
  11. "github.com/rigormorrtiss/discordo/util"
  12. "github.com/rivo/tview"
  13. "github.com/zalando/go-keyring"
  14. )
  15. var (
  16. app *tview.Application
  17. loginWidget *tview.Form
  18. guildsWidget *tview.TreeView
  19. messagesWidget *tview.TextView
  20. inputWidget *tview.InputField
  21. mainFlex *tview.Flex
  22. config *util.Config
  23. session *discordgo.Session
  24. selectedChannel *discordgo.Channel
  25. selectedMessage *discordgo.Message
  26. )
  27. func main() {
  28. config = util.NewConfig()
  29. tview.Styles = config.Theme
  30. app = tview.NewApplication()
  31. app.EnableMouse(config.Mouse)
  32. app.SetInputCapture(onAppInputCapture)
  33. guildsWidget = ui.NewGuildsWidget()
  34. guildsWidget.SetSelectedFunc(onGuildsWidgetSelected)
  35. messagesWidget = ui.NewMessagesWidget(app)
  36. messagesWidget.SetInputCapture(onMessagesWidgetInputCapture)
  37. inputWidget = ui.NewInputWidget()
  38. inputWidget.SetInputCapture(onInputWidgetInputCapture)
  39. mainFlex = ui.NewMainFlex(
  40. guildsWidget,
  41. messagesWidget,
  42. inputWidget,
  43. )
  44. token := config.Token
  45. if t, _ := keyring.Get("discordo", "token"); t != "" {
  46. token = t
  47. }
  48. if token != "" {
  49. app.
  50. SetRoot(mainFlex, true).
  51. SetFocus(guildsWidget)
  52. session = newSession()
  53. session.Token = token
  54. session.Identify.Token = token
  55. if err := session.Open(); err != nil {
  56. panic(err)
  57. }
  58. } else {
  59. loginWidget = ui.NewLoginWidget(onLoginFormLoginButtonSelected, false)
  60. app.SetRoot(loginWidget, true)
  61. }
  62. if err := app.Run(); err != nil {
  63. panic(err)
  64. }
  65. }
  66. func onAppInputCapture(e *tcell.EventKey) *tcell.EventKey {
  67. if e.Modifiers() == tcell.ModAlt {
  68. switch e.Rune() {
  69. case '1':
  70. app.SetFocus(guildsWidget)
  71. case '2':
  72. app.SetFocus(messagesWidget)
  73. case '3':
  74. app.SetFocus(inputWidget)
  75. }
  76. }
  77. return e
  78. }
  79. func findByMessageID(ms []*discordgo.Message, mID string) (int, *discordgo.Message) {
  80. for i, m := range ms {
  81. if mID == m.ID {
  82. return i, m
  83. }
  84. }
  85. return -1, nil
  86. }
  87. func onMessagesWidgetInputCapture(e *tcell.EventKey) *tcell.EventKey {
  88. if selectedChannel == nil {
  89. return nil
  90. }
  91. switch {
  92. case e.Key() == tcell.KeyUp || e.Rune() == 'k': // Up
  93. ms := selectedChannel.Messages
  94. if len(ms) == 0 {
  95. return nil
  96. }
  97. hs := messagesWidget.GetHighlights()
  98. // If there are no currently highlighted message, highlight the last
  99. // message in the TextView.
  100. if len(hs) == 0 {
  101. messagesWidget.
  102. Highlight(ms[len(ms)-1].ID).
  103. ScrollToHighlight()
  104. } else {
  105. // Find the index of the currently highlighted message in the
  106. // *discordgo.Channel.Messages slice.
  107. idx, _ := findByMessageID(ms, hs[0])
  108. // If the index of the currently highlighted message is equal to
  109. // zero
  110. // (first message in the TextView), do not handle the event.
  111. if idx == -1 || idx == 0 {
  112. return nil
  113. }
  114. // Highlight the message just before the currently highlighted
  115. // message.
  116. messagesWidget.
  117. Highlight(ms[idx-1].ID).
  118. ScrollToHighlight()
  119. }
  120. return nil
  121. case e.Key() == tcell.KeyDown || e.Rune() == 'j': // Down
  122. ms := selectedChannel.Messages
  123. if len(ms) == 0 {
  124. return nil
  125. }
  126. hs := messagesWidget.GetHighlights()
  127. // If there are no currently highlighted message, highlight the last
  128. // message in the TextView.
  129. if len(hs) == 0 {
  130. messagesWidget.
  131. Highlight(ms[len(ms)-1].ID).
  132. ScrollToHighlight()
  133. } else {
  134. // Find the index of the highlighted message in the
  135. // *discordgo.Channel.Messages slice.
  136. idx, _ := findByMessageID(ms, hs[0])
  137. // If the index of the currently highlighted message is equal to the
  138. // total number of elements in the *discordgo.Channel.Messages
  139. // slice, do not handle the event.
  140. if idx == -1 || idx == len(ms)-1 {
  141. return nil
  142. }
  143. // Highlight the message just after the currently highlighted
  144. // message.
  145. messagesWidget.
  146. Highlight(ms[idx+1].ID).
  147. ScrollToHighlight()
  148. }
  149. return nil
  150. case e.Key() == tcell.KeyHome || e.Rune() == 'g': // Top
  151. ms := selectedChannel.Messages
  152. if len(ms) == 0 {
  153. return nil
  154. }
  155. // Highlight the last message in the selectedChannel.Messages slice
  156. // (the first message rendered in the TextView).
  157. messagesWidget.
  158. Highlight(ms[0].ID).
  159. ScrollToHighlight()
  160. case e.Key() == tcell.KeyEnd || e.Rune() == 'G': // Bottom
  161. ms := selectedChannel.Messages
  162. if len(ms) == 0 {
  163. return nil
  164. }
  165. // Highlight the first message in the selectedChannel.Messages slice
  166. // (the last message rendered in the TextView).
  167. messagesWidget.
  168. Highlight(ms[len(ms)-1].ID).
  169. ScrollToHighlight()
  170. case e.Rune() == 'r': // Reply
  171. ms := selectedChannel.Messages
  172. if len(ms) == 0 {
  173. return nil
  174. }
  175. hs := messagesWidget.GetHighlights()
  176. if len(hs) == 0 {
  177. return nil
  178. }
  179. _, selectedMessage = findByMessageID(ms, hs[0])
  180. inputWidget.SetTitle(
  181. "Replying to " + selectedMessage.Author.Username,
  182. )
  183. app.SetFocus(inputWidget)
  184. }
  185. return e
  186. }
  187. func onInputWidgetInputCapture(e *tcell.EventKey) *tcell.EventKey {
  188. // If the "Alt" modifier key is pressed, do not handle the event.
  189. if e.Modifiers() == tcell.ModAlt {
  190. return nil
  191. }
  192. switch e.Key() {
  193. case tcell.KeyEnter:
  194. if selectedChannel == nil {
  195. return nil
  196. }
  197. t := strings.TrimSpace(inputWidget.GetText())
  198. if t == "" {
  199. return nil
  200. }
  201. if selectedMessage != nil {
  202. inputWidget.SetTitle("")
  203. go session.ChannelMessageSendReply(
  204. selectedMessage.ChannelID,
  205. t,
  206. selectedMessage.Reference(),
  207. )
  208. selectedMessage = nil
  209. } else {
  210. go session.ChannelMessageSend(selectedChannel.ID, t)
  211. }
  212. inputWidget.SetText("")
  213. case tcell.KeyCtrlV:
  214. text, _ := clipboard.ReadAll()
  215. text = inputWidget.GetText() + text
  216. inputWidget.SetText(text)
  217. case tcell.KeyEscape: // Cancel
  218. inputWidget.SetTitle("")
  219. selectedMessage = nil
  220. }
  221. return e
  222. }
  223. func newSession() *discordgo.Session {
  224. s, err := discordgo.New()
  225. if err != nil {
  226. panic(err)
  227. }
  228. s.UserAgent = config.UserAgent
  229. s.Identify.Compress = false
  230. s.Identify.Intents = 0
  231. s.Identify.LargeThreshold = 0
  232. s.Identify.Properties.Device = ""
  233. s.Identify.Properties.Browser = "Chrome"
  234. s.Identify.Properties.OS = "Linux"
  235. s.AddHandlerOnce(onSessionReady)
  236. s.AddHandler(onSessionMessageCreate)
  237. return s
  238. }
  239. func onSessionReady(_ *discordgo.Session, r *discordgo.Ready) {
  240. sort.Slice(r.Guilds, func(a, b int) bool {
  241. found := false
  242. for _, gID := range r.Settings.GuildPositions {
  243. if found {
  244. if gID == r.Guilds[b].ID {
  245. return true
  246. }
  247. } else {
  248. if gID == r.Guilds[a].ID {
  249. found = true
  250. }
  251. }
  252. }
  253. return false
  254. })
  255. n := guildsWidget.GetRoot()
  256. for _, g := range r.Guilds {
  257. gn := tview.NewTreeNode(g.Name).
  258. SetReference(g.ID)
  259. n.AddChild(gn)
  260. }
  261. guildsWidget.SetCurrentNode(n)
  262. }
  263. func onSessionMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
  264. if selectedChannel == nil {
  265. selectedChannel = &discordgo.Channel{ID: ""}
  266. }
  267. if selectedChannel.ID != m.ChannelID {
  268. if config.Notifications {
  269. for _, u := range m.Mentions {
  270. // If the client user account is mentioned in the message content, send a desktop notification with details.
  271. if u.ID == s.State.User.ID {
  272. g, err := s.State.Guild(m.GuildID)
  273. if err != nil {
  274. return
  275. }
  276. c, err := s.State.Channel(m.ChannelID)
  277. if err != nil {
  278. return
  279. }
  280. go beeep.Alert(fmt.Sprintf("%s (#%s)", g.Name, c.Name), m.ContentWithMentionsReplaced(), "")
  281. return
  282. }
  283. }
  284. }
  285. return
  286. }
  287. selectedChannel.Messages = append(selectedChannel.Messages, m.Message)
  288. util.WriteMessage(
  289. messagesWidget,
  290. m.Message,
  291. session.State.Ready.User.ID,
  292. )
  293. }
  294. func onGuildsWidgetSelected(n *tview.TreeNode) {
  295. selectedChannel = nil
  296. selectedMessage = nil
  297. messagesWidget.
  298. Clear().
  299. SetTitle("")
  300. switch n.GetLevel() {
  301. case 1:
  302. if len(n.GetChildren()) != 0 {
  303. n.SetExpanded(!n.IsExpanded())
  304. return
  305. }
  306. n.ClearChildren()
  307. gID := n.GetReference().(string)
  308. g, _ := session.State.Guild(gID)
  309. cs := g.Channels
  310. sort.Slice(cs, func(i, j int) bool {
  311. return cs[i].Position < cs[j].Position
  312. })
  313. // Top-level channels
  314. ui.CreateTopLevelChannelsTreeNodes(session.State, n, cs)
  315. // Category channels
  316. ui.CreateCategoryChannelsTreeNodes(session.State, n, cs)
  317. // Second-level channels
  318. ui.CreateSecondLevelChannelsTreeNodes(session.State, guildsWidget, cs)
  319. default:
  320. cID := n.GetReference().(string)
  321. c, _ := session.State.Channel(cID)
  322. if c.Type == discordgo.ChannelTypeGuildCategory {
  323. n.SetExpanded(!n.IsExpanded())
  324. } else if c.Type == discordgo.ChannelTypeGuildNews || c.Type == discordgo.ChannelTypeGuildText {
  325. selectedChannel = c
  326. app.SetFocus(inputWidget)
  327. title := "#" + c.Name
  328. if c.Topic != "" {
  329. title += " - " + c.Topic
  330. }
  331. messagesWidget.
  332. Clear().
  333. SetTitle(title)
  334. go writeMessages(c.ID)
  335. }
  336. }
  337. }
  338. func writeMessages(cID string) {
  339. msgs, _ := session.ChannelMessages(cID, config.GetMessagesLimit, "", "", "")
  340. for i := len(msgs) - 1; i >= 0; i-- {
  341. selectedChannel.Messages = append(selectedChannel.Messages, msgs[i])
  342. util.WriteMessage(
  343. messagesWidget,
  344. msgs[i],
  345. session.State.Ready.User.ID,
  346. )
  347. }
  348. }
  349. func onLoginFormLoginButtonSelected() {
  350. email := loginWidget.GetFormItem(0).(*tview.InputField).GetText()
  351. password := loginWidget.GetFormItem(1).(*tview.InputField).GetText()
  352. if email == "" || password == "" {
  353. return
  354. }
  355. session = newSession()
  356. // Try to login without TOTP
  357. lr, err := util.Login(session, email, password)
  358. if err != nil {
  359. panic(err)
  360. }
  361. if lr.Token != "" && !lr.MFA {
  362. app.
  363. SetRoot(mainFlex, true).
  364. SetFocus(guildsWidget)
  365. session.Token = lr.Token
  366. session.Identify.Token = lr.Token
  367. if err = session.Open(); err != nil {
  368. panic(err)
  369. }
  370. go keyring.Set("discordo", "token", lr.Token)
  371. } else if lr.MFA {
  372. loginWidget = ui.NewLoginWidget(func() {
  373. code := loginWidget.GetFormItem(0).(*tview.InputField).GetText()
  374. if code == "" {
  375. return
  376. }
  377. lr, err = util.TOTP(session, code, lr.Ticket)
  378. if err != nil {
  379. panic(err)
  380. }
  381. app.
  382. SetRoot(mainFlex, true).
  383. SetFocus(guildsWidget)
  384. session.Token = lr.Token
  385. session.Identify.Token = lr.Token
  386. if err = session.Open(); err != nil {
  387. panic(err)
  388. }
  389. go keyring.Set("discordo", "token", lr.Token)
  390. }, true)
  391. app.SetRoot(loginWidget, true)
  392. }
  393. }