Skip to content

Commit c84ad74

Browse files
committed
Better Support Private Messaging
This commit introduces the ability to private message in such a way as to be considered "stable". Each bot get its own discriminator and this makes it where if you were to connect 2 bots to the same Discord identity but 2 different IRC networks when you send a private message since the discriminator is required it will not accidentally be sent to both IRC networks wherein both networks have a client with the same nick. This also enhances privacy since it isn't guaranteed the same person has the same nick on both networks. Pending: log a fatal and exit if Discriminator == "".
1 parent 8838ded commit c84ad74

File tree

4 files changed

+14
-9
lines changed

4 files changed

+14
-9
lines changed

bridge/bridge.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type Config struct {
2222
ChannelMappings map[string]string
2323

2424
IRCServer string
25+
Discriminator string
2526
IRCServerPass string
2627
IRCListenerName string // i.e, "DiscordBot", required to listen for messages in all cases
2728
WebIRCPass string

bridge/discord.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,11 @@ func (d *discordBot) publishMessage(s *discordgo.Session, m *discordgo.Message,
144144

145145
// if the target could not be deduced. tell them this.
146146
if pmTarget == "" {
147-
_, _ = d.ChannelMessageSend(m.ChannelID, "Don't know who that is. Can't PM. Try 'name, message here'")
147+
_, _ = d.ChannelMessageSend(
148+
m.ChannelID,
149+
fmt.Sprintf(
150+
"Don't know who that is. Can't PM. Try 'name@%s, message here'",
151+
d.bridge.Config.Discriminator))
148152
return
149153
}
150154
break

bridge/irc_connection.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ func (i *ircConnection) OnWelcome(e *irc.Event) {
3939
if m.IsAction {
4040
i.innerCon.Action(m.IRCChannel, m.Message)
4141
} else {
42-
if !strings.HasPrefix(m.IRCChannel, "#") {
43-
i.experimentalNotice(m.IRCChannel)
44-
}
4542
i.innerCon.Privmsg(m.IRCChannel, m.Message)
4643
}
4744
}
@@ -77,7 +74,7 @@ func (i *ircConnection) UpdateDetails(discord DiscordUser) {
7774
go i.innerCon.Nick(i.nick)
7875
}
7976

80-
func (i *ircConnection) experimentalNotice(nick string) {
77+
func (i *ircConnection) introducePM(nick string) {
8178
d := i.manager.bridge.discord
8279

8380
if i.pmDiscordChannel == "" {
@@ -92,7 +89,9 @@ func (i *ircConnection) experimentalNotice(nick string) {
9289

9390
if !i.pmNoticed {
9491
i.pmNoticed = true
95-
_, err := d.ChannelMessageSend(i.pmDiscordChannel, "**Private messaging is still in dev. Proceed with caution.**")
92+
_, err := d.ChannelMessageSend(
93+
i.pmDiscordChannel,
94+
fmt.Sprintf("To reply type: %s@%s, message", nick, i.manager.bridge.Config.Discriminator))
9695
if err != nil {
9796
log.Warnln("Could not send pmNotice", i.discord, err)
9897
return
@@ -102,7 +101,6 @@ func (i *ircConnection) experimentalNotice(nick string) {
102101
nick = strings.ToLower(nick)
103102
if _, ok := i.pmNoticedSenders[nick]; !ok {
104103
i.pmNoticedSenders[nick] = struct{}{}
105-
i.innerCon.Privmsg(nick, "Private messaging is still in dev. Proceed with caution.")
106104
}
107105
}
108106

@@ -117,9 +115,9 @@ func (i *ircConnection) OnPrivateMessage(e *irc.Event) {
117115

118116
d := i.manager.bridge.discord
119117

120-
i.experimentalNotice(e.Nick)
118+
i.introducePM(e.Nick)
121119

122-
msg := fmt.Sprintf("%s,%s: %s", e.Connection.Server, e.Source, e.Message())
120+
msg := fmt.Sprintf("%s,%s - %s@%s: %s", e.Connection.Server, e.Source, e.Nick, e.Message())
123121
_, err := d.ChannelMessageSend(i.pmDiscordChannel, msg)
124122
if err != nil {
125123
log.Warnln("Could not send PM", i.discord, err)

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func main() {
6161
log.Fatalln(errors.Wrap(err, "could not read config"))
6262
}
6363

64+
discriminator := viper.GetString("discriminator") // unique per IRC network connected to, keeps PM's working
6465
discordBotToken := viper.GetString("discord_token") // Discord Bot User Token
6566
channelMappings := viper.GetStringMapString("channel_mappings") // Discord:IRC mappings in format '#discord1:#irc1,#discord2:#irc2,...'
6667
ircServer := viper.GetString("irc_server") // Server address to use, example `irc.freenode.net:7000`.
@@ -119,6 +120,7 @@ func main() {
119120

120121
dib, err := bridge.New(&bridge.Config{
121122
AvatarURL: avatarURL,
123+
Discriminator: discriminator,
122124
DiscordBotToken: discordBotToken,
123125
GuildID: guildID,
124126
IRCListenerName: ircUsername,

0 commit comments

Comments
 (0)