소스 검색

ui: support for bot accounts (prefix token with 'Bot ')

ayntgl 4 년 전
부모
커밋
cd90bbadaf
3개의 변경된 파일31개의 추가작업 그리고 17개의 파일을 삭제
  1. 25 14
      ui/app.go
  2. 5 0
      ui/handlers.go
  3. 1 3
      ui/views.go

+ 25 - 14
ui/app.go

@@ -3,6 +3,7 @@ package ui
 import (
 	"fmt"
 	"sort"
+	"strings"
 
 	"github.com/ayntgl/discordgo"
 	"github.com/ayntgl/discordo/config"
@@ -27,6 +28,7 @@ func NewApp() *App {
 	s, _ := discordgo.New()
 	return &App{
 		Application:       tview.NewApplication(),
+		MainFlex:          tview.NewFlex(),
 		GuildsList:        tview.NewList(),
 		ChannelsTreeView:  tview.NewTreeView(),
 		MessagesTextView:  tview.NewTextView(),
@@ -39,27 +41,32 @@ func NewApp() *App {
 }
 
 func (app *App) Connect(token string) error {
-	app.Session.Token = token
-	app.Session.UserAgent = app.Config.General.UserAgent
-
-	app.Session.Identify = discordgo.Identify{
-		Token:          token,
-		Compress:       false,
-		LargeThreshold: 0,
-		Intents:        0,
-		Properties: discordgo.IdentifyProperties{
-			OS:      "Linux",
-			Browser: "Firefox",
-			Device:  "",
-		},
+	if !strings.HasPrefix(token, "Bot") {
+		app.Session.UserAgent = app.Config.General.UserAgent
+		app.Session.Identify = discordgo.Identify{
+			Compress:       false,
+			LargeThreshold: 0,
+			Intents:        0,
+			Properties: discordgo.IdentifyProperties{
+				OS:      "Linux",
+				Browser: "Firefox",
+				Device:  "",
+			},
+		}
+		app.Session.AddHandlerOnce(app.onSessionReady)
 	}
-	app.Session.AddHandlerOnce(app.onSessionReady)
+
+	app.Session.Token = token
+	app.Session.Identify.Token = token
+	app.Session.AddHandler(app.onGuildCreate)
 	app.Session.AddHandler(app.onSessionMessageCreate)
 
 	return app.Session.Open()
 }
 
 func (app *App) onSessionReady(_ *discordgo.Session, r *discordgo.Ready) {
+	app.GuildsList.AddItem("Direct Messages", "", 0, nil)
+
 	sort.Slice(r.Guilds, func(a, b int) bool {
 		found := false
 		for _, guildID := range r.Settings.GuildPositions {
@@ -79,6 +86,10 @@ func (app *App) onSessionReady(_ *discordgo.Session, r *discordgo.Ready) {
 	}
 }
 
+func (app *App) onGuildCreate(_ *discordgo.Session, g *discordgo.GuildCreate) {
+	app.GuildsList.AddItem(g.Name, "", 0, nil)
+}
+
 func (app *App) onSessionMessageCreate(_ *discordgo.Session, m *discordgo.MessageCreate) {
 	if app.SelectedChannel == nil || app.SelectedChannel.ID != m.ChannelID {
 		if app.Config.General.Notifications {

+ 5 - 0
ui/handlers.go

@@ -39,6 +39,11 @@ func onGuildsListSelected(app *App, guildIdx int) {
 		SetTitle("")
 	app.MessageInputField.SetText("")
 
+	// If the user is a bot account, the direct messages item does not exist in the guilds list.
+	if app.Session.State.User.Bot && guildIdx == 0 {
+		guildIdx = 1
+	}
+
 	if guildIdx == 0 { // Direct Messages
 		cs := app.Session.State.PrivateChannels
 		sort.Slice(cs, func(i, j int) bool {

+ 1 - 3
ui/views.go

@@ -12,7 +12,6 @@ func NewMainFlex(app *App) *tview.Flex {
 
 	app.GuildsList.
 		ShowSecondaryText(false).
-		AddItem("Direct Messages", "", 0, nil).
 		SetSelectedFunc(func(guildIdx int, _ string, _ string, _ rune) {
 			onGuildsListSelected(app, guildIdx)
 		}).
@@ -62,8 +61,7 @@ func NewMainFlex(app *App) *tview.Flex {
 		SetDirection(tview.FlexRow).
 		AddItem(app.MessagesTextView, 0, 1, false).
 		AddItem(app.MessageInputField, 3, 1, false)
-
-	app.MainFlex = tview.NewFlex().
+	app.MainFlex.
 		AddItem(leftFlex, 0, 1, false).
 		AddItem(rightFlex, 0, 4, false)