botlib

package
v0.0.0-...-34c8659 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2023 License: GPL-3.0 Imports: 22 Imported by: 0

Documentation

Overview

Copyright (C) 2022-2023  ikafly144

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.

Copyright (C) 2022-2023  ikafly144

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.

Copyright (C) 2022-2023  ikafly144

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.

Copyright (C) 2022-2023  ikafly144

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.

Copyright (C) 2022-2023  ikafly144

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.

Copyright (C) 2022-2023  ikafly144

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.

Copyright (C) 2022-2023  ikafly144

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.

Copyright (C) 2022-2023  ikafly144

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.

Copyright (C) 2022-2023  sabafly

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.

Copyright (C) 2022-2023  ikafly144

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.

Index

Constants

View Source
const (
	FeatureMessageCreate FeatureType = "MESSAGE_CREATE"
	FeatureTypingStart               = "TYPING_START"
	FeatureCustom                    = "CUSTOM"
	FeatureUnknown       FeatureType = ""
)

Variables

View Source
var (
	Endpoint        = "http://localhost:8686/"
	EndpointAPI     = Endpoint + "api/v" + APIVersion + "/"
	EndpointGateway = EndpointAPI + "gateway"
	EndpointGuild   = EndpointAPI + "guild"
	EndpointMessage = EndpointAPI + "message"

	EndpointGuildFeature = EndpointGuild + "/feature"

	EndpointStatics            = EndpointAPI + "statics/"
	EndpointStaticsUser        = EndpointStatics + "user"
	EndpointStaticsUserMessage = EndpointStaticsUser + "/message"
)
View Source
var APIVersion = "0"
View Source
var (
	ErrNoFeatureData = errors.New("no feature data")
)
View Source
var Features = map[FeatureType]string{
	FeatureMessageCreate: "Message Create",
	FeatureTypingStart:   "Typing Start",
	FeatureCustom:        "Custom",
	FeatureUnknown:       "Unknown",
}

Functions

func ActivitiesNameString

func ActivitiesNameString(locale discordgo.Locale, activity *discordgo.Activity) (str string)

アクティビティ名をアクティビティの種類によって渡された言語に翻訳して返す

func AddIntegrationHandler

func AddIntegrationHandler[T any](b *BotManager, handler func(*Api, *discordgo.Session, T))

DiscordAPIイベントから内部APIを呼び出すときに使う

XXX: メソッドで実装したい

func ErrorMessageEmbed

func ErrorMessageEmbed(i *discordgo.InteractionCreate, t string) []*discordgo.MessageEmbed

エラーメッセージ埋め込みを作成する

func ErrorRespond

エラーが発生したことを返すレスポンスを作成する

func ErrorTraceEmbed

func ErrorTraceEmbed(locale discordgo.Locale, err error) []*discordgo.MessageEmbed

エラートレース埋め込みを作成する

func MessageLogDetails

func MessageLogDetails(m []MessageLog) (day, week, all int, channelID string)

func RequestFeatureIDRespond

func RequestFeatureIDRespond(s *discordgo.Session, i *discordgo.InteractionCreate, f *Feature) (ic *discordgo.InteractionCreate, fID []string)

TODO: 再利用可能に

func SendWebhook

func SendWebhook(s *discordgo.Session, channelID string, data *discordgo.WebhookParams) (st *discordgo.Message, err error)

func SetEmbedProperties

func SetEmbedProperties(embeds []*discordgo.MessageEmbed) []*discordgo.MessageEmbed

埋め込みの色、フッター、タイムスタンプを設定する

func StatusString

func StatusString(status discordgo.Status) (str string)

渡されたステータスの絵文字を返す

func WaitNewInteractionResponseSingle

func WaitNewInteractionResponseSingle(s *discordgo.Session, i *discordgo.InteractionCreate, messageData WaitNewInteractionResponseMessageData) (ic *discordgo.InteractionCreate, err error)

TODO: クソみたいな仕様 TODO: 使い方を書き残す TODO: 使わないかもしれない

Types

type Api

type Api struct {
	sync.RWMutex
	Client         http.Client
	MaxRestRetries int
	UserAgent      string

	Dialer *websocket.Dialer
	// contains filtered or unexported fields
}

func NewApi

func NewApi() *Api

func (*Api) FeatureDisable

func (a *Api) FeatureDisable(guildID, featureID, id string) (err error)

func (*Api) FeatureEnable

func (a *Api) FeatureEnable(guildID, featureID, id string) (err error)

func (*Api) FeatureEnabled

func (a *Api) FeatureEnabled(guildID, featureID, id string) (enabled bool, err error)

func (*Api) Gateway

func (a *Api) Gateway() (gateway string, err error)

接続ゲートウェイを取得

func (*Api) Request

func (a *Api) Request(method, urlStr string, data any) (response []byte, err error)

内部REST APIに(GET, POST)リクエストを送信する Sequenceはシーケンス回数を指定する。 もし502エラーで失敗したら成功するかシーケンスがapi.MaxRestRetries以上になるまでシーケンス回数+1回繰り返します

func (*Api) StaticsUserMessage

func (a *Api) StaticsUserMessage(guildID, userID string) (logs []MessageLog, err error)

type ApplicationCommand

type ApplicationCommand struct {
	*discordgo.ApplicationCommand
	Handler func(*discordgo.Session, *discordgo.InteractionCreate)
}

アプリケーションコマンドとそのハンダラを備えた構造体

type ApplicationCommands

type ApplicationCommands []*ApplicationCommand

アプリケーションコマンドのスライス型

func (*ApplicationCommands) Parse

アプリケーションコマンドを解析してハンダラを返す

type BotManager

type BotManager struct {
	*Api
	ShardCount int
	Shards     []*Shard
	// contains filtered or unexported fields
}

ボット接続を管理する

func New

func New(token string) (bot *BotManager, err error)

新規のボット接続を作成する

func (*BotManager) AddApiHandler

func (b *BotManager) AddApiHandler(handler any)

内部APIのイベントハンダラを登録する

func (*BotManager) AddHandler

func (b *BotManager) AddHandler(handler any)

セッションにDiscordAPIイベントハンダラを登録する

func (*BotManager) ApplicationCommandCreate

func (b *BotManager) ApplicationCommandCreate(tree ApplicationCommands) (registeredCommands []*discordgo.ApplicationCommand, err error)

ボットにアプリケーションコマンドを登録する

func (*BotManager) ApplicationCommandDelete

func (b *BotManager) ApplicationCommandDelete(cmd []*discordgo.ApplicationCommand) (err error)

ボットからアプリケーションコマンドを削除する

func (*BotManager) ApplicationCommands

func (b *BotManager) ApplicationCommands() ([]*discordgo.ApplicationCommand, error)

ボットに登録されているコマンドを取得する

func (*BotManager) Close

func (b *BotManager) Close() (err error)

ボットセッションを終了する

func (*BotManager) FeatureApplicationCommandHandler

func (bm *BotManager) FeatureApplicationCommandHandler() func(*discordgo.Session, *discordgo.InteractionCreate)

func (*BotManager) FeatureApplicationCommandSettingsSet

func (bm *BotManager) FeatureApplicationCommandSettingsSet(settings FeatureApplicationCommandSettings)

func (*BotManager) FeatureCreate

func (bm *BotManager) FeatureCreate(f *Feature) (err error)

func (*BotManager) FeatureHandler

func (bm *BotManager) FeatureHandler() func(*discordgo.Session, any)

func (*BotManager) FeaturesApplicationCommand

func (bm *BotManager) FeaturesApplicationCommand() *discordgo.ApplicationCommand

func (*BotManager) LocalApplicationCommandDelete

func (b *BotManager) LocalApplicationCommandDelete() error

サポートサーバーからすべてのコマンドを削除する

TODO: 消すか残りも実装するか

func (*BotManager) Open

func (b *BotManager) Open() (err error)

ボットセッションを開始する

type Event

type Event struct {
	Operation int             `json:"op"`
	Sequence  int64           `json:"s"`
	Type      string          `json:"t"`
	RawData   json.RawMessage `json:"d"`
	// Structはこのファイルのその他の型の一つを含む
	// TODO:よくわからん
	Struct any `json:"-"`
}

type EventHandler

type EventHandler interface {
	Type() string

	Handle(*Shard, any)
}

type EventInterfaceProvider

type EventInterfaceProvider interface {
	// Type is the type of event this handler belongs to.
	Type() string

	// New returns a new instance of the struct this event handler handles.
	// This is called once per event.
	// The struct is provided to all handlers of the same Type().
	New() any
}

type Feature

type Feature struct {
	Name         string
	ID           string
	IDType       FeatureIDType
	ChannelTypes []discordgo.ChannelType
	Type         FeatureType
	Handler      any
}

type FeatureApplicationCommandSettings

type FeatureApplicationCommandSettings struct {
	Name                    string
	Description             string
	NameLocalization        *map[discordgo.Locale]string
	DescriptionLocalization *map[discordgo.Locale]string
	Permission              int64
	DMPermission            bool
	Type                    discordgo.ApplicationCommandType
}

type FeatureData

type FeatureData interface {
	Write(string)
	Delete(string)
	IsEnabled(string) bool
}

type FeatureIDType

type FeatureIDType string
const (
	FeatureChannelID FeatureIDType = "CHANNEL_ID"
	FeatureUserID    FeatureIDType = "USER_ID"
	FeatureGuildID   FeatureIDType = "GUILD_ID"
	FeatureRoleID    FeatureIDType = "ROLE_ID"
)

type FeatureManager

type FeatureManager struct {
	sync.Mutex

	ApplicationCommandSettings FeatureApplicationCommandSettings
	// contains filtered or unexported fields
}

func NewFeatureManager

func NewFeatureManager() *FeatureManager

type FeatureType

type FeatureType string

func (FeatureType) String

func (f FeatureType) String() string

type GuildFeature

type GuildFeature struct {
	GuildID   string `gorm:"index"`
	TargetID  string
	FeatureID string
}

type MessageLog

type MessageLog struct {
	ID        string
	GuildID   string
	ChannelID string
	UserID    string
	Content   string
	Bot       bool
}

type RequestConfig

type RequestConfig struct {
	Request        *http.Request
	MaxRestRetries int
	Client         http.Client
}

type Shard

type Shard struct {
	ShardID int
	*Api
	Session *discordgo.Session
}

シャードとセッションをまとめる

func (*Shard) AddHandler

func (a *Shard) AddHandler(handler any) func()

APIイベントのハンダラを登録する

func (*Shard) ApiClose

func (a *Shard) ApiClose() (err error)

内部APIとの接続を閉じる

TODO:実装する

func (*Shard) ApiOpen

func (a *Shard) ApiOpen() (err error)

type StatusUpdate

type StatusUpdate struct {
	Servers int // サーバー数
	Users   int
}

ボットステータス更新イベント

type WaitNewInteractionResponseMessageData

type WaitNewInteractionResponseMessageData struct {
	Data *discordgo.InteractionResponseData
	Type discordgo.InteractionResponseType

	// TypeがInteractionResponseModalの時のみ使用される
	ModalComponent []discordgo.MessageComponent
	ModalHandler   func(*discordgo.Session, *discordgo.MessageComponentInteractionData)
}

Jump to

Keyboard shortcuts

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