Skip to content

Commit

Permalink
Fix: crash when setting prefix before bot connects
Browse files Browse the repository at this point in the history
  • Loading branch information
rasikhq committed Oct 20, 2021
1 parent 9051358 commit 59157dd
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 29 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.c linguist-detectable=false
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# MTA Discord Module
Discord bot module for MTA. Does not require NodeJS setup or sockets module. You just need to setup a bot on discord developer site and get the token.
Discord bot module for MTA. This module lets you setup a mta discord bot that does not require NodeJS setup or sockets module. You just need to setup a bot on discord developer site and get the token.

**Note:**
- This module is still in development and may be incomplete, unstable, and/or broken. It can crash your MTA server, use at your own risk.
- Only x64 (64-bit) is supported. I'll add support for 32-bit if I get the time.

# Installation
- Simply grab the DLL or SO file from Release and follow installation steps at the bottom of this doc
- Simply grab the DLL or SO file from Release and follow installation steps from the "Installing the module" section at the bottom of this doc

# Build Guidelines
## Requirements
Expand Down
46 changes: 25 additions & 21 deletions bot/module/Bot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Bot::Bot(const std::string& token, lua_State* vm, void* userdata)
: SleepyDiscord::DiscordClient(token)
{
m_VM = vm;
m_State = new sol::state_view(m_VM);
m_Userdata = userdata;
m_State = new sol::state_view(m_VM);
}

Bot::~Bot()
Expand All @@ -17,17 +17,19 @@ Bot::~Bot()

void Bot::onReady(SleepyDiscord::Ready readyResult)
{
nlohmann::json o;
o["id"] = this->getID().string();
o["user"] =
nlohmann::json responseObject;
responseObject["id"] = this->getID().string();
responseObject["user"] =
{
{"id", readyResult.user.ID.string()},
{"username", readyResult.user.username}
};

try {
g_Pulses.push_back(new BotPulse(m_State, "onDiscordReady", o.dump()));
//TRIGGER_MTA_EVENT(m_State, "onDiscordReady", o.dump());
g_Pulses.push_back(new BotPulse(m_State, "onDiscordReady", responseObject.dump()));
}
catch (const std::bad_alloc& e) {
std::cout << "Failed to allocate heap (" << e.what() << ")\n";
}
catch (const nlohmann::json::exception& e) {
std::cerr << e.what() << '\n';
Expand All @@ -41,23 +43,23 @@ void Bot::onResumed()

void Bot::onServer(SleepyDiscord::Server server)
{
nlohmann::json o;
nlohmann::json responseObject;
std::stringstream ss;
o["server"] =
responseObject["server"] =
{
{"id", server.ID.string()},
{"name", server.name}
};

o["roles"] = {};
responseObject["roles"] = {};

auto& rolesList = server.roles;
for (auto& role : rolesList)
{
ss.str(std::string());
ss << "0x" << std::hex << std::setfill('0') << std::setw(2) << role.color;

o["roles"].push_back
responseObject["roles"].push_back
({
{"id", role.ID.string()},
{"name", role.name},
Expand All @@ -66,8 +68,10 @@ void Bot::onServer(SleepyDiscord::Server server)
}

try {
g_Pulses.push_back(new BotPulse(m_State, "onDiscordServer", o.dump()));
//TRIGGER_MTA_EVENT(m_State, "onDiscordServer", o.dump());
g_Pulses.push_back(new BotPulse(m_State, "onDiscordServer", responseObject.dump()));
}
catch (const std::bad_alloc& e) {
std::cout << "Failed to allocate heap (" << e.what() << ")\n";
}
catch (const nlohmann::json::exception& e) {
std::cerr << e.what() << '\n';
Expand All @@ -80,34 +84,34 @@ void Bot::onMessage(SleepyDiscord::Message message)

if (message.startsWith(m_Prefix))
{
nlohmann::json o;
nlohmann::json responseObject;

const SleepyDiscord::ServerMember serverMember = getMember(message.serverID, message.author.ID);
const SleepyDiscord::Channel channel = getChannel(message.channelID);
auto& roles = serverMember.roles;

o["user"] = {};
responseObject["user"] = {};
nlohmann::json rolesObj = {};

for (auto& role : roles)
rolesObj[role.string()] = true;

o["message"] =
responseObject["message"] =
{
{"prefix", m_Prefix},
{"raw_content", message.content},
{"content", message.content.substr(m_Prefix.length())}
};

o["message"]["author"] =
responseObject["message"]["author"] =
{
{"id", message.author.ID.string()},
{"username", message.author.username},
{"nickname", serverMember.nick},
{"roles", rolesObj}
};

o["message"]["channel"] =
responseObject["message"]["channel"] =
{
{"id", channel.ID.string()},
{"name", channel.name},
Expand All @@ -116,17 +120,17 @@ void Bot::onMessage(SleepyDiscord::Message message)
};

try {
g_Pulses.push_back(new BotPulse(m_State, "onDiscordMessage", o.dump()));
//TRIGGER_MTA_EVENT(m_State, "onDiscordMessage", o.dump());
g_Pulses.push_back(new BotPulse(m_State, "onDiscordMessage", responseObject.dump()));
}
catch (const std::bad_alloc& e) {
std::cout << "Failed to allocate heap (" << e.what() << ")\n";
}
catch (const nlohmann::json::exception& e) {
std::cerr << e.what() << '\n';
}
}
}



void Bot::setPrefix(const std::string& newPrefix)
{
m_Prefix = newPrefix;
Expand Down
35 changes: 30 additions & 5 deletions bot/module/BotSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ std::mutex g_PulseMutex;

usertype["setPrefix"] = &BotSession::setPrefix;
usertype["sendMessage"] = &BotSession::sendMessage;
usertype["editMessage"] = &BotSession::editMessage;
usertype["sendEmbed"] = &BotSession::sendEmbed;
usertype["editMessage"] = &BotSession::editMessage;
usertype["setActivity"] = &BotSession::setActivity;
}

Expand Down Expand Up @@ -68,9 +69,13 @@ void BotSession::login(const std::string& token, sol::this_state s)

m_VM = s.lua_state();
m_Userdata = lua_newuserdata(m_VM, 128);
m_Bot = new Bot(token, m_VM, m_Userdata);

m_Thread = new std::thread(&BotSession::threadHandle, this);
try {
m_Bot = new Bot(token, m_VM, m_Userdata);
m_Thread = new std::thread(&BotSession::threadHandle, this);
}
catch (const std::bad_alloc& e) {
std::cout << "Failed to allocate heap (" << e.what() << ")\n";
}
}

void BotSession::disconnect()
Expand All @@ -92,7 +97,7 @@ void BotSession::disconnect()

void BotSession::setPrefix(const std::string& newPrefix)
{
if (!isReady())
if (!isConnected())
return;

m_Prefix = newPrefix; // This is for the operator() overload
Expand All @@ -114,10 +119,29 @@ void BotSession::sendMessage(const std::string& channelID, const std::string& me
}
}

void BotSession::sendEmbed(const std::string& channelID, const std::string& message)
{
if (!isConnected())
return;

try
{
//nlohmann::json embedMessage(message);
SleepyDiscord::Embed embed(message);
if (!embed.empty())
m_Bot->sendMessage(channelID, "", embed);
}
catch (...)
{
pModuleManager->ErrorPrintf("BotSession::sendMessage Failed!");
}
}

void BotSession::editMessage(const std::string& channelID, const std::string& messageID, const std::string& newMessage)
{
if (!isConnected())
return;

try
{
m_Bot->editMessage(channelID, messageID, newMessage);
Expand All @@ -132,6 +156,7 @@ void BotSession::setActivity(const std::string& activity)
{
if (!isConnected())
return;

try
{
m_Bot->updateStatus(activity);
Expand Down
1 change: 1 addition & 0 deletions bot/module/BotSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class BotSession

void setPrefix(const std::string& newPrefix);
void sendMessage(const std::string& channelID, const std::string& message);
void sendEmbed(const std::string& channelID, const std::string& message);
void editMessage(const std::string& channelID, const std::string& messageID, const std::string& newMessage);
void setActivity(const std::string& activity);

Expand Down
2 changes: 1 addition & 1 deletion bot/module/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ MTAEXPORT bool InitModule(ILuaModuleManager10* pManager, char* szModuleName, cha
const auto author = "DizzasTeR";
std::memcpy(szModuleName, module_name, MAX_INFO_LENGTH);
std::memcpy(szAuthor, author, MAX_INFO_LENGTH);
*fVersion = 1.0f;
*fVersion = 1.1f;

return true;
}
Expand Down

0 comments on commit 59157dd

Please sign in to comment.