Add support for running Discord bot commands. Fixes #35
This commit is contained in:
20
user.go
20
user.go
@@ -59,6 +59,9 @@ type User struct {
|
||||
markedOpened map[string]time.Time
|
||||
markedOpenedLock sync.Mutex
|
||||
|
||||
pendingInteractions map[string]*WrappedCommandEvent
|
||||
pendingInteractionsLock sync.Mutex
|
||||
|
||||
nextDiscordUploadID atomic.Int32
|
||||
}
|
||||
|
||||
@@ -197,6 +200,8 @@ func (br *DiscordBridge) NewUser(dbUser *database.User) *User {
|
||||
|
||||
markedOpened: make(map[string]time.Time),
|
||||
PermissionLevel: br.Config.Bridge.Permissions.Get(dbUser.MXID),
|
||||
|
||||
pendingInteractions: make(map[string]*WrappedCommandEvent),
|
||||
}
|
||||
user.nextDiscordUploadID.Store(rand.Int31n(100))
|
||||
user.BridgeState = br.NewBridgeStateQueue(user, user.log)
|
||||
@@ -540,6 +545,8 @@ func (user *User) Connect() error {
|
||||
user.Session.AddHandler(user.messageAckHandler)
|
||||
user.Session.AddHandler(user.typingStartHandler)
|
||||
|
||||
user.Session.AddHandler(user.interactionSuccessHandler)
|
||||
|
||||
user.Session.Identify.Presence.Status = "online"
|
||||
|
||||
return user.Session.Open()
|
||||
@@ -963,6 +970,19 @@ func (user *User) typingStartHandler(_ *discordgo.Session, t *discordgo.TypingSt
|
||||
}
|
||||
}
|
||||
|
||||
func (user *User) interactionSuccessHandler(_ *discordgo.Session, s *discordgo.InteractionSuccess) {
|
||||
user.pendingInteractionsLock.Lock()
|
||||
defer user.pendingInteractionsLock.Unlock()
|
||||
ce, ok := user.pendingInteractions[s.Nonce]
|
||||
if !ok {
|
||||
user.log.Debugfln("Got interaction success for unknown interaction %s/%s", s.Nonce, s.ID)
|
||||
} else {
|
||||
user.log.Infofln("Got interaction success for pending interaction %s/%s", s.Nonce, s.ID)
|
||||
ce.React("✅")
|
||||
delete(user.pendingInteractions, s.Nonce)
|
||||
}
|
||||
}
|
||||
|
||||
func (user *User) ensureInvited(intent *appservice.IntentAPI, roomID id.RoomID, isDirect bool) bool {
|
||||
if intent == nil {
|
||||
intent = user.bridge.Bot
|
||||
|
||||
Reference in New Issue
Block a user