notifications

package
v2.49.2 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2024 License: MIT Imports: 25 Imported by: 0

README

Notifications

Simple notifications plugin for YAGPDB.

Will provide general notifications in a server for the following events:

  • User join
  • User leave
  • Topic changed
  • Message pinned

Documentation

Index

Constants

View Source
const MaxResponses = 10
View Source
const RecordSeparator = "\x1e"

Variables

View Source
var DBSchemas = []string{`
CREATE TABLE IF NOT EXISTS general_notification_configs (
	guild_id BIGINT PRIMARY KEY,
	created_at TIMESTAMP WITH TIME ZONE NOT NULL,
	updated_at TIMESTAMP WITH TIME ZONE NOT NULL,

	-- Many of the following columns should be non-nullable, but were originally
	-- managed by gorm (which does not add NOT NULL constraints by default) so are
	-- missing them. Unfortunately, it is unfeasible to retroactively fill missing
	-- values with defaults and add the constraints as there are simply too many
	-- rows in production.

	-- For similar legacy reasons, many fields that should have type BIGINT are TEXT.

	join_server_enabled BOOLEAN,
	join_server_channel TEXT,
	-- This column should be a TEXT[]. But for legacy reasons, it is instead a single
	-- TEXT column containing all template responses joined together and delimited by
	-- the character U+001E (INFORMATION SEPARATOR TWO.)
	join_dm_enabled BOOLEAN,
	join_dm_msg TEXT,

	leave_enabled BOOLEAN,
	leave_channel TEXT,
	
	topic_enabled BOOLEAN,
	topic_channel TEXT,

	censor_invites BOOLEAN
);
`, `

-- Tables created with gorm have missing NOT NULL constraints for created_at and
-- updated_at columns; since these columns are never null in existing rows, we can
-- retraoctively add the constraints without needing to update any data.

ALTER TABLE general_notification_configs ALTER COLUMN created_at SET NOT NULL;
`, `
ALTER TABLE general_notification_configs ALTER COLUMN updated_at SET NOT NULL;
`,
	`
ALTER TABLE general_notification_configs ADD COLUMN IF NOT EXISTS join_server_msgs TEXT;
`, `
ALTER TABLE general_notification_configs ADD COLUMN IF NOT EXISTS leave_msgs TEXT;
`, `

-- Now the more complicated migration. For legacy reasons, the general_notification_configs
-- table previously contained two pairs of columns for join and leave message:
--   * join_server_msg, join_server_msgs_
--   * leave_msg, leave_msgs_
-- all of type TEXT. (The variants with _ were added when multiple-response support was
-- implemented and contain the individual responses separated by U+001E as described
-- previously.)

-- Ideally, we only have one column for each message. We achieve this state with the following
-- multi-step migration:
--   1. Update old records with join_server_msg != '' or leave_msg != '' to use the plural
--      join_server_msgs_ and leave_msgs_ columns respectively.
--   2. Drop the join_server_msg and leave_msg columns.
--   3. Rename join_server_msgs_ to join_server_msgs and leave_msgs_ to leave_msgs.

-- Here goes.
DO $$
BEGIN

-- only run if general_notifcation_configs.join_server_msg (indicative of legacy table) exists
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='general_notification_configs' AND column_name='join_server_msgs')
AND EXISTS(SELECT 1 FROM information_schema.columns WHERE table_name='general_notification_configs' AND column_name='join_server_msg') THEN
	UPDATE general_notification_configs SET join_server_msgs_ = join_server_msg WHERE join_server_msg != '';
	UPDATE general_notification_configs SET leave_msgs_ = leave_msg WHERE leave_msg != '';

	ALTER TABLE general_notification_configs DROP COLUMN join_server_msg;
	ALTER TABLE general_notification_configs DROP COLUMN leave_msg;

	ALTER TABLE general_notification_configs RENAME COLUMN join_server_msgs_ to join_server_msgs;
	ALTER TABLE general_notification_configs RENAME COLUMN leave_msgs_ to leave_msgs;
END IF;
END $$;
`}
View Source
var PageHTML string

Functions

func HandleChannelUpdate

func HandleChannelUpdate(evt *eventsystem.EventData) (retry bool, err error)

func HandleGuildMemberAdd

func HandleGuildMemberAdd(evtData *eventsystem.EventData) (retry bool, err error)

func HandleGuildMemberRemove

func HandleGuildMemberRemove(evt *eventsystem.EventData) (retry bool, err error)

func HandleNotificationsGet

func HandleNotificationsGet(w http.ResponseWriter, r *http.Request) interface{}

func HandleNotificationsPost

func HandleNotificationsPost(w http.ResponseWriter, r *http.Request) (web.TemplateData, error)

func RegisterPlugin

func RegisterPlugin()

func SaveConfig added in v2.40.0

func SaveConfig(config *Config) error

Types

type Config

type Config struct {
	GuildID   int64
	CreatedAt time.Time
	UpdatedAt time.Time

	JoinServerEnabled bool  `json:"join_server_enabled" schema:"join_server_enabled"`
	JoinServerChannel int64 `json:"join_server_channel" schema:"join_server_channel" valid:"channel,true"`

	JoinServerMsgs []string `json:"join_server_msgs" schema:"join_server_msgs" valid:"template,5000"`
	JoinDMEnabled  bool     `json:"join_dm_enabled" schema:"join_dm_enabled"`
	JoinDMMsg      string   `json:"join_dm_msg" schema:"join_dm_msg" valid:"template,5000"`

	LeaveEnabled bool     `json:"leave_enabled" schema:"leave_enabled"`
	LeaveChannel int64    `json:"leave_channel" schema:"leave_channel" valid:"channel,true"`
	LeaveMsgs    []string `json:"leave_msgs" schema:"leave_msgs" valid:"template,5000"`

	TopicEnabled bool  `json:"topic_enabled" schema:"topic_enabled"`
	TopicChannel int64 `json:"topic_channel" schema:"topic_channel" valid:"channel,true"`

	CensorInvites bool `schema:"censor_invites"`
}

func BotCachedGetConfig added in v2.41.0

func BotCachedGetConfig(guildID int64) (*Config, error)

func FetchConfig added in v2.41.0

func FetchConfig(guildID int64) (*Config, error)

func (*Config) ToModel added in v2.40.0

func (c *Config) ToModel() *models.GeneralNotificationConfig

func (*Config) Validate added in v2.40.0

func (c *Config) Validate(tmpl web.TemplateData, _ int64) bool

type Plugin

type Plugin struct{}

func (*Plugin) BotInit

func (p *Plugin) BotInit()

func (*Plugin) InitWeb

func (p *Plugin) InitWeb()

func (*Plugin) LoadServerHomeWidget

func (p *Plugin) LoadServerHomeWidget(w http.ResponseWriter, r *http.Request) (web.TemplateData, error)

func (*Plugin) PluginInfo

func (p *Plugin) PluginInfo() *common.PluginInfo

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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