-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
97 lines (76 loc) · 1.76 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
92
93
94
95
96
97
package main
import (
"fmt"
"io/ioutil"
"os"
"os/signal"
"strings"
"syscall"
"github.com/bwmarrin/discordgo"
"github.com/jiro4989/ojosama"
)
func GetHelp() string {
f, err := os.Open("help.txt")
if err != nil {
panic(err)
}
defer f.Close()
b, err := ioutil.ReadAll(f)
text, err := ojosama.Convert(string(b), nil)
if err != nil {
panic(err)
}
return text
}
func onMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
splitedContent := strings.Split(m.Content, " ")
if splitedContent[0] != "/ojosama" {
return
}
if splitedContent[0] == "/ojosama" {
if splitedContent[1] == "--help" {
s.ChannelMessageSend(m.ChannelID, GetHelp())
return
}
text, err := ojosama.Convert(splitedContent[1], nil)
if err != nil {
panic(err)
}
s.ChannelMessageSend(m.ChannelID, text)
}
return
}
func OjosamaErrorHandling(message string, err error) {
errText, err := ojosama.Convert(message+"\n以下エラーメッセージです!", nil)
if err != nil {
panic(err)
}
fmt.Println(errText)
fmt.Println(err)
}
func main() {
discord, err := discordgo.New("Bot " + os.Getenv("CLIENT_TOKEN"))
if err != nil {
OjosamaErrorHandling("セッションの作成に失敗しました!", err)
return
}
discord.AddHandler(onMessageCreate)
discord.Identify.Intents = discordgo.IntentsGuildMessages
err = discord.Open()
if err != nil {
OjosamaErrorHandling("コネクションエラーです!", err)
return
}
if runningText, err := ojosama.Convert("ボット実行中です!", nil); err == nil {
fmt.Println(runningText)
} else {
panic(err)
}
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
discord.Close()
}