Pārlūkot izejas kodu

ui: add GUILD_DELETE event handler

ayntgl 4 gadi atpakaļ
vecāks
revīzija
0fba737c12
2 mainītis faili ar 12 papildinājumiem un 0 dzēšanām
  1. 11 0
      ui/app.go
  2. 1 0
      ui/guilds.go

+ 11 - 0
ui/app.go

@@ -59,6 +59,7 @@ func (app *App) Connect() error {
 	}
 
 	app.Session.AddHandler(app.onSessionGuildCreate)
+	app.Session.AddHandler(app.onSessionGuildDelete)
 	app.Session.AddHandler(app.onSessionMessageCreate)
 	return app.Session.Open()
 }
@@ -126,6 +127,16 @@ func (app *App) onSessionReady(_ *astatine.Session, r *astatine.Ready) {
 
 func (app *App) onSessionGuildCreate(_ *astatine.Session, g *astatine.GuildCreate) {
 	app.GuildsList.AddItem(g.Name, "", 0, nil)
+	app.Draw()
+}
+
+func (app *App) onSessionGuildDelete(_ *astatine.Session, g *astatine.GuildDelete) {
+	items := app.GuildsList.FindItems(g.BeforeDelete.Name, "", false, false)
+	if len(items) != 0 {
+		app.GuildsList.RemoveItem(items[0])
+	}
+
+	app.Draw()
 }
 
 func (app *App) onSessionMessageCreate(_ *astatine.Session, m *astatine.MessageCreate) {

+ 1 - 0
ui/guilds.go

@@ -50,6 +50,7 @@ func (gl *GuildsList) onSelected(idx int, mainText string, secondaryText string,
 			rootTreeNode.AddChild(channelTreeNode)
 		}
 	} else { // Guild
+		// Decrement the index of the selected item by one since the first item in the list is always "Direct Messages".
 		cs := gl.app.Session.State.Guilds[idx-1].Channels
 		sort.Slice(cs, func(i, j int) bool {
 			return cs[i].Position < cs[j].Position