-
Notifications
You must be signed in to change notification settings - Fork 0
RSSモジュールを追加 #252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
RSSモジュールを追加 #252
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
cfcac61
rssモジュールを追加
yuito-it d6ff6a7
Update Dockerfile.main
yuito-it a5e2742
Update Dockerfile.rss_cron
yuito-it 1800727
Update src/cmd/rss_cron/main.go
yuito-it 5729780
Update src/internal/bot/handlers/interaction/command/general/rss/subs…
yuito-it 93b8242
メッセージのミス
yuito-it 3e9d140
Update src/internal/bot/handlers/interaction/registry.go
yuito-it 1034d63
Update Dockerfile.rss_cron
yuito-it fc1679c
不要なコードを削除
yuito-it e349e0d
importの追加
yuito-it a6902a9
初回fetchを作ったりする
yuito-it bc6d4b1
miss
yuito-it 9507af8
安全にする
yuito-it 1008af4
miss
yuito-it b75f024
共通変数に括り出していい感じにする
yuito-it 288e27e
セキュリティに関する修正
yuito-it efd3c0f
Update Dockerfile.rss_cron
yuito-it c77204d
未指定アドレスとマルチキャストを追加
yuito-it 7de0735
transportを確実にcloseする
yuito-it 80f2703
コメントの誤字を修正
yuito-it File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| # check=error=true | ||
| # syntax=docker/dockerfile:1 | ||
| FROM golang:1.26.4-alpine3.24 AS builder | ||
| WORKDIR /app | ||
|
|
||
| RUN apk update && apk add --no-cache \ | ||
| curl git file unzip \ | ||
| python3 make g++ \ | ||
| pkgconf | ||
|
|
||
| COPY . . | ||
|
|
||
| WORKDIR /app/src | ||
|
|
||
| RUN --mount=type=cache,target=/go/pkg/mod/,sharing=locked \ | ||
| go mod download -x | ||
|
|
||
| RUN ../scripts/_build.sh --rss-cron | ||
|
|
||
| FROM alpine:3.24.0 | ||
| WORKDIR /root/ | ||
|
|
||
| RUN apk update && apk add --no-cache \ | ||
| ca-certificates | ||
|
|
||
| COPY --from=builder /app/src/main . | ||
|
|
||
| RUN addgroup -g 1000 appuser && \ | ||
| adduser -D -u 1000 -G appuser appuser | ||
| WORKDIR /home/appuser | ||
|
|
||
| RUN mv /root/main /home/appuser/main && chown appuser:appuser ./main && chmod 755 ./main | ||
|
|
||
| USER appuser | ||
|
|
||
| CMD ["/home/appuser/main"] | ||
|
|
||
| # OCI Metadata | ||
|
|
||
| LABEL org.opencontainers.image.authors="Yuito Akatsuki <yuito@yuito-it.jp>" | ||
| LABEL org.opencontainers.image.url="https://github.com/UniPro-tech/UniBot" | ||
| LABEL org.opencontainers.image.source="https://github.com/UniPro-tech/UniBot" | ||
| LABEL org.opencontainers.image.licenses="MIT" | ||
| LABEL org.opencontainers.image.title="UniBot" | ||
| LABEL org.opencontainers.image.description="A multifunctional Discord bot for community management, entertainment, and productivity." | ||
| LABEL org.opencontainers.image.vendor="All-Japan Digital Creative Club UniProject <info@uniproject.jp>" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/base64" | ||
| "fmt" | ||
| "log" | ||
| "os" | ||
| "slices" | ||
| "sort" | ||
| "unibot/internal/db" | ||
| "unibot/internal/repository" | ||
| "unibot/internal/util" | ||
|
|
||
| "github.com/mmcdole/gofeed" | ||
|
|
||
| "github.com/disgoorg/disgo" | ||
| "github.com/disgoorg/disgo/bot" | ||
| "github.com/disgoorg/disgo/cache" | ||
| "github.com/disgoorg/disgo/discord" | ||
| "github.com/disgoorg/disgo/events" | ||
| "github.com/disgoorg/disgo/gateway" | ||
| "github.com/disgoorg/snowflake/v2" | ||
| "gorm.io/gorm" | ||
| "gorm.io/gorm/logger" | ||
| ) | ||
|
|
||
| func main() { | ||
| token := os.Getenv("DISCORD_TOKEN") | ||
| if token == "" { | ||
| log.Fatal("DISCORD_TOKEN is not set") | ||
| } | ||
|
|
||
| dbConnection, err := db.NewDB() | ||
| if err != nil { | ||
| log.Fatal(err) | ||
| } | ||
| dbConnection.Logger = dbConnection.Logger.LogMode(logger.Info) | ||
|
|
||
| client, err := disgo.New(token, | ||
| //bot.WithDefaultGateway(), | ||
| bot.WithGatewayConfigOpts( | ||
| // Intents | ||
| gateway.WithIntents( | ||
| gateway.IntentsNonPrivileged, | ||
| ), | ||
| ), | ||
| // Cache | ||
| bot.WithCacheConfigOpts( | ||
| cache.WithCaches(cache.FlagVoiceStates), | ||
| cache.WithCaches(cache.FlagChannels), | ||
| cache.WithCaches(cache.FlagMessages), | ||
| cache.WithCaches(cache.FlagRoles), | ||
| cache.WithCaches(cache.FlagMembers), | ||
| cache.WithCaches(cache.FlagGuilds), | ||
| ), | ||
| // Event Handler | ||
| bot.WithEventListenerFunc(func(e *events.Ready) { | ||
| Ready(dbConnection, e) | ||
| }), | ||
| ) | ||
| if err != nil { | ||
| log.Fatal("error while building disgo instance: ", err) | ||
| } | ||
|
|
||
| defer client.Close(context.TODO()) | ||
|
|
||
| // 接続開始 | ||
| if err = client.OpenGateway(context.TODO()); err != nil { | ||
| log.Fatal("error while connecting to gateway: ", err) | ||
| } | ||
|
|
||
| log.Println("Bot is running...") | ||
| } | ||
|
|
||
| func Ready(db *gorm.DB, e *events.Ready) { | ||
| log.Println("Bot is ready 🚀") | ||
| log.Printf("Logged in as: %v#%v", e.User.Username, e.User.Discriminator) | ||
|
|
||
| repo := repository.NewRSSSettingRepository(db) | ||
| rssSubscribeList, err := repo.List() | ||
| if err != nil { | ||
| log.Fatal("An error occured:", err) | ||
| return | ||
| } | ||
| for _, rssSetting := range rssSubscribeList { | ||
| url := rssSetting.URL | ||
| feed, err := util.FetchFeed(url) | ||
| if err != nil { | ||
| rssSetting.IsFailed = true | ||
| if err := repo.Update(rssSetting); err != nil { | ||
| log.Print("Update Record Failed", err) | ||
| } | ||
| continue | ||
| } | ||
|
|
||
| feedTitle := rssSetting.Title | ||
| if feedTitle == nil { | ||
| feedTitle = &feed.Title | ||
| } | ||
|
|
||
| // 新しい日時がindex:0 | ||
| sort.Slice(feed.Items, func(i, j int) bool { | ||
| prev := feed.Items[i] | ||
| next := feed.Items[j] | ||
| if prev.PublishedParsed != nil && next.PublishedParsed != nil { | ||
| return prev.PublishedParsed.UnixNano() >= next.PublishedParsed.UnixNano() | ||
| } | ||
| return false | ||
| }) | ||
|
|
||
| // 保存済みハッシュより新しい記事を収集する | ||
| // - LastItemTitleDescriptionHash が nil(初回)なら全件対象 | ||
| // - 一致するハッシュが見つかった時点で break(それ以降は既読) | ||
| var targetItems []*gofeed.Item | ||
| for _, item := range feed.Items { | ||
| hash := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", item.Title, item.Description))) | ||
| if rssSetting.LastItemTitleDescriptionHash != nil && hash == *rssSetting.LastItemTitleDescriptionHash { | ||
| break // ここから先は既読 | ||
| } | ||
| targetItems = append(targetItems, item) | ||
| } | ||
|
|
||
| if len(targetItems) == 0 { | ||
| continue | ||
| } | ||
|
|
||
| // 古い→新しい順に送信 | ||
| slices.Reverse(targetItems) | ||
| channelID := snowflake.MustParse(rssSetting.ChannelID) | ||
| client := e.Client() | ||
| for _, item := range targetItems { | ||
| itemTitle := item.Title | ||
| if itemTitle == "" { | ||
| itemTitle = "(タイトルなし)" | ||
| } | ||
| itemDescription := item.Description | ||
| if itemDescription == "" { | ||
| if item.Content != "" { | ||
| itemDescription = item.Content | ||
| } else { | ||
| itemDescription = "(説明なし)" | ||
| } | ||
| } | ||
| itemLink := item.Link | ||
| if itemLink == "" { | ||
| itemLink = "リンクなし" | ||
| } | ||
| message := fmt.Sprintf( | ||
| "# %s に新しい記事が追加されました!\n## %s\n%s\nURL: %s", | ||
| *feedTitle, itemTitle, itemDescription, itemLink, | ||
| ) | ||
| _, err := client.Rest.CreateMessage(channelID, discord.NewMessageCreate().WithContent(message)) | ||
| if err != nil { | ||
| log.Print("Message create error:", err) | ||
| } | ||
| } | ||
|
|
||
| // 送信完了後に最新ハッシュを保存(feed.Items[0] = 最新記事) | ||
| newestHash := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", feed.Items[0].Title, feed.Items[0].Description))) | ||
| rssSetting.LastItemTitleDescriptionHash = &newestHash | ||
| rssSetting.IsFailed = false | ||
| if err := repo.Update(rssSetting); err != nil { | ||
| log.Print("Update Record Failed", err) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.