-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent_add.go
330 lines (273 loc) · 11.1 KB
/
event_add.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package main
import (
"database/sql"
"fmt"
"strconv"
"strings"
"time"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)
func (t *TeleBot) eventAdd(u tgbotapi.Update) bool {
var userIDDB string
var userTypeDB string
db := dbConn("football_bot")
stmt, err := db.Prepare("select id, user_type from users where user_name=?")
if err != nil {
t.botAPI.Send(tgbotapi.NewMessage(u.Message.Chat.ID, "\u26A0 System Error "+t.HandleError(err)))
return false
}
defer stmt.Close()
err = stmt.QueryRow(u.Message.From.UserName).Scan(&userIDDB, &userTypeDB)
if err != nil {
if err == sql.ErrNoRows {
msg := `No such user here
Try to run command /start firstly and then try again`
t.botAPI.Send(tgbotapi.NewMessage(u.Message.Chat.ID, "\u26A0 "+msg))
return false
}
t.HandleError(err)
}
if userTypeDB != "organizer" {
msg := "Forbidden! Only Organizers can create Games!"
t.botAPI.Send(tgbotapi.NewMessage(u.Message.Chat.ID, "\u26A0 "+msg+" "))
} else {
t.event.eventUserID = userIDDB
t.event.eventOrganizer = u.Message.From.UserName
}
return true
}
func (t *TeleBot) addCalendar(u tgbotapi.Update) {
var nextMonth time.Month
var selYear int
year, month, date := time.Now().Date()
currDate := date
td := time.Date(year, month+1, 0, 0, 0, 0, 0, time.UTC)
if month == 12 {
nextMonth = 1
selYear = td.Year() + 1
} else {
nextMonth = td.Month() + 1
selYear = year
}
varWeekDate := weekDayToInt[time.Now().Weekday().String()]
if varWeekDate <= 0 || varWeekDate > 7 {
varWeekDate = 1
}
firstDayCurrentMonth := time.Date(td.Year(), td.Month(), 1, 0, 0, 0, 0, time.Local)
lastDayCurrentMonth := firstDayCurrentMonth.AddDate(0, 1, 0).Add(time.Nanosecond * -1)
sliceWeek := []string{}
sliceMonth := [][]string{}
for i := 0; i < varWeekDate-1; i++ {
sliceWeek = append(sliceWeek, "0")
}
for n := 0; n <= 14; n++ {
if n >= varWeekDate {
sliceWeek = append(sliceWeek, strconv.Itoa(date))
date++
}
if date == lastDayCurrentMonth.Day() {
date = 1
sliceWeek = append(sliceWeek, strconv.Itoa(lastDayCurrentMonth.Day()))
}
if len(sliceWeek) == 7 {
sliceMonth = append(sliceMonth, sliceWeek)
sliceWeek = []string{}
}
}
msg := tgbotapi.NewMessage(u.Message.Chat.ID, "Select date:")
keyboard := tgbotapi.InlineKeyboardMarkup{}
var rowMonth []tgbotapi.InlineKeyboardButton
var rowDays []tgbotapi.InlineKeyboardButton
var rowDatesWeek1 []tgbotapi.InlineKeyboardButton
var rowDatesWeek2 []tgbotapi.InlineKeyboardButton
days := []string{"Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"}
selMonth := month.String()
currMonth := tgbotapi.NewInlineKeyboardButtonData(month.String()+" "+strconv.Itoa(year), "eventDate#\u0001"+month.String()+" "+strconv.Itoa(year))
rowMonth = append(rowMonth, currMonth)
for _, d := range days {
btDay := tgbotapi.NewInlineKeyboardButtonData(d, "eventDate#\u0002#"+d+"#"+strconv.Itoa(varWeekDate))
rowDays = append(rowDays, btDay)
}
for i := 0; i < 7; i++ {
if sliceMonth[0][i] == "0" {
sliceMonth[0][i] = "\u0000"
selMonth = "0"
} else {
selMonth = month.String()
}
fmt.Printf("DEBUG MONTH: %v, Date: %v\n", sliceMonth, currDate)
if sliceMonth[0][i] > "0" && sliceMonth[0][i] < strconv.Itoa(currDate) {
selMonth = nextMonth.String()
fmt.Printf("Next month: %v, date: %v\n", selMonth, currDate)
}
fmt.Printf("Select Month: %v\n", selMonth)
btDate := tgbotapi.NewInlineKeyboardButtonData(sliceMonth[0][i], "eventDate#"+sliceMonth[0][i]+" "+selMonth+" "+strconv.Itoa(selYear))
rowDatesWeek1 = append(rowDatesWeek1, btDate)
}
for i := 0; i < 7; i++ {
btDate := tgbotapi.NewInlineKeyboardButtonData(sliceMonth[1][i], "eventDate#"+sliceMonth[1][i]+" "+selMonth+" "+strconv.Itoa(selYear))
rowDatesWeek2 = append(rowDatesWeek2, btDate)
}
keyboard.InlineKeyboard = append(keyboard.InlineKeyboard, rowMonth)
keyboard.InlineKeyboard = append(keyboard.InlineKeyboard, rowDays)
keyboard.InlineKeyboard = append(keyboard.InlineKeyboard, rowDatesWeek1)
keyboard.InlineKeyboard = append(keyboard.InlineKeyboard, rowDatesWeek2)
msg.ReplyMarkup = keyboard
msg.Text = "Select Date:"
t.botAPI.Send(msg)
}
func (t *TeleBot) eventDate(u tgbotapi.Update) {
resCallBackQuery := strings.SplitN(u.CallbackQuery.Data, "#", -1)
fmt.Printf("Selected Date: %v\n", resCallBackQuery[1])
t.botAPI.AnswerCallbackQuery(tgbotapi.NewCallback(u.CallbackQuery.ID, "Selected Date: "+resCallBackQuery[1]))
t.botAPI.Send(tgbotapi.NewMessage(u.CallbackQuery.Message.Chat.ID, "Selected Date: "+resCallBackQuery[1]))
t.event.eventDate = resCallBackQuery[1]
t.addTime(u)
}
func (t *TeleBot) addTime(u tgbotapi.Update) {
del := tgbotapi.NewDeleteMessage(u.CallbackQuery.Message.Chat.ID, u.CallbackQuery.Message.MessageID)
config := tgbotapi.NewCallback(u.CallbackQuery.ID, strconv.Itoa(u.CallbackQuery.Message.MessageID))
go t.botAPI.AnswerCallbackQuery(config)
go t.botAPI.Send(del)
go t.creatInlineKeyboardTimeMarkup(u.CallbackQuery.Message.Chat.ID)
go t.emptyAnswer(u.CallbackQuery.ID)
}
func (t *TeleBot) eventTime(u tgbotapi.Update) {
resCallBackQuery := strings.SplitN(u.CallbackQuery.Data, "#", -1)
fmt.Printf("Selected Time: %v\n", resCallBackQuery[1])
t.botAPI.AnswerCallbackQuery(tgbotapi.NewCallback(u.CallbackQuery.ID, "Selected Time: "+resCallBackQuery[1]))
t.botAPI.Send(tgbotapi.NewMessage(u.CallbackQuery.Message.Chat.ID, "Selected Time: "+resCallBackQuery[1]))
t.event.eventTime = resCallBackQuery[1]
t.teamSize(u)
}
func (t *TeleBot) teamSize(u tgbotapi.Update) {
del := tgbotapi.NewDeleteMessage(u.CallbackQuery.Message.Chat.ID, u.CallbackQuery.Message.MessageID)
config := tgbotapi.NewCallback(u.CallbackQuery.ID, strconv.Itoa(u.CallbackQuery.Message.MessageID))
go t.botAPI.AnswerCallbackQuery(config)
go t.botAPI.Send(del)
go t.creatInlineKeyboardTeamSizeMarkup(u.CallbackQuery.Message.Chat.ID)
go t.emptyAnswer(u.CallbackQuery.ID)
}
func (t *TeleBot) creatInlineKeyboardTeamSizeMarkup(chatID int64) {
msg := tgbotapi.NewMessage(chatID, "Pitch Size?")
msg.ReplyMarkup = tgbotapi.NewInlineKeyboardMarkup(
tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData("5x5", "pitchSize#5"),
tgbotapi.NewInlineKeyboardButtonData("7x7", "pitchSize#7"),
tgbotapi.NewInlineKeyboardButtonData("9x9", "pitchSize#9"),
tgbotapi.NewInlineKeyboardButtonData("11x11", "pitchSize#11"),
),
)
t.botAPI.Send(msg)
}
func (t *TeleBot) creatInlineKeyboardTimeMarkup(chatID int64) {
msg := tgbotapi.NewMessage(chatID, "Please select Event Time:")
msg.ReplyMarkup = tgbotapi.NewInlineKeyboardMarkup(
tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData("07:00", "eventTime#07:00"),
tgbotapi.NewInlineKeyboardButtonData("08:00", "eventTime#08:00"),
tgbotapi.NewInlineKeyboardButtonData("09:00", "eventTime#09:00"),
tgbotapi.NewInlineKeyboardButtonData("10:00", "eventTime#10:00"),
tgbotapi.NewInlineKeyboardButtonData("11:00", "eventTime#11:00"),
),
tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData("12:00", "eventTime#12:00"),
tgbotapi.NewInlineKeyboardButtonData("13:00", "eventTime#13:00"),
tgbotapi.NewInlineKeyboardButtonData("14:00", "eventTime#14:00"),
tgbotapi.NewInlineKeyboardButtonData("15:00", "eventTime#15:00"),
tgbotapi.NewInlineKeyboardButtonData("16:00", "eventTime#16:00"),
),
tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData("17:00", "eventTime#17:00"),
tgbotapi.NewInlineKeyboardButtonData("18:00", "eventTime#18:00"),
tgbotapi.NewInlineKeyboardButtonData("19:00", "eventTime#19:00"),
tgbotapi.NewInlineKeyboardButtonData("20:00", "eventTime#20:00"),
tgbotapi.NewInlineKeyboardButtonData("21:00", "eventTime#21:00"),
),
)
t.botAPI.Send(msg)
}
func (t *TeleBot) emptyAnswer(CallbackQueryID string) {
configAlert := tgbotapi.NewCallback(CallbackQueryID, "")
t.botAPI.AnswerCallbackQuery(configAlert)
}
func (t *TeleBot) pitchSize(u tgbotapi.Update) {
del := tgbotapi.NewDeleteMessage(u.CallbackQuery.Message.Chat.ID, u.CallbackQuery.Message.MessageID)
config := tgbotapi.NewCallback(u.CallbackQuery.ID, strconv.Itoa(u.CallbackQuery.Message.MessageID))
go t.botAPI.AnswerCallbackQuery(config)
go t.botAPI.Send(del)
resCallBackQuery := strings.SplitN(u.CallbackQuery.Data, "#", -1)
fmt.Printf("Pitch Size: %v\n", resCallBackQuery[1])
t.botAPI.AnswerCallbackQuery(tgbotapi.NewCallback(u.CallbackQuery.ID, "Pitch Size: "+resCallBackQuery[1]+"x"+resCallBackQuery[1]))
t.botAPI.Send(tgbotapi.NewMessage(u.CallbackQuery.Message.Chat.ID, "Pitch Size: "+resCallBackQuery[1]+"x"+resCallBackQuery[1]))
t.event.eventTeamPlayers = resCallBackQuery[1]
}
func (t *TeleBot) dbUpdateEventAdd(u tgbotapi.Update) {
fmt.Printf("Updating DB| Date:%s, Organizer: %s, Team Size: %s\n", t.event.eventDate, t.event.eventOrganizer, t.event.eventTeamPlayers)
fmtDateSlice := strings.SplitN(t.event.eventDate, " ", -1)
myDateString := fmtDateSlice[2] + "-" + fmtDateSlice[1] + "-" + fmtDateSlice[0] + " " + t.event.eventTime
myDate, err := time.Parse("2006-January-2 15:04", myDateString)
if err != nil {
t.botAPI.Send(tgbotapi.NewMessage(u.Message.Chat.ID, "\u26A0 System Error "+t.HandleError(err)))
return
}
db := dbConn("football_bot")
query := `REPLACE INTO events(bot_id,event_time,event_team_players,event_organizer,event_info,event_status) VALUES (?,?,?,?,?,?);`
stmt, err := db.Prepare(query)
if err != nil {
db.Close()
t.botAPI.Send(tgbotapi.NewMessage(u.Message.Chat.ID, "\u26A0 System Error "+t.HandleError(err)))
return
}
_, err = stmt.Exec(botID, myDate.Format("2006-01-02 15:04:00"), t.event.eventTeamPlayers, t.event.eventUserID, "No Data", 1)
if err != nil {
db.Close()
t.botAPI.Send(tgbotapi.NewMessage(u.Message.Chat.ID, "\u26A0 System Error "+t.HandleError(err)))
return
}
var dbPlayerID int64
allPlayersID := make([]int64, 0)
rows, err := db.Query("select user_id from users where bot_id=?", botID)
if err != nil {
t.botAPI.Send(tgbotapi.NewMessage(u.Message.Chat.ID, "\u26A0 System Error "+t.HandleError(err)))
return
}
defer rows.Close()
for rows.Next() {
err := rows.Scan(&dbPlayerID)
if err != nil {
t.HandleError(err)
return
}
allPlayersID = append(allPlayersID, dbPlayerID)
}
err = rows.Err()
if err != nil {
t.botAPI.Send(tgbotapi.NewMessage(u.Message.Chat.ID, "\u26A0 System Error "+t.HandleError(err)))
return
}
stmt, err = db.Prepare("select event_time from events where event_organizer=?")
if err != nil {
t.botAPI.Send(tgbotapi.NewMessage(u.Message.Chat.ID, "\u26A0 System Error "+t.HandleError(err)))
return
}
defer stmt.Close()
err = stmt.QueryRow(t.event.eventUserID).Scan(&t.event.eventTime)
if err != nil {
if err == sql.ErrNoRows {
msg := "\u26A0 " + `No Games here
To create a new game run /new_game command`
t.botAPI.Send(tgbotapi.NewMessage(u.Message.Chat.ID, msg))
}
return
}
db.Close()
td, _ := time.Parse(sqlDateTimeForm, t.event.eventTime)
fmt.Printf("t Event Time: %v\n", t.event.eventTime)
for _, playerID := range allPlayersID {
msg := `New Game has been Scheduled on ` + strconv.Itoa(td.Day()) + ` of ` + td.Month().String() + ` at ` + strconv.Itoa(td.Hour()) + `:00
To add yourself to the Game - type /add
Type /help for more information`
t.botAPI.Send(tgbotapi.NewMessage(playerID, "\u26BD "+msg))
}
}