/
rcp.go
140 lines (124 loc) · 3.67 KB
/
rcp.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
// rcp.go version 4.0.0
package main
import "fmt"
import "os"
import "strconv"
import "time"
import "github.com/bwmarrin/discordgo"
func main() {
if (len(os.Args) < 3) &&
(len(os.Args) < 2 || os.Args[1] != "reminder") {
fmt.Fprintf(os.Stderr, "usage: %s vote emoji\n" +
" %s reminder", os.Args[0])
os.Exit(1)
}
if os.Args[1] == "msg" {
return
}
buf, _ := os.ReadFile("nomic/poll-thread-id")
thread := string(buf[:len(buf) - 1])
buf, _ = os.ReadFile("nomic/poll-msg-id")
msg := string(buf[:len(buf) - 1])
players, _ := os.ReadDir("nomic/players")
numplayers := len(players)
discord, err := discordgo.New("Bot " + os.Getenv("key"))
if err != nil {
fmt.Fprintf(os.Stderr, "discordgo.New: %s\n", err.Error())
os.Exit(1)
}
y, _ := discord.MessageReactions(thread, msg, "✅", numplayers, "", "")
n, _ := discord.MessageReactions(thread, msg, "❌", numplayers, "", "")
if os.Args[1] == "reminder" {
players, _ := os.ReadDir("nomic/players")
var novote []string
for _, player := range players {
found := false
buf, _ := os.ReadFile("nomic/players/" + player.Name())
id := string(buf[:len(buf) - 1])
for _, vote := range y {
if vote.ID == id {
found = true
break
}
}
if ! found {
for _, vote := range n {
if vote.ID == id {
found = true
break
}
}
}
if ! found {
novote = append(novote, id)
}
}
txt := "The following players have not voted:"
for _, id := range novote {
user, _ := discord.User(id)
txt = txt + "\n" + user.Mention()
}
buf, _ := os.ReadFile("nomic/channels/announcements")
channel := string(buf[:len(buf) - 1])
discord.ChannelMessageSend(
channel,
txt + "\n" + "This is your 48-hour reminder to vote.")
return
}
if len(y) + len(n) == numplayers {
buf, _ := os.ReadFile("nomic/channels/announcements")
channel := string(buf[:len(buf) - 1])
discord.ChannelMessageSend(
channel,
"<@&1001702350571974716>, " +
"all the votes are in! In 15 minutes the votes will be tallied " +
"unless someone retracts their vote.")
os.WriteFile("nomic/phase", []byte("dummy\n"), 0644)
dur, _ := time.ParseDuration("15m")
time.Sleep(dur)
y, _ = discord.MessageReactions(thread, msg, "✅", numplayers, "", "")
n, _ = discord.MessageReactions(thread, msg, "❌", numplayers, "", "")
if len(y) + len(n) == numplayers {
buf, _ := os.ReadFile("nomic/rcp")
rcp, _ := strconv.Atoi(string(buf))
if len(n) > len(y) {
discord.ChannelMessageSend(
channel,
"<@&1001702350571974716>, " +
"the RCP failed to receive a majority vote. No points " +
"have been awarded, and the player who proposed this " +
"rule has lost 10 points.")
} else if rcp > 319 && len(n) > 1 {
discord.ChannelMessageSend(
channel,
"<@&1001702350571974716>, " +
"the RCP failed to receive a unanimous vote. No points " +
"have been awarded, and the player who proposed this " +
"rule has lost 10 points.")
} else {
discord.ChannelMessageSend(
channel,
"<@&1001702350571974716>, " +
"the RCP has passed the vote. " +
strconv.Itoa((rcp - 291) * len(y) / len(n)) +
" points have been awarded to the player who proposed " +
"this rule.")
}
os.WriteFile(
"nomic/rcp",
[]byte(strconv.Itoa(rcp + 1) + "\n"),
0644)
os.Remove("nomic/poll-msg-id")
os.Remove("nomic/author")
players, _ := os.ReadDir("nomic/players")
os.Symlink(
"../players/" + players[(rcp - 300) % len(players)].Name(),
"nomic/author")
} else {
discord.ChannelMessageSend(
channel,
"<@&1001702350571974716>, " +
"votes were retracted; resuming the poll.")
}
}
}