-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
141 lines (129 loc) · 2.96 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"strings"
"syscall"
"cloud.google.com/go/translate"
"github.com/bwmarrin/discordgo"
"github.com/joho/godotenv"
"golang.org/x/text/language"
"mvdan.cc/xurls/v2"
)
var (
ctx context.Context
client *translate.Client
)
func init() {
err := godotenv.Load(".env")
if err != nil {
log.Fatal(err)
}
ctx = context.Background()
client, err = translate.NewClient(ctx)
defer client.Close()
if err != nil {
log.Fatal(err)
}
}
func main() {
botToken := os.Getenv("DISCORD_BOT_TOKEN")
if botToken == "" {
log.Fatal("DISCORD_BOT_TOKEN is not set.")
}
dg, err := discordgo.New("Bot " + botToken)
if err != nil {
log.Fatal(err)
}
defer dg.Close()
dg.AddHandler(messageCreate)
dg.Identify.Intents = discordgo.IntentsGuildMessages
err = dg.Open()
if err != nil {
log.Fatal(err)
}
fmt.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
}
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
roles, err := s.GuildRoles(m.GuildID)
if err != nil {
return
}
var detectedRole *discordgo.Role
for _, role := range roles {
if role.Name == "translation" || role.Name == "translation bot" {
detectedRole = role
break
}
}
channel, err := s.Channel(m.ChannelID)
if err != nil {
return
}
isSendable := false
for _, permission := range channel.PermissionOverwrites {
if permission.ID == detectedRole.ID &&
permission.Type == discordgo.PermissionOverwriteTypeRole &&
(permission.Allow == 2048 || permission.Allow == 3072) {
isSendable = true
break
}
}
if isSendable == false {
return
}
mes := m.Content
lang, err := DetectLanguage(mes)
if err != nil {
return
}
trans := Translate(mes, lang)
result := AddBracketOnUrl(trans)
s.ChannelMessageSend(m.ChannelID, m.Author.Username + ": " + result)
}
func DetectLanguage(mes string) (language.Tag, error) {
lang, err := client.DetectLanguage(ctx, []string{mes})
if err != nil {
log.Fatal(err)
}
if len(lang) == 0 || len(lang[0]) == 0 {
return language.English, fmt.Errorf("DetectLanguage return value empty")
}
return lang[0][0].Language, nil
}
func Translate(mes string, lang language.Tag) string {
targetLang := language.English
option := &translate.Options{
Source: language.Japanese,
Format: translate.Text,
}
if lang != language.Japanese {
targetLang = language.Japanese
option.Source = lang
}
res, err := client.Translate(ctx, []string{mes}, targetLang, option)
if err != nil {
log.Fatal(err)
}
trans := []string{}
for _, r := range res {
trans = append(trans, r.Text)
}
return strings.Join(trans, "\n")
}
func AddBracketOnUrl(mes string) string {
reg := xurls.Relaxed()
fixedMsg := reg.ReplaceAllFunc([]byte(mes), func(url []byte) []byte {
return append(append([]byte{'<'}, url...), byte('>'))
})
return string(fixedMsg)
}