Just read from discord channel
This commit is contained in:
2025-03-15 17:38:20 +09:00
commit aea31da9c0
3 changed files with 82 additions and 0 deletions

50
discord/discordread.go Normal file
View File

@@ -0,0 +1,50 @@
package discord
import (
"fmt"
"github.com/bwmarrin/discordgo"
)
type discord struct {
Token string
ChannelID string
dgsession *discordgo.Session
}
func Discord(token string, channelId string) *discord {
return &discord{
Token: token,
ChannelID: channelId,
}
}
func (d *discord) BeginRead(tweetchannel chan<- string) {
d.read(tweetchannel)
}
func (d *discord) read(tweetchannel chan<- string) {
dgsession, err := discordgo.New("Bot " + d.Token)
if err != nil {
return
}
dgsession.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.ChannelID == d.ChannelID {
tweetchannel <- m.Content
}
})
d.dgsession = dgsession
errOpen := dgsession.Open()
if errOpen != nil {
fmt.Println(errOpen)
}
}
func Read() string {
return "discord"
}
func (d *discord) Write(str string) {
d.dgsession.ChannelMessageSend(d.ChannelID, str)
}

11
go.mod Normal file
View File

@@ -0,0 +1,11 @@
module tweetdistributor
go 1.24.1
require github.com/bwmarrin/discordgo v0.28.1
require (
github.com/gorilla/websocket v1.4.2 // indirect
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
)

21
main.go Normal file
View File

@@ -0,0 +1,21 @@
package main
import (
"fmt"
"os"
"tweetdistributor/discord"
)
func main() {
fmt.Println("Hello, World!")
d := discord.Discord(os.Getenv("DISCORD_TOKEN"), os.Getenv("DISCORD_CHANNEL"))
tweetchannel := make(chan string, 1)
d.BeginRead(tweetchannel)
d.Write("Hello, World!")
for tweet := range tweetchannel {
fmt.Println(tweet)
}
}