Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
149 lines (120 loc) · 4.73 KB
/
Copy pathmain.cpp
File metadata and controls
149 lines (120 loc) · 4.73 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
#include <cctype>
#include <cstdlib>
#include <dpp/appcommand.h>
#include <dpp/cluster.h>
#include <dpp/commandhandler.h>
#include <dpp/dispatcher.h>
#include <dpp/dpp.h>
#include <dpp/intents.h>
#include <dpp/message.h>
#include <dpp/once.h>
#include <dpp/snowflake.h>
#include <string>
#include <random>
using namespace std;
using namespace dpp;
const std::string BOT_TOKEN = getenv("DISCORD_BOT_TOKEN") ? getenv("DISCORD_BOT_TOKEN") : "";
void loadEnvFile(const string& filename = ".env") {
ifstream file(filename);
if (!file.is_open()) {
cerr << "Warning: .env File not found!" << endl;
return;
}
cout << ".env File found, loading Variables..." << endl;
string line;
while (getline(file, line)) {
if (line.empty() || line[0] == '#') continue;
size_t pos = line.find('=');
if (pos != string::npos) {
string key = line.substr(0, pos);
string value = line.substr(pos + 1);
key.erase(0, key.find_first_not_of(" \t"));
key.erase(key.find_last_not_of(" \t") + 1);
value.erase(0, value.find_first_not_of(" \t"));
value.erase(value.find_last_not_of(" \t") + 1);
if (!value.empty() && value.front() == '"' && value.back() == '"') {
value = value.substr(1, value.length() - 2);
}
#ifdef _WIN32
_putenv_s(key.c_str(), value.c_str());
#else
setenv(key.c_str(), value.c_str(), 1);
#endif
cout << "Environmentvariables loaded: " << key << " (Length: " << value.length() << ")" << endl;
}
}
file.close();
}
string getComputerChoice() {
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<> distr(1,3);
int randomNum = distr(gen);
switch (randomNum) {
case 1: return "Rock";
case 2: return "Paper";
case 3: return"Scissors";
default: return "Rock";
}
}
string determineWinner(const string& playerChoice, const string& computerChoice){
if (playerChoice == computerChoice) {
return "It's a tie 🤝";
}
map<string, string> wins = {
{"Rock", "Scissors"},
{"Paper", "Rock"},
{"Scissors", "Paper"}
};
if(wins[playerChoice] == computerChoice) {
return "You win! 🫵";
} else {
return "Dave wins! 🧙🏼♂️";
}
}
int main() {
loadEnvFile();
const char* token_env = getenv("DISCORD_BOT_TOKEN");
if (token_env == nullptr || strlen(token_env) == 0) {
cerr << "\nERROR: DISCORD_BOT_TOKEN not found!" << endl;
cerr << "Make sure .env File contains the following:" << endl;
cerr << "DISCORD_BOT_TOKEN=your_token_here" << endl;
return 1;
}
string BOT_TOKEN = token_env;
// Debug
cout << "\nBot Token loaded!" << endl;
cout << "First 10 Chars: " << BOT_TOKEN.substr(0, min(10, (int)BOT_TOKEN.length())) << "..." << endl;
cout << "Token Length: " << BOT_TOKEN.length() << " Chars" << endl;
cout << "\nStarting Bot...\n" << endl;
cluster bot(BOT_TOKEN);
bot.on_log(utility::cout_logger());
bot.on_slashcommand([&bot](const slashcommand_t &event){
string cmd = event.command.get_command_name();
if (cmd == "challenge") {
event.reply(message("Challenge Accepted 🧙🏼♂️\n Use /rock ✊, /paper ✋, or /scissors ✌️ to play!"));
} else if (cmd == "rock" || cmd == "paper" || cmd == "scissors") {
string playerChoice = cmd;
playerChoice[0] = toupper(playerChoice[0]);
string computerChoice = getComputerChoice();
string result = determineWinner(playerChoice, computerChoice);
event.reply(message(
"You picked: " + playerChoice + "\n" +
"Dave picked: " + computerChoice + "\n" +
result
));
} else if (cmd == "cheat") {
event.reply(message("You are a dirty little cheater... \nShame on you 🖕😭"));
}
});
bot.on_ready([&bot](const ready_t &event) {
if (run_once<struct register_bot_commands>()) {
bot.global_command_create( slashcommand("challenge", "Challenge Dave the Wizard to a Rock-Paper-Scissors Duell", bot.me.id));
bot.global_command_create(slashcommand("rock", "Show Rock", bot.me.id));
bot.global_command_create(slashcommand("paper", "Show Paper", bot.me.id));
bot.global_command_create(slashcommand("scissors", "Show Scissors", bot.me.id));
bot.global_command_create(slashcommand("cheat", "Try to cheat.", bot.me.id));
}
});
bot.start(st_wait);
}