Init
Just read from discord channel
This commit is contained in:
50
discord/discordread.go
Normal file
50
discord/discordread.go
Normal 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
11
go.mod
Normal 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
21
main.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user