Compare commits

...

3 Commits

Author SHA1 Message Date
b8152b3c6a remove redundant stdout 2025-03-20 14:49:46 +09:00
c25735f9c9 add StdOutput 2025-03-20 14:48:08 +09:00
5f9b838af5 change channel struct 2025-03-20 14:40:52 +09:00
3 changed files with 29 additions and 6 deletions

View File

@@ -19,11 +19,11 @@ func Discord(token string, channelId string) *discord {
} }
} }
func (d *discord) BeginRead(tweetchannel chan<- string) { func (d *discord) BeginRead(tweetchannel chan<- *discordgo.MessageCreate) {
d.read(tweetchannel) d.read(tweetchannel)
} }
func (d *discord) read(tweetchannel chan<- string) { func (d *discord) read(tweetchannel chan<- *discordgo.MessageCreate) {
dgsession, err := discordgo.New("Bot " + d.Token) dgsession, err := discordgo.New("Bot " + d.Token)
if err != nil { if err != nil {
return return
@@ -31,7 +31,7 @@ func (d *discord) read(tweetchannel chan<- string) {
dgsession.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) { dgsession.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.ChannelID == d.ChannelID && m.Author.ID != s.State.User.ID { if m.ChannelID == d.ChannelID && m.Author.ID != s.State.User.ID {
tweetchannel <- m.Content tweetchannel <- m
} }
}) })
d.dgsession = dgsession d.dgsession = dgsession

View File

@@ -5,24 +5,26 @@ import (
"os" "os"
"tweetdistributor/discord" "tweetdistributor/discord"
"tweetdistributor/output" "tweetdistributor/output"
"github.com/bwmarrin/discordgo"
) )
func main() { func main() {
d := discord.Discord(os.Getenv("DISCORD_TOKEN"), os.Getenv("DISCORD_CHANNEL")) d := discord.Discord(os.Getenv("DISCORD_TOKEN"), os.Getenv("DISCORD_CHANNEL"))
tweetchannel := make(chan string, 1) tweetchannel := make(chan *discordgo.MessageCreate, 1)
d.BeginRead(tweetchannel) d.BeginRead(tweetchannel)
d.Write("Tweetdistributor Started") d.Write("Tweetdistributor Started")
var outputs []output.OutputInterface var outputs []output.OutputInterface
outputs = append(outputs, output.StdOutput())
outputs = append(outputs, output.TwitterOutput(os.Getenv("TW_ACCESS_TOKEN"), os.Getenv("TW_ACCESS_SECRET"))) outputs = append(outputs, output.TwitterOutput(os.Getenv("TW_ACCESS_TOKEN"), os.Getenv("TW_ACCESS_SECRET")))
outputs = append(outputs, output.BlueskyOutput(os.Getenv("BSKY_IDENTIFIER"), os.Getenv("BSKY_PASSWORD"))) outputs = append(outputs, output.BlueskyOutput(os.Getenv("BSKY_IDENTIFIER"), os.Getenv("BSKY_PASSWORD")))
for tweet := range tweetchannel { for tweet := range tweetchannel {
fmt.Println(tweet)
for _, output := range outputs { for _, output := range outputs {
err := output.Write(tweet) err := output.Write(tweet.Content)
if err != nil { if err != nil {
errstr := fmt.Sprintf("%s Error: %s", output.GetName(), err) errstr := fmt.Sprintf("%s Error: %s", output.GetName(), err)
fmt.Fprintln(os.Stderr, errstr) fmt.Fprintln(os.Stderr, errstr)

21
output/stdout.go Normal file
View File

@@ -0,0 +1,21 @@
package output
import (
"fmt"
)
type stdoutput struct {
}
func StdOutput() *stdoutput {
return &stdoutput{}
}
func (so *stdoutput) Write(str string) error {
fmt.Println(str)
return nil
}
func (so *stdoutput) GetName() string {
return "stdout"
}