-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBot.cpp
More file actions
172 lines (143 loc) · 4.66 KB
/
Bot.cpp
File metadata and controls
172 lines (143 loc) · 4.66 KB
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
#include "Bot.h"
#include <sstream>
std::pair<time_t, time_t> getWeekInterval(const std::tm& timeData);
UnicornBot::UnicornBot(const std::string token, const char numOfThreads): SleepyDiscord::DiscordClient(token, numOfThreads), isDBready(true), disp(db), handler(db)
{
db.open("database.db");
int ec = db.lastResultCode();
if (ec != 0) //Change magic 0 error code
isDBready = false;
}
void UnicornBot::onMessage(SleepyDiscord::Message message)
{
if (message.author.bot)
return;
auto channel = getChannel(message.channelID);
if (channel.cast().type != SleepyDiscord::Channel::ChannelType::DM)
return;
if (!isDBready)
{
sendMessage(message.channelID, "Database not connected! Please, contact the administrator.");
return;
}
std::stringstream ss;
ss << message.content;
std::string command;
ss >> command;
std::string reply;
if (command == "help")
{
reply = handleHelpCommand();
}
else if (command == "add")
{
reply = handleAddCommand(ss, message);
}
else if (command == "status")
{
reply = handleStatusCommand(message);
}
else
{
return;
}
sendMessage(message.channelID, reply);
}
std::string UnicornBot::handleHelpCommand()
{
std::stringstream answer;
answer << "How to use bot:" << std::endl << std::endl;
answer << "add <task> <CU>" << std::endl;
answer << "Logging <CU> content units for the <task>" << std::endl << std::endl;
answer << "status" << std::endl;
answer << "Show all tracked CUs for the current week" << std::endl;
return answer.str();
}
std::string UnicornBot::handleAddCommand(std::stringstream& ss, const SleepyDiscord::Message& message)
{
std::time_t result = std::time(nullptr);
std::tm time = *std::localtime(&result);
std::string task;
ss >> task;
int CU;
ss >> CU;
Worktime entry;
entry.Username = message.author.username;
entry.Task = task;
entry.CU = CU;
entry.timestamp = std::mktime(&time);
WorktimeWriteEntryCommand command;
command.data.push_back(entry);
disp.execute(command);
std::stringstream answer;
answer << "I've added " << CU << " CUs for the task " << task;
return answer.str();
}
std::string UnicornBot::handleStatusCommand(const SleepyDiscord::Message& message)
{
std::time_t result = std::time(nullptr);
std::tm time = *std::localtime(&result);
auto getWeekReport = [this, &message](const std::tm& time) -> std::string
{
std::pair<time_t, time_t> week = getWeekInterval(time);
WorktimeTimeIntervalQuery query;
query.Username = message.author.username;
query.timestampFrom = week.first;
query.timestampTo = week.second;
std::vector<Worktime> res = handler.request(query);
std::stringstream answer;
int totalCU = 0;
std::map<std::string, int> cuForTask;
for (const auto& entry: res)
{
cuForTask[entry.Task] += entry.CU;
}
std::tm fromTime = *std::localtime(&week.first);
std::tm toTime = *std::localtime(&week.second);
answer << "Status on week from " << fromTime.tm_mday << "."
<< fromTime.tm_mon + 1 << "." << fromTime.tm_year + 1900 << " to "
<< toTime.tm_mday << "."
<< toTime.tm_mon + 1 << "." << toTime.tm_year + 1900 << std::endl;
for (const auto& task: cuForTask)
{
totalCU += task.second;
answer << task.first << " : " << task.second << std::endl;
}
answer << "Total CU for a week: " << totalCU << std::endl;
return answer.str();
};
std::string currentWeekReport = getWeekReport(time);
time.tm_mday -= 7;
std::time_t prevWeekTimestamp = std::mktime(&time);
std::tm prevWeekTime = *std::localtime(&prevWeekTimestamp);
std::string prevWeekReport = getWeekReport(prevWeekTime);
return prevWeekReport + currentWeekReport;
}
std::pair<time_t, time_t> getWeekInterval(const std::tm& timeData)
{
int weekDay = timeData.tm_wday;
int daysSinceMonday = 0;
int daysToSunday = 0;
if (weekDay != 0)
{
daysSinceMonday = weekDay - 1;
daysToSunday = 7 - weekDay;
}
else
{
daysSinceMonday = 6;
daysToSunday = 0;
}
std::tm monday = timeData;
std::tm sunday = timeData;
monday.tm_hour = 0;
monday.tm_min = 0;
monday.tm_sec = 1;
monday.tm_mday -= daysSinceMonday;
sunday.tm_hour = 23;
sunday.tm_min = 59;
sunday.tm_sec = 59;
sunday.tm_sec = 59;
sunday.tm_mday += daysToSunday;
return std::pair<time_t, time_t>{std::mktime(&monday), std::mktime(&sunday)};
}