Skip to content
This repository was archived by the owner on Nov 26, 2018. It is now read-only.

Commit e758e0f

Browse files
mvdanyml
authored andcommitted
Don't use "self" for receiver names
Reported by golint: > receiver name should be a reflection of its identity; don't use generic names such as "me", "this", or "self"
1 parent 5396e02 commit e758e0f

5 files changed

Lines changed: 98 additions & 98 deletions

File tree

botbot.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -52,35 +52,35 @@ func NewBotBot(storage common.Storage, queue common.Queue) *BotBot {
5252
}
5353

5454
// Listen for incoming commands
55-
func (self *BotBot) listen(queueName string) {
55+
func (bot *BotBot) listen(queueName string) {
5656

5757
var msg []byte
5858
var err error
5959

6060
for {
61-
_, msg, err = self.queue.Blpop([]string{queueName}, 0)
61+
_, msg, err = bot.queue.Blpop([]string{queueName}, 0)
6262
if err != nil {
6363
glog.Fatal("Error reading (BLPOP) from queue. ", err)
6464
}
6565
if len(msg) != 0 {
6666
if glog.V(1) {
6767
glog.Infoln("Command: ", string(msg))
6868
}
69-
self.fromBus <- string(msg)
69+
bot.fromBus <- string(msg)
7070
}
7171
}
7272
}
7373

74-
func (self *BotBot) mainLoop() {
75-
// TODO (yml) comment out self.recordUserCounts because I think it is
74+
func (bot *BotBot) mainLoop() {
75+
// TODO (yml) comment out bot.recordUserCounts because I think it is
7676
// leaking postgres connection.
77-
//go self.recordUserCounts()
77+
//go bot.recordUserCounts()
7878

7979
var busCommand string
8080
var args string
8181
for {
8282
select {
83-
case serverLine, ok := <-self.fromServer:
83+
case serverLine, ok := <-bot.fromServer:
8484
if !ok {
8585
// Channel is closed, we're offline. Stop.
8686
break
@@ -91,15 +91,15 @@ func (self *BotBot) mainLoop() {
9191
// QUIT and NICK don't have a channel name
9292
// They need to go to all channels the user is in
9393
case "QUIT", "NICK":
94-
self.dis.DispatchMany(serverLine, self.users.In(serverLine.User))
94+
bot.dis.DispatchMany(serverLine, bot.users.In(serverLine.User))
9595

9696
default:
97-
self.dis.Dispatch(serverLine)
97+
bot.dis.Dispatch(serverLine)
9898
}
9999

100-
self.users.Act(serverLine)
100+
bot.users.Act(serverLine)
101101

102-
case busMessage, ok := <-self.fromBus:
102+
case busMessage, ok := <-bot.fromBus:
103103
if !ok {
104104
break
105105
}
@@ -110,7 +110,7 @@ func (self *BotBot) mainLoop() {
110110
args = parts[1]
111111
}
112112

113-
self.handleCommand(busCommand, args)
113+
bot.handleCommand(busCommand, args)
114114
}
115115
}
116116
}
@@ -119,7 +119,7 @@ func (self *BotBot) mainLoop() {
119119
// Current commands:
120120
// - WRITE <chatbotid> <channel> <msg>: Send message to server
121121
// - REFRESH: Reload plugin configuration
122-
func (self *BotBot) handleCommand(cmd string, args string) {
122+
func (bot *BotBot) handleCommand(cmd string, args string) {
123123
if glog.V(2) {
124124
glog.Infoln("HandleCommand:", cmd)
125125
}
@@ -134,41 +134,41 @@ func (self *BotBot) handleCommand(cmd string, args string) {
134134
return
135135
}
136136

137-
self.netMan.Send(chatbotId, parts[1], parts[2])
137+
bot.netMan.Send(chatbotId, parts[1], parts[2])
138138

139139
// Now send it back to ourself, so other plugins see it
140140
internalLine := &line.Line{
141141
ChatBotId: chatbotId,
142142
Raw: args,
143-
User: self.netMan.GetUserByChatbotId(chatbotId),
143+
User: bot.netMan.GetUserByChatbotId(chatbotId),
144144
Command: "PRIVMSG",
145145
Received: time.Now().UTC().Format(time.RFC3339Nano),
146146
Content: parts[2],
147147
Channel: strings.TrimSpace(parts[1])}
148148

149-
self.dis.Dispatch(internalLine)
149+
bot.dis.Dispatch(internalLine)
150150

151151
case "REFRESH":
152152
if glog.V(1) {
153153
glog.Infoln("Reloading configuration from database")
154154
}
155-
self.netMan.RefreshChatbots()
155+
bot.netMan.RefreshChatbots()
156156
}
157157
}
158158

159159
// Writes the number of users per channel, every hour. Run in go routine.
160-
func (self *BotBot) recordUserCounts() {
160+
func (bot *BotBot) recordUserCounts() {
161161

162162
for {
163163

164-
for ch, _ := range self.users.Channels() {
165-
self.storage.SetCount(ch, self.users.Count(ch))
164+
for ch, _ := range bot.users.Channels() {
165+
bot.storage.SetCount(ch, bot.users.Count(ch))
166166
}
167167
time.Sleep(1 * time.Hour)
168168
}
169169
}
170170

171171
// Stop
172-
func (self *BotBot) shutdown() {
173-
self.netMan.Shutdown()
172+
func (bot *BotBot) shutdown() {
173+
bot.netMan.Shutdown()
174174
}

common/queue.go

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ func (mq *MockQueue) Llen(key string) (int, error) {
8989
return len(mq.Got), nil
9090
}
9191

92-
func (self *MockQueue) Ltrim(key string, start int, end int) error {
92+
func (mq *MockQueue) Ltrim(key string, start int, end int) error {
9393
return nil
9494
}
9595

96-
func (self *MockQueue) Ping() (string, error) {
96+
func (mq *MockQueue) Ping() (string, error) {
9797
return "PONG", nil
9898
}
9999

@@ -122,25 +122,25 @@ func NewRedisQueue() Queue {
122122
}
123123

124124
redisQueue := goredis.Client{Addr: redisUrl.Host, Db: redisDb}
125-
s := RedisQueue{queue: &redisQueue}
126-
s.waitForRedis()
127-
return &s
125+
rq := RedisQueue{queue: &redisQueue}
126+
rq.waitForRedis()
127+
return &rq
128128
}
129129

130-
func (self *RedisQueue) waitForRedis() {
130+
func (rq *RedisQueue) waitForRedis() {
131131

132-
_, err := self.queue.Ping()
132+
_, err := rq.queue.Ping()
133133
for err != nil {
134134
glog.Errorln("Waiting for redis...")
135135
time.Sleep(1 * time.Second)
136136

137-
_, err = self.queue.Ping()
137+
_, err = rq.queue.Ping()
138138
}
139139
}
140140

141-
func (self *RedisQueue) Publish(queue string, message []byte) error {
141+
func (rq *RedisQueue) Publish(queue string, message []byte) error {
142142

143-
err := self.queue.Publish(queue, message)
143+
err := rq.queue.Publish(queue, message)
144144
if err == nil {
145145
return nil
146146
}
@@ -150,13 +150,13 @@ func (self *RedisQueue) Publish(queue string, message []byte) error {
150150
return err
151151
}
152152

153-
self.waitForRedis()
154-
return self.Publish(queue, message) // Recurse
153+
rq.waitForRedis()
154+
return rq.Publish(queue, message) // Recurse
155155
}
156156

157-
func (self *RedisQueue) Blpop(keys []string, timeoutsecs uint) (*string, []byte, error) {
157+
func (rq *RedisQueue) Blpop(keys []string, timeoutsecs uint) (*string, []byte, error) {
158158

159-
key, val, err := self.queue.Blpop(keys, timeoutsecs)
159+
key, val, err := rq.queue.Blpop(keys, timeoutsecs)
160160
if err == nil {
161161
return key, val, nil
162162
}
@@ -166,13 +166,13 @@ func (self *RedisQueue) Blpop(keys []string, timeoutsecs uint) (*string, []byte,
166166
return key, val, err
167167
}
168168

169-
self.waitForRedis()
170-
return self.Blpop(keys, timeoutsecs) // Recurse
169+
rq.waitForRedis()
170+
return rq.Blpop(keys, timeoutsecs) // Recurse
171171
}
172172

173-
func (self *RedisQueue) Rpush(key string, val []byte) error {
173+
func (rq *RedisQueue) Rpush(key string, val []byte) error {
174174

175-
err := self.queue.Rpush(key, val)
175+
err := rq.queue.Rpush(key, val)
176176
if err == nil {
177177
return nil
178178
}
@@ -182,13 +182,13 @@ func (self *RedisQueue) Rpush(key string, val []byte) error {
182182
return err
183183
}
184184

185-
self.waitForRedis()
186-
return self.Rpush(key, val) // Recurse
185+
rq.waitForRedis()
186+
return rq.Rpush(key, val) // Recurse
187187
}
188188

189-
func (self *RedisQueue) Lpush(key string, val []byte) error {
189+
func (rq *RedisQueue) Lpush(key string, val []byte) error {
190190

191-
err := self.queue.Lpush(key, val)
191+
err := rq.queue.Lpush(key, val)
192192
if err == nil {
193193
return nil
194194
}
@@ -198,13 +198,13 @@ func (self *RedisQueue) Lpush(key string, val []byte) error {
198198
return err
199199
}
200200

201-
self.waitForRedis()
202-
return self.Lpush(key, val) // Recurse
201+
rq.waitForRedis()
202+
return rq.Lpush(key, val) // Recurse
203203
}
204204

205-
func (self *RedisQueue) Llen(key string) (int, error) {
205+
func (rq *RedisQueue) Llen(key string) (int, error) {
206206

207-
size, err := self.queue.Llen(key)
207+
size, err := rq.queue.Llen(key)
208208
if err == nil {
209209
return size, nil
210210
}
@@ -214,13 +214,13 @@ func (self *RedisQueue) Llen(key string) (int, error) {
214214
return size, err
215215
}
216216

217-
self.waitForRedis()
218-
return self.Llen(key) // Recurse
217+
rq.waitForRedis()
218+
return rq.Llen(key) // Recurse
219219
}
220220

221-
func (self *RedisQueue) Ltrim(key string, start int, end int) error {
221+
func (rq *RedisQueue) Ltrim(key string, start int, end int) error {
222222

223-
err := self.queue.Ltrim(key, start, end)
223+
err := rq.queue.Ltrim(key, start, end)
224224
if err == nil {
225225
return nil
226226
}
@@ -230,10 +230,10 @@ func (self *RedisQueue) Ltrim(key string, start int, end int) error {
230230
return err
231231
}
232232

233-
self.waitForRedis()
234-
return self.Ltrim(key, start, end) // Recurse
233+
rq.waitForRedis()
234+
return rq.Ltrim(key, start, end) // Recurse
235235
}
236236

237-
func (self *RedisQueue) Ping() (string, error) {
238-
return self.queue.Ping()
237+
func (rq *RedisQueue) Ping() (string, error) {
238+
return rq.queue.Ping()
239239
}

common/storage.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ func NewMockStorage(serverPort string) Storage {
3737
return &MockStorage{botConfs: []*BotConfig{botConf}}
3838
}
3939

40-
func (self *MockStorage) BotConfig() []*BotConfig {
41-
return self.botConfs
40+
func (ms *MockStorage) BotConfig() []*BotConfig {
41+
return ms.botConfs
4242
}
4343

44-
func (self *MockStorage) SetCount(channel string, count int) error {
44+
func (ms *MockStorage) SetCount(channel string, count int) error {
4545
return nil
4646
}
4747

@@ -80,15 +80,15 @@ func NewPostgresStorage() *PostgresStorage {
8080
return &PostgresStorage{db}
8181
}
8282

83-
func (self *PostgresStorage) BotConfig() []*BotConfig {
83+
func (ms *PostgresStorage) BotConfig() []*BotConfig {
8484

8585
var err error
8686
var rows *sql.Rows
8787

8888
configs := make([]*BotConfig, 0)
8989

9090
sql := "SELECT id, server, server_password, nick, password, real_name, server_identifier FROM bots_chatbot WHERE is_active=true"
91-
rows, err = self.db.Query(sql)
91+
rows, err = ms.db.Query(sql)
9292
if err != nil {
9393
glog.Fatal("Error running: ", sql, " ", err)
9494
}
@@ -120,7 +120,7 @@ func (self *PostgresStorage) BotConfig() []*BotConfig {
120120
configs = append(configs, config)
121121
glog.Infoln("config.Id:", config.Id)
122122
}
123-
channelStmt, err := self.db.Prepare("SELECT id, name, password, fingerprint FROM bots_channel WHERE is_active=true and chatbot_id=$1")
123+
channelStmt, err := ms.db.Prepare("SELECT id, name, password, fingerprint FROM bots_channel WHERE is_active=true and chatbot_id=$1")
124124
if err != nil {
125125
glog.Fatal("[Error] Error while preparing the statements to retrieve the channel:", err)
126126
}
@@ -148,12 +148,12 @@ func (self *PostgresStorage) BotConfig() []*BotConfig {
148148
return configs
149149
}
150150

151-
func (self *PostgresStorage) SetCount(channel string, count int) error {
151+
func (ms *PostgresStorage) SetCount(channel string, count int) error {
152152

153153
now := time.Now()
154154
hour := now.Hour()
155155

156-
channelId, err := self.channelId(channel)
156+
channelId, err := ms.channelId(channel)
157157
if err != nil {
158158
return err
159159
}
@@ -163,7 +163,7 @@ func (self *PostgresStorage) SetCount(channel string, count int) error {
163163
updateSQL := "UPDATE bots_usercount SET counts[$1] = $2 WHERE channel_id = $3 AND dt = $4"
164164

165165
var res sql.Result
166-
res, err = self.db.Exec(updateSQL, hour, count, channelId, now)
166+
res, err = ms.db.Exec(updateSQL, hour, count, channelId, now)
167167
if err != nil {
168168
return err
169169
}
@@ -183,13 +183,13 @@ func (self *PostgresStorage) SetCount(channel string, count int) error {
183183

184184
insSQL := "INSERT INTO bots_usercount (channel_id, dt, counts) VALUES ($1, $2, '{NULL}')"
185185

186-
_, err = self.db.Exec(insSQL, channelId, now)
186+
_, err = ms.db.Exec(insSQL, channelId, now)
187187
if err != nil {
188188
return err
189189
}
190190

191191
// Run the update again
192-
_, err = self.db.Query(updateSQL, hour, count, channelId, now)
192+
_, err = ms.db.Query(updateSQL, hour, count, channelId, now)
193193
if err != nil {
194194
return err
195195
}
@@ -198,12 +198,12 @@ func (self *PostgresStorage) SetCount(channel string, count int) error {
198198
}
199199

200200
// The channel Id for a given channel name
201-
func (self *PostgresStorage) channelId(name string) (int, error) {
201+
func (ms *PostgresStorage) channelId(name string) (int, error) {
202202

203203
var channelId int
204204
query := "SELECT id from bots_channel WHERE name = $1"
205205

206-
rows, err := self.db.Query(query, name)
206+
rows, err := ms.db.Query(query, name)
207207
if err != nil {
208208
return -1, err
209209
}
@@ -220,6 +220,6 @@ func (self *PostgresStorage) channelId(name string) (int, error) {
220220
return channelId, nil
221221
}
222222

223-
func (self *PostgresStorage) Close() error {
224-
return self.db.Close()
223+
func (ms *PostgresStorage) Close() error {
224+
return ms.db.Close()
225225
}

0 commit comments

Comments
 (0)