discord

package
v2.0.0-...-4a423d9 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2023 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option = func(*Service)

Option is a function that applies an option to the service.

func WithClient

func WithClient(session *discordgo.Session) Option

WithClient sets the discord client (session) to use for sending messages. Naming it WithClient to stay consistent with the other services.

func WithContinueOnErr

func WithContinueOnErr(continueOnErr bool) Option

WithContinueOnErr sets the continue on error flag. If set to true, the service will continue sending the message to the next recipient even if an error occurred.

func WithDryRun

func WithDryRun(dryRun bool) Option

WithDryRun sets the dry run flag. If set to true, messages will not be sent.

func WithLogger

func WithLogger(logger onelog.Logger) Option

WithLogger sets the logger. The default logger is a no-op logger.

func WithMessageRenderer

func WithMessageRenderer(builder func(conf *SendConfig) string) Option

WithMessageRenderer sets the message renderer. The default function will put the subject and message on separate lines.

Example:

telegram.WithMessageRenderer(func(conf *SendConfig) string {
	var builder strings.Builder

	builder.WriteString(conf.subject)
	builder.WriteString("\n")
	builder.WriteString(conf.message)

	return builder.String()
})

func WithName

func WithName(name string) Option

WithName sets the name of the service. The default is "discord".

func WithRecipients

func WithRecipients(recipients ...string) Option

WithRecipients sets the channel IDs or webhook URLs to send messages to. You can add more channel IDs or webhook URLs by calling Service.AddRecipients.

type SendConfig

type SendConfig struct {
	Subject       string
	Message       string
	Attachments   []notify.Attachment
	Metadata      map[string]any
	DryRun        bool
	ContinueOnErr bool
}

SendConfig represents the configuration needed for sending a message.

This struct complies with the notify.SendConfig interface and allows you to alter the behavior of the send function. This can be achieved by either passing send options to the send function or by manipulating the fields of this struct in your custom message renderer.

All fields of this struct are exported to offer maximum flexibility to users. However, users must be aware that they are responsible for managing thread-safety and other similar concerns when manipulating these fields directly.

func (*SendConfig) SetAttachments

func (c *SendConfig) SetAttachments(attachments ...notify.Attachment)

SetAttachments adds attachments to the message. This method is needed as part of the notify.SendConfig interface.

func (*SendConfig) SetContinueOnErr

func (c *SendConfig) SetContinueOnErr(continueOnErr bool)

SetContinueOnErr sets the continue on error flag of the message. This method is needed as part of the notify.SendConfig interface.

func (*SendConfig) SetDryRun

func (c *SendConfig) SetDryRun(dryRun bool)

SetDryRun sets the dry run flag of the message. This method is needed as part of the notify.SendConfig interface.

func (*SendConfig) SetMetadata

func (c *SendConfig) SetMetadata(metadata map[string]any)

SetMetadata sets the metadata of the message. This method is needed as part of the notify.SendConfig interface.

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service struct holds necessary data to communicate with the Discord API.

func New

func New(token string, opts ...Option) (*Service, error)

New creates a new Discord service using an OAuth2 token for authentication.

func NewBot

func NewBot(token string, opts ...Option) (*Service, error)

NewBot creates a new Discord bot service.

func NewWebhook

func NewWebhook(opts ...Option) (*Service, error)

NewWebhook creates a new Discord webhook service. The recipient string must be a webhook URL.

func (*Service) AddRecipients

func (s *Service) AddRecipients(recipients ...string)

AddRecipients takes Service channel IDs or webhook URLs and adds them to the list of recipients. You can add more channel IDs or webhook URLs by calling AddRecipients again.

func (*Service) Name

func (s *Service) Name() string

Name returns the name of the service.

func (*Service) Send

func (s *Service) Send(ctx context.Context, subject, message string, opts ...notify.SendOption) error

Send takes a message subject and a message body and sends them to all previously set chats.

Jump to

Keyboard shortcuts

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