model

package
v0.3.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 4, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Message

type Message struct {
	*discordgo.Message `json:"message,omitempty"`
	Metadata           Metadata `json:"metadata"`
}

Message is the struct that is used to pass messages from the Gateway to the Redis pubsub (inbound messages)

func (*Message) Marshal

func (m *Message) Marshal() ([]byte, error)

Marhsal converts the message to JSON

func (*Message) MarshalJSON added in v0.2.0

func (m *Message) MarshalJSON() ([]byte, error)

MarshalJSON converts the message to JSON Because the *discordgo.Message struct is embedded in the Message struct and also has a MarshalJSON method, go will call the MarshalJSON method of the *discordgo.Message struct when the Message struct is marshaled unless we override it with our own MarshalJSON method in the Message struct, which we do Example:

msg := &model.Message{
	Message: &discordgo.Message{
		Content: "hello world",
	},
}
b, err := msg.MarshalJSON()
if err != nil {
	log.Fatal(err)
}
fmt.Println(string(b))

func (*Message) MarshalReply

func (m *Message) MarshalReply(meta Metadata, dest string, s string) ([]byte, error)

Deprecated in favor of newer methods that consume the entire model.Message struct MarshalReply converts the message to JSON and adds the metadata from the original message MarshalReply sends a response to the originating channel or direct message but does not do a "discord reply"

func (*Message) RespondToChannelOrThread

func (m *Message) RespondToChannelOrThread(sourceApp, content string, shouldReply, shouldMention bool) *MessageSend

RespondToChannelOrThread generates a MessageSend struct that can be used to respond to a channel or thread It optionally allows the message to reply or mention the user that sent the original message

Typically when constructing replies you need access to the discordgo.Session but applications that use this library may not have access to that object, so it actually gets handled in the gateway this constraint forces the MessageSend struct to be a little bigger than I would like it to be but it's necessary for now to have the correct context to respond to messages

func (*Message) Unmarshal

func (m *Message) Unmarshal(b []byte) error

Unmarshal converts the JSON (in bytes) to a message This method is deprecated in favor of the UnmarshalJSON method and will be removed in a future release Correct behavior from this method is not guaranteed Example:

msg := &model.Message{}
if err := msg.Unmarshal([]byte(`{"content":"hello world"}`)); err != nil {
	log.Fatal(err)
}
fmt.Println(msg.Content)

func (*Message) UnmarshalJSON added in v0.2.0

func (m *Message) UnmarshalJSON(b []byte) error

UnmarshalJSON converts the JSON (in bytes) to a message Because the *discordgo.Message struct is embedded in the Message struct and also has an UnmarshalJSON method, go will call the UnmarshalJSON method of the *discordgo.Message struct when the Message struct is marshaled unless we override it with our own UnmarshalJSON method in the Message struct, which we do Example:

msg := &model.Message{}
if err := msg.UnmarshalJSON([]byte(`{"content":"hello world"}`)); err != nil {
	log.Fatal(err)
}

type MessageSend

type MessageSend struct {
	ChannelID       string             `json:"channel_id"`       // ChannelID is the ID of the discord channel to send the message to
	Content         string             `json:"content"`          // Content is the text body of the message to send
	Metadata        Metadata           `json:"metadata"`         // Metadata is the metadata that is used to track the message
	PreviousMessage *discordgo.Message `json:"previous_message"` // PreviousMessage is the message that triggered this message
	ShouldReply     bool               `json:"should_reply"`     // ShouldReply is a flag that indicates if the message should reply to the user that sent the previous message
	ShouldMention   bool               `json:"should_mention"`   // ShouldMention is a flag that indicates if the message should mention the user that sent the previous message
}

MessageSend is the struct that is used to pass messages from the Redis pubsub to the Discord Gateway (outbound messages) Because the discordgo.Session.ChannelMessageSend() method only accepts channel ID and content as a string, our struct limits iteslef to those two fields as well. Future work may expand this to include more fields or expand metadata to include more information that can be used to forumlate more complex responses.

func (*MessageSend) MarshalJSON added in v0.2.0

func (m *MessageSend) MarshalJSON() ([]byte, error)

MarshalJSON converts the message to JSON This method is preferred over the Marshal method and will be the only method in a future release Example:

	msg := &model.MessageSend{
		Content: "hello world",
     Metadata: model.Metadata{
         Source: "test",
         Dest: "discord",
     },
	}
	b, err := msg.MarshalJSON()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(string(b))

func (*MessageSend) Unmarshal

func (m *MessageSend) Unmarshal(b []byte) error

Unmarshal converts the JSON (in bytes) to a message This method is deprecated in favor of the UnmarshalJSON method and will be removed in a future release Correct behavior from this method is not guaranteed Example:

msg := &model.MessageSend{}
if err := msg.Unmarshal([]byte(`{"content":"hello world"}`)); err != nil {
	log.Fatal(err)
}
fmt.Println(msg.Content)

func (*MessageSend) UnmarshalJSON added in v0.2.0

func (m *MessageSend) UnmarshalJSON(b []byte) error

UnmarshalJSON converts the JSON (in bytes) to a message This method is preferred over the Unmarshal method and will be the only method in a future release Example:

msg := &model.MessageSend{}
if err := msg.UnmarshalJSON([]byte(`{"content":"hello world"}`)); err != nil {
	log.Fatal(err)
}
fmt.Println(msg.Content)

type Metadata

type Metadata struct {
	Source string    `json:"source,omitempty"` // Source is the ID of the Gateway or App that sent the message
	Dest   string    `json:"dest,omitempty"`   // Dest is the ID of the Gateway or App that the message is intended for
	ID     uuid.UUID `json:"id,omitempty"`     // ID is a UUID that is generated for each message
}

Metadata is used by the Gateway(s) and app(s) to trace messages and identify intended recipients

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL