This repository has been archived by the owner on Feb 14, 2019. It is now read-only.
/
trans.go
81 lines (71 loc) · 1.6 KB
/
trans.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
package plugin
import (
"log"
"strings"
"github.com/jqs7/Jqs7Bot/conf"
"github.com/st3v/translator"
"github.com/st3v/translator/microsoft"
)
type Trans struct{ Default }
func (t *Trans) Run() {
if len(t.Args) < 2 {
if t.Message.ReplyToMessage != nil &&
t.Message.ReplyToMessage.Text != "" {
in := t.Message.ReplyToMessage.Text
result := t.translator(in)
t.NewMessage(t.ChatID, result).Send()
} else {
t.setStatus("trans")
}
} else if len(t.Args) >= 2 {
in := strings.Join(t.Args[1:], " ")
result := t.translator(in)
t.NewMessage(t.ChatID, result).Send()
}
}
func (t *Default) translator(in string) string {
result := make(chan string)
typingChan := make(chan bool)
go func() {
t.NewChatAction(t.ChatID).Typing().Send()
typingChan <- true
}()
go func() {
result <- ZhTrans(in)
}()
<-typingChan
return <-result
}
type MsTrans struct {
t translator.Translator
}
func (m *MsTrans) New() {
m.t = microsoft.NewTranslator(
conf.GetItem("msTransId"),
conf.GetItem("msTransSecret"))
}
func (m *MsTrans) Detect(in string) (string, error) {
return m.t.Detect(in)
}
func (m *MsTrans) Trans(in, from, to string) (string, error) {
return m.t.Translate(in, from, to)
}
func ZhTrans(in string) (out string) {
m := &MsTrans{}
m.New()
from, err := m.Detect(in)
if err != nil {
log.Println(err.Error())
return "警报!弹药系统过载!请放宽后重试"
}
switch from {
case "zh-CHS", "zh-CHT":
out, err = m.Trans(in, from, "en")
default:
out, err = m.Trans(in, from, "zh-CHS")
}
if err != nil {
return "可怜的群组娘被母舰放逐了X﹏X"
}
return out
}