Skip to content

Commit

Permalink
Added check for invalid serverID in GetRoleName function
Browse files Browse the repository at this point in the history
Added EditChannel function
  • Loading branch information
Luckshya committed May 15, 2020
1 parent afcd072 commit c2df682
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 15 deletions.
105 changes: 90 additions & 15 deletions src/CSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ SQInteger CSession::GetRoleName(HSQUIRRELVM vm)
session = Sqrat::Var< CSession * >(vm, 1).value;
}
catch (const Sqrat::Exception& e) {
return sq_throwerror(vm, e.Message().c_str());
return sq_throwerror(vm, e.what());
}

if (!session) {
Expand Down Expand Up @@ -380,24 +380,30 @@ SQInteger CSession::GetRoleName(HSQUIRRELVM vm)
}

try {
std::list<SleepyDiscord::Role> roles = session->client->s_servers.at(std::string(serverID)).roles;
std::string s_serverID = std::string(serverID);

bool found = false;
CCStr role_name = nullptr;
auto rolesIndex = session->client->s_servers.find(std::string(serverID));

for (const auto & role : roles) {
if (role.ID.string() == std::string(roleID)) {
found = true;
role_name = role.name.c_str();
sq_pushstring(vm, role_name, -1);
break;
}
}
if(rolesIndex == session->client->s_servers.end()) {
return sq_throwerror(vm, "Invalid server ID");
}

std::list<SleepyDiscord::Role>& roles = (rolesIndex->second).roles;
bool found = false;
CCStr role_name = nullptr;

if (!found) {
return sq_throwerror(vm, "Invalid role ID");
}
for (const auto & role : roles) {
if (role.ID.string() == std::string(roleID)) {
found = true;
role_name = role.name.c_str();
sq_pushstring(vm, role_name, -1);
break;
}
}

if (!found) {
return sq_throwerror(vm, "Invalid role ID");
}
}
catch (...) {
OutputErr("An Error has occured at [CSession] function => [RoleName]");
Expand All @@ -406,6 +412,74 @@ SQInteger CSession::GetRoleName(HSQUIRRELVM vm)
return 1;
}

// ------------------------------------------------------------------------------------------------
SQInteger CSession::EditChannel(HSQUIRRELVM vm) {
const int top = sq_gettop(vm);

if (top <= 1)
{
return sq_throwerror(vm, "Missing the channel ID value");
}

else if (top <= 2)
{
return sq_throwerror(vm, "Missing the channel name value");
}

else if (top <= 3)
{
return sq_throwerror(vm, "Missing the channel topic value");
}

CSession * session = nullptr;

try {
session = Sqrat::Var< CSession * >(vm, 1).value;
}
catch (const Sqrat::Exception& e) {
return sq_throwerror(vm, e.what());
}

if (!session) {
return sq_throwerror(vm, "Invalid session instance");
}

else if (!session->client) {
return sq_throwerror(vm, "Invalid Discord client");
}

else if (!session->isConnected) {
return sq_throwerror(vm, "Session is not connected");
}

CCStr channelID = nullptr;
if (SQ_FAILED(sq_getstring(vm, 2, &channelID)))
{
return sq_throwerror(vm, "Failed to retrieve argument 1 as string");
}

CCStr name = nullptr;
if (SQ_FAILED(sq_getstring(vm, 3, &name)))
{
return sq_throwerror(vm, "Failed to retrieve argument 2 as string");
}

CCStr topic = nullptr;
if (SQ_FAILED(sq_getstring(vm, 4, &topic)))
{
return sq_throwerror(vm, "Failed to retrieve argument 3 as string");
}

try {
auto response = session->client->editChannel(std::string(channelID), std::string(name), std::string(topic), SleepyDiscord::Async);
}
catch (...) {
OutputErr("An Error has occured at [CSession] function => [EditChannel]");
}

return 0;
}

// ------------------------------------------------------------------------------------------------
SQInteger CSession::SetActivity(HSQUIRRELVM vm)
{
Expand Down Expand Up @@ -552,6 +626,7 @@ void Register_CSession(Sqrat::Table discordcn)
.SquirrelFunc("Message", &CSession::Message)
.SquirrelFunc("MessageEmbed", &CSession::MessageEmbed)
.SquirrelFunc("GetRoleName", &CSession::GetRoleName)
.SquirrelFunc("EditChannel", &CSession::EditChannel)
.SquirrelFunc("SetActivity", &CSession::SetActivity)
);
}
Expand Down
1 change: 1 addition & 0 deletions src/CSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class CSession
static SQInteger Message(HSQUIRRELVM vm);
static SQInteger MessageEmbed(HSQUIRRELVM vm);
static SQInteger GetRoleName(HSQUIRRELVM vm);
static SQInteger EditChannel(HSQUIRRELVM vm);
static SQInteger SetActivity(HSQUIRRELVM vm);
};

Expand Down

0 comments on commit c2df682

Please sign in to comment.