diff --git a/ui/app.go b/ui/app.go index 11267b1..f904655 100644 --- a/ui/app.go +++ b/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 { diff --git a/ui/handlers.go b/ui/handlers.go index c8dad61..142b9ec 100644 --- a/ui/handlers.go +++ b/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 { diff --git a/ui/views.go b/ui/views.go index 9ef1477..6cb091c 100644 --- a/ui/views.go +++ b/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)