Skip to content

Base ​

go
type Base struct {
	Client     *CustomTelegramClient
	DB         *Database
	Translator *Translator
}

Embed it and a module needs only Name and Commands.

go
type Weather struct{ goroku.Base }

Fields ​

FieldUse
ClientTelegram API — send files, resolve peers, inline manager
DBPersistent storage, see Storage
TranslatorBacking store for T; you rarely touch it directly

All three are populated by the loader before Init runs, so they are usable from Init, ClientReady, commands and watchers alike.

Methods ​

T(key, def string) string ​

Translated string for key, or def when absent. Replaces the per-module getTrans helper that modules used to copy.

go
return msg.Answer(m.T("no_city", "❌ <b>Which city?</b>"))

Resolution tries goroku.modules.<name>.<key> with the module name as written, lowercased, and snake_cased, then any TranslationAliases.

Safe on a nil Base and on an unregistered module — it returns def.

Init(client, db) error ​

Wires the fields and returns nil. Override freely:

go
func (m *Weather) Init(client *goroku.CustomTelegramClient, db *goroku.Database) error {
	if err := m.Base.Init(client, db); err != nil {
		return err
	}
	m.cache = make(map[string]string)
	return nil
}

The loader populates the fields before calling Init, so the m.Base.Init call is redundant in normal operation. Keep it: it makes the module work when constructed directly in a test.

ClientReady() / OnUnload() / OnDlmod() error ​

Return nil. Override the ones you need.

Strings() map[string]string ​

Returns nil. Override to declare text.

Watchers() []WatcherHandler ​

Returns nil. Override to watch every message.

NewBase ​

go
func NewBase(client *CustomTelegramClient, db *Database) Base

For a module constructed by hand rather than registered through the loader — internal helpers and tests:

go
mod := &Weather{Base: goroku.NewBase(client, db)}

The embedding module is not known here, so T returns its default until the module is registered.

Name collisions ​

If your struct already has a field called Client, DB or Translator, embedding Base makes the name ambiguous. Rename yours, or reach through the embedded struct explicitly with m.Base.Client.

Released under the GNU AGPL v3 License.