Skip to content

Commit

Permalink
Panic less
Browse files Browse the repository at this point in the history
  • Loading branch information
qaisjp committed Jan 5, 2018
1 parent 5ae0f3d commit b03a20a
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 24 deletions.
13 changes: 9 additions & 4 deletions bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,15 +310,20 @@ func (b *Bridge) loop() {
avatar = "https://api.adorable.io/avatars/128/" + msg.Username
}

// TODO: What if it takes a long time? See wait=true below.
err := b.discord.whx.Execute(mapping.DiscordChannel, &discordgo.WebhookParams{
params := discordgo.WebhookParams{
Content: msg.Message,
Username: msg.Username,
AvatarURL: avatar,
})
}

// TODO: What if it takes a long time? See wait=true below.
err := b.discord.whx.Execute(mapping.DiscordChannel, &params)

if err != nil {
log.Errorln("Message from IRC to Discord was unsuccessfully sent!", err.Error())
log.WithFields(log.Fields{
"error": err,
"params": params,
}).Errorln("could not send message to Discord")
}

// Messages from Discord to IRC
Expand Down
4 changes: 2 additions & 2 deletions bridge/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ func (d *discordBot) Open() error {
return errors.Wrap(err, "The bot does not have the 'Manage Webhooks' permission.")
}

panic(err)
return errors.Wrap(err, "could not get webhooks")
}

for _, wh := range hooks {
if strings.HasPrefix(wh.Name, d.bridge.Config.WebhookPrefix) {
if err := d.WebhookDelete(wh.ID); err != nil {
log.Printf("Could not delete webhook %s (\"%s\")", wh.ID, wh.Name)
log.Errorln("Could not delete webhook %s (\"%s\")", wh.ID, wh.Name)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion bridge/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func Leftpad(s string, length int, ch ...rune) string {
func SnowflakeToIP(base string, snowflake string) string {
num, err := strconv.ParseUint(snowflake, 10, 64)
if err != nil {
panic(errors.Wrapf(err, "could not convert snowflake to uint: %s", snowflake))
panic(errors.Wrap(err, "could not convert snowflake to uint"))
}

for i, c := range Leftpad(strconv.FormatUint(num, 16), 16, '0') {
Expand Down
11 changes: 5 additions & 6 deletions bridge/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (x *WebhookDemuxer) Execute(channelID string, data *discordgo.WebhookParams
// If we still haven't found a webhook, create one.
var newWebhook *discordgo.Webhook
if chosenWebhook == nil {
log.Println("Creating a webhook stream...")
log.WithField("params", data).Debugln("Creating a new webhook...")

newWebhook, err = x.Discord.WebhookCreate(channelID, x.Discord.bridge.Config.WebhookPrefix+" IRC", "")
if err != nil {
Expand All @@ -100,19 +100,18 @@ func (x *WebhookDemuxer) Execute(channelID string, data *discordgo.WebhookParams
if len(x.webhooks) > 0 {
chosenWebhook, err = x.webhooks[0].ModifyChannel(x, channelID)
if err != nil {
// Panic. We can't send our message :(
panic(errors.Wrap(err, "Could not modify existing webhook after webhook creation failure"))
return errors.Wrap(err, "Could not modify existing webhook after webhook creation failure")
}
// ... if we can. But we can't. Because there aren't any webhooks to use.
} else {
panic(errors.Wrap(err, "No webhooks available to fall back on after webhook creation failure"))
return errors.Wrap(err, "No webhooks available to fall back on after webhook creation failure")
}
}
}

// If we have created a new webhook
if newWebhook != nil {
log.Println("Created new webhook now, so creating wrapped webhook")
log.Debugln("Created new webhook now, so creating wrapped webhook")
// Create demux compatible webhook
chosenWebhook = &Webhook{
Webhook: newWebhook,
Expand All @@ -129,7 +128,7 @@ func (x *WebhookDemuxer) Execute(channelID string, data *discordgo.WebhookParams
// Update the webook username field
chosenWebhook.Username = data.Username

log.Println("--------- done, executing webhook -------")
log.Debugln("--------- done, executing webhook -------")

// TODO: What if it takes a long time? See wait=true below.
err = x.Discord.WebhookExecute(chosenWebhook.ID, chosenWebhook.Token, true, data)
Expand Down
26 changes: 15 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,26 @@ func main() {
WebhookPrefix: webhookPrefix,
})

if err != nil {
log.WithField("error", err).Fatalln("Go-Discord-IRC failed to initialise.")
return
}

// Create new signal receiver
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)

// Open the bot
err = dib.Open()
if err != nil {
log.WithField("error", err).Fatalln("Go-Discord-IRC failed to start.")
return
}

// Inform the user that things are happening!
log.Infoln("Go-Discord-IRC is now running. Press Ctrl-C to exit.")

// Start watching for live changes...
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
log.Println("Configuration file has changed!")
Expand Down Expand Up @@ -137,20 +150,11 @@ func main() {
}
})

// Create new signal receiver
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)

err = dib.Open()
if err != nil {
panic(err)
}

// Watch for a signal
// Watch for a shutdown signal
<-sc

log.Infoln("Shutting down Go-Discord-IRC...")

// Cleanly close down the Discord session.
// Cleanly close down the bridge.
dib.Close()
}

0 comments on commit b03a20a

Please sign in to comment.