Skip to content

Commit 119fa16

Browse files
committed
Initial Commit
0 parents  commit 119fa16

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+12515
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test

.idea/.gitignore

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/pttLink-public.iml

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/consts.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package common
2+
3+
import "regexp"
4+
5+
const LiveUpdateCommand = "\x1B[D\x1B[C\x1B[4~"
6+
7+
type ViewState int32
8+
9+
const (
10+
ViewStateBacking ViewState = iota
11+
ViewStateSearching
12+
ViewStateReadyViewing
13+
ViewStateViewing
14+
)
15+
16+
// PostRegex [Message Cursor ID Status PushCount]
17+
var PostRegex = regexp.MustCompile("([> ])(\\d+) ([+Mm -!]) ?(X?\\d+|[爆 ]) ?(\\d{1,2})/ ?(\\d{1,2}) (\\w+)\\s+([\\S\\s]*?)\\s+\\n")
18+
19+
// CommentRegex [Message StrType Author Content Time]
20+
var CommentRegex = regexp.MustCompile("(→ |→|推|噓) (\\w+): ([\\s\\S]+?)(?: +?| {0})(?:\\S+\\n|(\\d+/\\d+ \\d+:\\d+))")

common/ptt.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package common
2+
3+
import (
4+
"github.com/disgoorg/disgo/bot"
5+
"github.com/patyhank/ptd/ent"
6+
"sync"
7+
)
8+
9+
type Instance struct {
10+
discord bot.Client
11+
currentPost *ent.PostInfo // 目前瀏覽的貼文
12+
previousPost *ent.PostInfo // 上一個瀏覽的貼文,瀏覽下一篇時,於Discord新增已完成標籤
13+
db *ent.Client
14+
15+
viewState ViewState
16+
sync.Mutex
17+
}
18+
19+
func NewInstance() {
20+
21+
}

common/utils.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package common
2+
3+
import (
4+
"github.com/patyhank/ptd/ent"
5+
"strings"
6+
)
7+
8+
// SplitMessageByContentLength 將訊息依照字數分割,避免訊息過長無法發送至Discord
9+
func SplitMessageByContentLength(collection ent.Messages, wordCount int) (chunks []ent.Messages) {
10+
var chunk []*ent.Message
11+
var count int
12+
for _, data := range collection {
13+
count += len(data.Content)
14+
if count > wordCount {
15+
chunks = append(chunks, chunk)
16+
chunk = []*ent.Message{}
17+
count = 0
18+
}
19+
chunk = append(chunk, data)
20+
}
21+
if len(chunk) > 0 {
22+
chunks = append(chunks, chunk)
23+
}
24+
return
25+
}
26+
27+
// SplitContentByLength
28+
// 將字串依照指定的字數分割,並提前於換行處分割
29+
// 這讓Discord訊息更好閱讀,並且不會截斷連結
30+
func SplitContentByLength(input string, wordCount int) (chunks []string) {
31+
var chunk string
32+
var count int
33+
collection := strings.Split(input, "\n")
34+
for _, data := range collection {
35+
count += len([]rune(data))
36+
if count >= wordCount {
37+
chunks = append(chunks, chunk)
38+
chunk = ""
39+
count = 0
40+
}
41+
chunk += data + "\n"
42+
}
43+
if len(chunk) > 0 {
44+
chunks = append(chunks, chunk)
45+
}
46+
return
47+
}

config/comment.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package config
2+
3+
type CommentType int32
4+
5+
const (
6+
_ CommentType = iota
7+
CommentTypeUpVote
8+
CommentTypeDownVote
9+
CommentTypeReply
10+
)
11+
12+
func (c CommentType) String() string {
13+
switch c {
14+
case CommentTypeUpVote:
15+
return "推"
16+
case CommentTypeDownVote:
17+
return "噓"
18+
case CommentTypeReply:
19+
return "→"
20+
}
21+
return ""
22+
}

config/config.go

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package config
2+
3+
import (
4+
"github.com/disgoorg/disgo/discord"
5+
"github.com/disgoorg/snowflake/v2"
6+
"strings"
7+
)
8+
9+
// Config is the configuration for the bot
10+
type Config struct {
11+
Discord Discord `yaml:"discord"`
12+
PTT PTTConfig `yaml:"ptt"`
13+
14+
Searches []SearchConfig `yaml:"searches"`
15+
}
16+
17+
type Discord struct {
18+
Token string `yaml:"token"`
19+
}
20+
21+
type EmojiConfig struct {
22+
UpVote discord.Emoji `yaml:"up_vote"`
23+
DownVote discord.Emoji `yaml:"down_vote"`
24+
Reply discord.Emoji `yaml:"reply"`
25+
}
26+
27+
type PTTConfig struct {
28+
Connection ConnectionConfig `yaml:"connection"`
29+
}
30+
31+
type ConnectionConfig struct {
32+
Username string `yaml:"username"`
33+
Password string `yaml:"password"`
34+
Host string `yaml:"host"`
35+
HostOrigin string `yaml:"host_origin"`
36+
}
37+
type SearchConfig struct {
38+
Board string `json:"board"`
39+
SearchVariant []SearchPattern `json:"search_variant"`
40+
TitleSearchVariant []SearchPattern `json:"title_search_variant"`
41+
42+
PreFillSize int `yaml:"prefill_size"`
43+
44+
PostMatchRegex string `json:"post_match_regex"`
45+
PostTitle string `json:"post_title"`
46+
47+
TrackingSeconds int `yaml:"tracking_seconds"`
48+
49+
ForumChannel snowflake.ID `yaml:"forum_channel,omitempty"`
50+
TextChannel snowflake.ID `yaml:"channel,omitempty"`
51+
Emoji EmojiConfig `yaml:"emoji"`
52+
}
53+
54+
type SearchPattern string
55+
56+
func (s SearchPattern) Keys() []string {
57+
content := string(s)
58+
59+
content = strings.ReplaceAll(content, "\r\n", "\n")
60+
content = strings.ReplaceAll(content, "\\r", "\r")
61+
62+
args := strings.Split(content, "|")
63+
64+
return args
65+
}

config/default.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package config
2+
3+
import (
4+
"github.com/disgoorg/disgo/discord"
5+
)
6+
7+
var DefaultConfig = Config{
8+
Discord: Discord{
9+
Token: "",
10+
},
11+
PTT: PTTConfig{
12+
Connection: ConnectionConfig{
13+
Username: "user",
14+
Password: "pass",
15+
Host: "wss://ws.ptt.cc/bbs",
16+
HostOrigin: "https://term.ptt.cc",
17+
},
18+
},
19+
Searches: []SearchConfig{
20+
{
21+
Board: "c_chat",
22+
SearchVariant: []SearchPattern{"/holo\\r/直播\\r", "/holo\\r/間直播\\r", "/holo\\r/直播單\\r"}, // 不建議超過3避免無法搜尋文章日期等
23+
TitleSearchVariant: []SearchPattern{"/holo\\r/直播\\r", "/holo\\r/間直播\\r", "/holo\\r/直播單\\r"},
24+
TrackingSeconds: 600,
25+
PostMatchRegex: "\\[\\S+?] \\[Vtub] {1,2}Hololive ([晚日])間直播單(?:(| \\()(\\d+)(?:)|\\))",
26+
PostTitle: "%[2]s-%[1]s間直播串",
27+
28+
ForumChannel: 0,
29+
TextChannel: 0,
30+
Emoji: EmojiConfig{
31+
UpVote: discord.Emoji{
32+
Name: "push",
33+
ID: 1033734367937310720,
34+
},
35+
DownVote: discord.Emoji{
36+
Name: "boo",
37+
ID: 1033734366330892428,
38+
},
39+
Reply: discord.Emoji{
40+
Name: "addon",
41+
ID: 1033734364950953994,
42+
},
43+
},
44+
},
45+
},
46+
}

0 commit comments

Comments
 (0)