Skip to content

Commit

Permalink
Refactor a new bot.channelMessage that stuffs the GuildID into the re…
Browse files Browse the repository at this point in the history
…turned message, and document why it exists. Part of #34.
  • Loading branch information
jkomoros committed Jun 15, 2021
1 parent a785c50 commit 6cb0389
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,23 @@ func createForkMessageEmbed(msg *discordgo.Message) *discordgo.MessageEmbed {
}
}

//channelRef is a wrapper around session.ChannelMessage. The Discord API for
//some reason omits GuildID for messages fetched via ChannelMessage, but other
//processing assumes it exists. This method will fetch it but also stuff the
//GuildID in.
func (b *bot) channelMessage(ref *discordgo.MessageReference) (*discordgo.Message, error) {
//OK, it has forks, we need to fetch the updated message.
msg, err := b.session.ChannelMessage(ref.ChannelID, ref.MessageID)
if err != nil {
return nil, fmt.Errorf("couldn't fetch the raw updated message: %v", err)
}
//No idea why this is happening, but the Message comes back form
//ChannelMessage with a zeroed guildID! Stuff it in for the sake of
//downstream stuff...
msg.GuildID = ref.GuildID
return msg, nil
}

func (b *bot) updateForkedMessagesIfTheyExist(ref *discordgo.MessageReference) error {
idf, err := b.getLiveIDFIndex(ref.GuildID)
if err != nil {
Expand All @@ -283,14 +300,11 @@ func (b *bot) updateForkedMessagesIfTheyExist(ref *discordgo.MessageReference) e
return nil
}
//OK, it has forks, we need to fetch the updated message.
sourceMessage, err := b.session.ChannelMessage(ref.ChannelID, ref.MessageID)
sourceMessage, err := b.channelMessage(ref)
if err != nil {
return fmt.Errorf("couldn't fetch the raw updated message: %v", err)
}
//No idea why this is happening, but the Message comes back form
//ChannelMessage with a zeroed guildID! Stuff it in for the sake of
//downstream stuff...
sourceMessage.GuildID = ref.GuildID

if err := b.updateForkedMessages(sourceMessage); err != nil {
return fmt.Errorf("couldn't update forked messages: %v", err)
}
Expand Down Expand Up @@ -318,12 +332,10 @@ func (b *bot) updateForkedMessages(sourceMessage *discordgo.Message) error {
}

func (b *bot) forkMessage(sourceRef *discordgo.MessageReference, targetChannelID string) error {
msg, err := b.session.ChannelMessage(sourceRef.ChannelID, sourceRef.MessageID)
msg, err := b.channelMessage(sourceRef)
if err != nil {
return fmt.Errorf("couldn't fetch message: %v", err)
}
//No idea why ChannelMessage comes back without guildID set??
msg.GuildID = sourceRef.GuildID

embed := createForkMessageEmbed(msg)

Expand Down

0 comments on commit 6cb0389

Please sign in to comment.