Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose flags on MessageEdit so you can suppress embeds on other messages #973

Merged
merged 10 commits into from Apr 14, 2022

Conversation

nixxquality
Copy link
Contributor

This pull request exposes the flags member of the MessageEdit struct so you can suppress embeds on other people's messages.

Other users can only edit flags and only if they have the MANAGE_MESSAGES permission in the corresponding channel.
https://discord.com/developers/docs/resources/channel#edit-message

I've tested it and it works with the following code:

original, err := s.ChannelMessage("290610944458096642", "867522047768133632")
edit := discordgo.NewMessageEdit("290610944458096642", "867522047768133632")
edit.Flags = original.Flags | discordgo.MessageFlagsSuppressEmbeds
msg, err := s.ChannelMessageEditComplex(edit)

Additionally, I wanted to include a convenience function for suppressing embeds on a message like so:

// SuppressEmbeds is a convenience function that sets the
// SUPPRESS_EMBEDS flag on the message.
func (m *MessageEdit) SuppressEmbeds() *MessageEdit {
	m.Flags = MessageFlagsSuppressEmbeds
	return m
}

However, that wouldn't work.

When specifying flags, ensure to include all previously set flags/bits in addition to ones that you are modifying.
https://discord.com/developers/docs/resources/channel#edit-message

Since the MessageEdit struct knows nothing about the message it's editing and it's not connected to a session, I'm not sure how you would like to handle this.

Should it not be included?

Should the MessageEdit include a reference to the Session that created it?

I also changed two more things. First, I noticed a typo in the enum so I marked that member as deprecated (not sure if that works?) and included a fixed one with the same value.

I also had to make Components omitempty on the MessageEdit struct or Discord would give an error about "Cannot edit a message authored by another user"

Also fix typo in the message flags enum
I wasn't sure if this was going to work but the documentation says:

> The "omitempty" option specifies that the field should be omitted from the encoding if the field has an empty value, defined as false, 0, a nil pointer, a nil interface value, and any empty array, slice, map, or string.

Therefore, if Flags is unmodified it will be omitted.
I think the null value here may be triggering the "Cannot edit a message authored by another user"
Stops the gofmt error
message.go Outdated
Comment on lines 182 to 186
// Deprecated: use MessageFlagsSuppressEmbeds
const (
MessageFlagsSupressEmbeds MessageFlags = 1 << 2
)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be best to just put it right after the corrected constant.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I put it separate was because of a gofmt error in the previous build

0.06s$ diff <(gofmt -d .) <(echo -n)
1,14d0
< diff -u message.go.orig message.go
< --- message.go.orig	2021-07-22 08:05:45.890441692 +0000
< +++ message.go	2021-07-22 08:05:45.890441692 +0000
< @@ -172,8 +172,8 @@
<  
<  // Valid MessageFlags values
<  const (
< -	MessageFlagsCrossPosted          MessageFlags = 1 << 0
< -	MessageFlagsIsCrossPosted        MessageFlags = 1 << 1
< +	MessageFlagsCrossPosted   MessageFlags = 1 << 0
< +	MessageFlagsIsCrossPosted MessageFlags = 1 << 1
<  	// Deprecated: use MessageFlagsSuppressEmbeds
<  	MessageFlagsSupressEmbeds        MessageFlags = 1 << 2
<  	MessageFlagsSuppressEmbeds       MessageFlags = 1 << 2
The command "diff <(gofmt -d .) <(echo -n)" exited with 1.

Between messing up the spacing or extracting it to a separate section I chose the latter.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just put it at the end of the section, like that

const (
   ...
   // TODO: remove when compatibility is not required
   MessageFlagsSupressEmbeds       MessageFlags = 1 << 2
)

message.go Outdated
@@ -204,9 +209,10 @@ type MessageSend struct {
// is also where you should get the instance from.
type MessageEdit struct {
Content *string `json:"content,omitempty"`
Components []MessageComponent `json:"components"`
Components []MessageComponent `json:"components,omitempty"`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was intentional, to allow users to remove components from messages.
Also it would be probably better to move this into a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to add this to make it work. Before I added it, Discord would give an error about "Cannot edit a message authored by another user"

This seems to be mutually exclusive with your current requirements, so I don't know what to do about that.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While it's related to SuppressEmbeds flag, the issue existed before, so I think it would be better to resolve this in a separate PR.

message.go Outdated Show resolved Hide resolved
@FedorLap2006
Copy link
Collaborator

Sorry it took a long time

@FedorLap2006 FedorLap2006 added this to the v0.25.0 milestone Apr 6, 2022
@FedorLap2006
Copy link
Collaborator

Also, please note the conflicts

@FedorLap2006
Copy link
Collaborator

I've made a couple of changes myself, to speed up the process.
But thanks for the contribution!

@FedorLap2006 FedorLap2006 merged commit b138df6 into bwmarrin:master Apr 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants