Skip to content

Commit

Permalink
Quit IRC when their user leaves the Discord server
Browse files Browse the repository at this point in the history
Fix #45
  • Loading branch information
qaisjp committed Jul 11, 2020
1 parent 056a4f9 commit 6dc5f88
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ type Bridge struct {
discordMessagesChan chan IRCMessage
discordMessageEventsChan chan *DiscordMessage
updateUserChan chan DiscordUser
removeUserChan chan string // user id
}

// Close the Bridge
Expand Down Expand Up @@ -203,6 +204,7 @@ func New(conf *Config) (*Bridge, error) {
discordMessagesChan: make(chan IRCMessage),
discordMessageEventsChan: make(chan *DiscordMessage),
updateUserChan: make(chan DiscordUser),
removeUserChan: make(chan string),
}

if err := dib.load(conf); err != nil {
Expand Down Expand Up @@ -414,6 +416,9 @@ func (b *Bridge) loop() {
case user := <-b.updateUserChan:
b.ircManager.HandleUser(user)

case userID := <-b.removeUserChan:
b.ircManager.DisconnectUser(userID)

// Done!
case <-b.done:
b.discord.Close()
Expand Down
6 changes: 6 additions & 0 deletions bridge/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func newDiscord(bridge *Bridge, botToken, guildID string) (*discordBot, error) {
if !bridge.Config.SimpleMode {
discord.AddHandler(discord.onMemberListChunk)
discord.AddHandler(discord.onMemberUpdate)
discord.AddHandler(discord.onMemberLeave)
discord.AddHandler(discord.OnPresencesReplace)
discord.AddHandler(discord.OnPresenceUpdate)
discord.AddHandler(discord.OnTypingStart)
Expand Down Expand Up @@ -348,6 +349,11 @@ func (d *discordBot) onMemberUpdate(s *discordgo.Session, m *discordgo.GuildMemb
d.handleMemberUpdate(m.Member, false)
}

// onMemberLeave is triggered when a user is removed from a guild (leave/kick/ban).
func (d *discordBot) onMemberLeave(s *discordgo.Session, m *discordgo.GuildMemberRemove) {
d.bridge.removeUserChan <- m.User.ID
}

// What does this do? Probably what it sounds like.
func (d *discordBot) OnPresencesReplace(s *discordgo.Session, m *discordgo.PresencesReplace) {
for _, p := range *m {
Expand Down
9 changes: 9 additions & 0 deletions bridge/irc_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ func (m *IRCManager) SetConnectionCooldown(con *ircConnection) {
log.WithField("nick", con.nick).Println("IRC connection cooldownTimer created...")
}

// DisconnectUser immediately disconnects a Discord user if it exists
func (m *IRCManager) DisconnectUser(userID string) {
con, ok := m.ircConnections[userID]
if !ok {
return
}
m.CloseConnection(con)
}

// HandleUser deals with messages sent from a DiscordUser
func (m *IRCManager) HandleUser(user DiscordUser) {
if user.Username == "" || user.Discriminator == "" {
Expand Down

0 comments on commit 6dc5f88

Please sign in to comment.