-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparrot.go
101 lines (85 loc) · 2.57 KB
/
parrot.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
// Copyright (c) 2020-2022, The OneBot Contributors. All rights reserved.
package main
import (
"strings"
"sync"
"github.com/TheDiscordian/onebot/onelib"
)
const (
// NAME is same as filename, minus extension
NAME = "parrot"
// LONGNAME is what's presented to the user
LONGNAME = "Parrot Plugin"
// VERSION of the plugin
VERSION = "v0.0.0"
)
// Load returns the Plugin object.
func Load() onelib.Plugin {
pp := new(ParrotPlugin)
pp.msgs = make(map[onelib.UUID]string, 1)
pp.monitor = &onelib.Monitor{
OnMessageWithText: pp.OnMessageWithText,
}
return pp
}
func reverse(s string) string {
runes := []rune(s)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}
func revParrot(msg onelib.Message, sender onelib.Sender) {
sender.Location().SendText(reverse(msg.Text()))
}
func parrot(msg onelib.Message, sender onelib.Sender) {
sender.Location().SendFormattedText(msg.Text(), msg.FormattedText())
}
func formatParrot(msg onelib.Message, sender onelib.Sender) {
sender.Location().SendFormattedText(msg.Text(), msg.Text())
}
// ParrotPlugin is an object for satisfying the Plugin interface.
type ParrotPlugin struct {
monitor *onelib.Monitor
msgs map[onelib.UUID]string
lock sync.RWMutex
}
// Name returns the name of the plugin, usually the filename.
func (pp *ParrotPlugin) Name() string {
return NAME
}
// LongName returns the display name of the plugin.
func (pp *ParrotPlugin) LongName() string {
return LONGNAME
}
// Version returns the version of the plugin, usually in the format of "v0.0.0".
func (pp *ParrotPlugin) Version() string {
return VERSION
}
func (pp *ParrotPlugin) OnMessageWithText(from onelib.Sender, msg onelib.Message) {
var splitMsg []string
txt := msg.Text()
if strings.HasPrefix(txt, "s/") || strings.HasPrefix(txt, "ss/") {
splitMsg = strings.Split(txt, "/")
if len(splitMsg) < 2 {
return
}
} else {
pp.lock.Lock()
pp.msgs[from.Location().UUID()] = txt
pp.lock.Unlock()
return
}
loc := from.Location()
pp.lock.RLock()
outMsg := strings.ReplaceAll(pp.msgs[loc.UUID()], splitMsg[1], splitMsg[2])
pp.lock.RUnlock()
loc.SendText(outMsg)
}
// Implements returns a map of commands and monitor the plugin implements.
func (pp *ParrotPlugin) Implements() (map[string]onelib.Command, *onelib.Monitor) {
return map[string]onelib.Command{"say": parrot, "s": parrot, "r": revParrot, "rev": revParrot, "format": formatParrot, "form": formatParrot}, pp.monitor
}
// Remove is necessary to satisfy the Plugin interface, it does nothing.
func (pp *ParrotPlugin) Remove() {
}