-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
91 lines (77 loc) · 2.11 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/bwmarrin/discordgo"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
"github.com/joho/godotenv"
)
var db *sqlx.DB
func init() {
err := godotenv.Load()
if err != nil {
log.Println("info: no .env file")
}
// MYSQLへの接続
mysqlUser := os.Getenv("MYSQL_USER")
mysqlPass := os.Getenv("MYSQL_PASSWORD")
mysqlAddr := os.Getenv("MYSQL_ADDRESS")
mysqlPort := os.Getenv("MYSQL_PORT")
mysqlDBName := os.Getenv("MYSQL_DATABASE")
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", mysqlUser, mysqlPass, mysqlAddr, mysqlPort, mysqlDBName)
log.Println("info: DSN ->", dsn)
for {
db, err = sqlx.Connect("mysql", dsn)
if err != nil {
log.Println("SQL Connect Error\n", err)
time.Sleep(time.Second * 15)
continue
}
break
}
// MYSQLのスキーマ定義
schema1 := `
CREATE TABLE IF NOT EXISTS timeline_channel (
guild_id BIGINT UNSIGNED NOT NULL PRIMARY KEY,
timeline_id BIGINT UNSIGNED NOT NULL
);`
schema2 := `
CREATE TABLE IF NOT EXISTS timeline_message (
timeline_message_id BIGINT UNSIGNED NOT NULL PRIMARY KEY,
link_message_id BIGINT UNSIGNED NOT NULL,
original_message_id BIGINT UNSIGNED NOT NULL
);`
db.MustExec(schema1)
db.MustExec(schema2)
}
func main() {
discordToken := os.Getenv("DISCORD_TOKEN")
dg, err := discordgo.New("Bot " + discordToken)
if err != nil {
fmt.Println("Error creacting Discord session,", err)
}
// timelineチャンネルの登録(!timeline)
dg.AddHandler(registTimelineChannel)
// timelineに送る
dg.AddHandler(sendTimeline)
// 元のメッセージが編集されたら、タイムライン側も編集
dg.AddHandler(editTimeline)
// 元のチャットが削除されたら、タイムライン側も削除
dg.AddHandler(delTimeline)
dg.Identify.Intents = discordgo.IntentsGuildMessages
err = dg.Open()
if err != nil {
log.Fatal("Error opening connection,", err)
return
}
log.Println("Bot is now running. Press Ctrl-C to exit")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
dg.Close()
}