Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Discord integration for servers #3664

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 3 additions & 1 deletion Server/mods/deathmatch/logic/ASE.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
#ifndef INVALID_SOCKET
#define INVALID_SOCKET -1
#endif
typedef int SOCKET;
#ifndef SOCKET
#define SOCKET int
#endif
#endif

#include "CConnectHistory.h"
Expand Down
113 changes: 113 additions & 0 deletions Server/mods/deathmatch/logic/CDiscord.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*****************************************************************************
*
* PROJECT: Multi Theft Auto v1.0
* LICENSE: See LICENSE in the top level directory
* FILE: mods/deathmatch/logic/CDiscord.cpp
* PURPOSE: Discord bot manager
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
*
*****************************************************************************/

#include "StdInc.h"
#include "CDiscord.h"

std::unordered_map<dpp::snowflake, std::unique_ptr<CDiscordGuild>> CDiscord::ms_guilds = {};

IDiscord::IDiscord() noexcept : dpp::cluster("")
{
}

CDiscord::CDiscord() noexcept
{
auto handle = on_ready.attach(
[this](const dpp::ready_t& event)
{
if (m_hasStarted)
return;

m_hasStarted = true;
});
m_eventHandlers[DiscordEvent::on_ready].push_back(handle);
}

CDiscord::~CDiscord() noexcept
{
this->stop();
}

bool CDiscord::HasStarted() const noexcept
{
return m_hasStarted;
}

void CDiscord::login(const std::string_view& string) noexcept
{
this->token = string;
}

void CDiscord::start()
{
dpp::cluster::start(dpp::st_return);
}

void CDiscord::stop()
{
if (!m_hasStarted)
return;
m_hasStarted = false;
this->shutdown();
}

CDiscordGuild::CDiscordGuild()
{
m_scriptID = CIdArray::PopUniqueId(this, EIdClass::DISCORD_GUILD);
m_scriptID = INVALID_ARRAY_ID;
}

CDiscordGuild::CDiscordGuild(dpp::guild guild) : CDiscordGuild()
{
*this = guild;
}

CDiscordGuild::CDiscordGuild(const dpp::guild* guild) : CDiscordGuild(guild ? *guild : dpp::guild())
{
}

CDiscordGuild::~CDiscordGuild()
{
CIdArray::PushUniqueId(this, EIdClass::DISCORD_GUILD, m_scriptID);
}

std::uint32_t CDiscordGuild::GetScriptID() const noexcept
{
return m_scriptID;
}

CDiscordGuild* CDiscordGuild::GetFromSciptID(std::uint32_t id)
{
return static_cast<CDiscordGuild*>(CIdArray::FindEntry(id, EIdClass::DISCORD_GUILD));
}

IDiscordGuild* IDiscordGuild::GetFromSciptID(std::uint32_t id)
{
return CDiscordGuild::GetFromSciptID(id);
}

CDiscordGuild* CDiscord::GetGuild(dpp::snowflake id) noexcept
{
if (SharedUtil::MapContains(ms_guilds, id))
return ms_guilds[id].get();

auto guild = dpp::find_guild(id);
if (!guild)
return nullptr;

ms_guilds[id] = std::make_unique<CDiscordGuild>(*guild);
return ms_guilds[id].get();
}

IDiscordGuild* IDiscord::GetGuild(dpp::snowflake id) noexcept
{
return dynamic_cast<IDiscordGuild*>(CDiscord::GetGuild(id));
}
173 changes: 173 additions & 0 deletions Server/mods/deathmatch/logic/CDiscord.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/*****************************************************************************
*
* PROJECT: Multi Theft Auto v1.0
* LICENSE: See LICENSE in the top level directory
* FILE: mods/deathmatch/logic/CDiscord.h
* PURPOSE: Discord bot manager
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
*
*****************************************************************************/

#pragma once

#ifdef _WIN32
#include <dpp/win32_safe_warnings.h>
#endif
#include <dpp/dpp.h>
#include <CIdArray.h>

// Interfaces for modules

class IDiscordGuild : public dpp::guild
{
public:
virtual std::uint32_t GetScriptID() const noexcept = 0;
static IDiscordGuild* GetFromSciptID(std::uint32_t id);

protected:
};

class CDiscordGuild : public IDiscordGuild
{
public:
CDiscordGuild();
CDiscordGuild(dpp::guild guild);
CDiscordGuild(const dpp::guild* guild);
~CDiscordGuild() override;

static CDiscordGuild* GetFromSciptID(std::uint32_t id);
std::uint32_t GetScriptID() const noexcept override;

protected:
std::uint32_t m_scriptID{INVALID_ARRAY_ID};
};

class IDiscord : public dpp::cluster
{
public:
enum class DiscordEvent
{
on_voice_state_update,
on_voice_client_disconnect,
on_voice_client_speaking,
on_log,
on_guild_join_request_delete,
on_interaction_create,
on_slashcommand,
on_button_click,
on_autocomplete,
on_select_click,
on_message_context_menu,
on_user_context_menu,
on_form_submit,
on_guild_delete,
on_channel_delete,
on_channel_update,
on_ready,
on_message_delete,
on_guild_member_remove,
on_resumed,
on_guild_role_create,
on_typing_start,
on_message_reaction_add,
on_guild_members_chunk,
on_message_reaction_remove,
on_guild_create,
on_channel_create,
on_message_reaction_remove_emoji,
on_message_delete_bulk,
on_guild_role_update,
on_guild_role_delete,
on_channel_pins_update,
on_message_reaction_remove_all,
on_voice_server_update,
on_guild_emojis_update,
on_guild_stickers_update,
on_presence_update,
on_webhooks_update,
on_automod_rule_create,
on_automod_rule_update,
on_automod_rule_delete,
on_automod_rule_execute,
on_guild_member_add,
on_invite_delete,
on_guild_update,
on_guild_integrations_update,
on_guild_member_update,
on_invite_create,
on_message_update,
on_user_update,
on_message_create,
on_message_poll_vote_add,
on_message_poll_vote_remove,
on_guild_audit_log_entry_create,
on_guild_ban_add,
on_guild_ban_remove,
on_integration_create,
on_integration_update,
on_integration_delete,
on_thread_create,
on_thread_update,
on_thread_delete,
on_thread_list_sync,
on_thread_member_update,
on_thread_members_update,
on_guild_scheduled_event_create,
on_guild_scheduled_event_update,
on_guild_scheduled_event_delete,
on_guild_scheduled_event_user_add,
on_guild_scheduled_event_user_remove,
on_voice_buffer_send,
on_voice_user_talking,
on_voice_ready,
on_voice_receive,
on_voice_receive_combined,
on_voice_track_marker,
on_stage_instance_create,
on_stage_instance_update,
on_stage_instance_delete,
on_entitlement_create,
on_entitlement_update,
on_entitlement_delete,
};

public:
IDiscord() noexcept;

virtual bool HasStarted() const noexcept = 0;

virtual void login(const std::string_view& token) noexcept = 0;
virtual void start() = 0;
virtual void start(bool) = delete;
virtual void stop() = 0;

static IDiscordGuild* GetGuild(dpp::snowflake id) noexcept;

protected:
};

// Usable classes


class CDiscord : public IDiscord
{
public:
CDiscord() noexcept;
~CDiscord() noexcept override;

bool HasStarted() const noexcept override;

void login(const std::string_view& token) noexcept override;
void start() override;
void stop() override;

static CDiscordGuild* GetGuild(dpp::snowflake id) noexcept;

protected:
std::atomic<bool> m_hasStarted;

std::unordered_map<DiscordEvent, std::vector<dpp::event_handle>> m_eventHandlers;

static std::unordered_map<dpp::snowflake, std::unique_ptr<CDiscordGuild>> ms_guilds;
};
2 changes: 1 addition & 1 deletion Server/mods/deathmatch/logic/CGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ void sighandler(int sig)
}
#endif

CGame::CGame() : m_FloodProtect(4, 30000, 30000) // Max of 4 connections per 30 seconds, then 30 second ignore
CGame::CGame() : m_FloodProtect(4, 30000, 30000) // Max of 4 connections per 30 seconds, then 30 second ignore
{
// Set our global pointer
g_pGame = this;
Expand Down
1 change: 1 addition & 0 deletions Server/mods/deathmatch/logic/CGame.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class CGame;

#include "CLightsyncManager.h"
#include "CBanManager.h"
#include <CDiscord.h>

// Forward declarations
class ASE;
Expand Down
4 changes: 4 additions & 0 deletions Server/mods/deathmatch/logic/CResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,8 @@ bool CResource::Start(std::list<CResource*>* pDependents, bool bManualStart, con
// Sort by priority, for start grouping on the client
m_StartedResources.sort([](CResource* a, CResource* b) { return a->m_iDownloadPriorityGroup > b->m_iDownloadPriorityGroup; });

m_discord = std::make_unique<CDiscord>();

return true;
}

Expand Down Expand Up @@ -1174,6 +1176,8 @@ bool CResource::Stop(bool bManualStop)

OnResourceStateChange("loaded");
m_eState = EResourceState::Loaded;

m_discord.reset();
return true;
}

Expand Down
5 changes: 5 additions & 0 deletions Server/mods/deathmatch/logic/CResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ class CResource : public EHS
*/
CResourceFile* GetResourceFile(const SString& relativePath) const;


const CDiscord* GetDiscordManager() const noexcept { return m_discord.get(); }
CDiscord* GetDiscordManager() noexcept { return m_discord.get(); }
public:
static std::list<CResource*> m_StartedResources;

Expand Down Expand Up @@ -365,6 +368,8 @@ class CResource : public EHS
bool IsHttpAccessAllowed(CAccount* pAccount);

private:
std::unique_ptr<CDiscord> m_discord;

EResourceState m_eState = EResourceState::None;
bool m_bClientSync = false;

Expand Down
8 changes: 8 additions & 0 deletions Server/mods/deathmatch/logic/lua/CLuaArguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,14 @@ CLuaArgument* CLuaArguments::PushDbQuery(CDbJobData* pJobData)
return pArgument;
}

CLuaArgument* CLuaArguments::PushDiscordGuild(dpp::guild* guild)
{
CLuaArgument* arg = new CLuaArgument;
arg->ReadScriptID(guild->id);
m_Arguments.push_back(arg);
return arg;
}

void CLuaArguments::DeleteArguments()
{
// Delete each item
Expand Down
2 changes: 2 additions & 0 deletions Server/mods/deathmatch/logic/lua/CLuaArguments.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ extern "C"
#include "../common/CBitStream.h"
#include "json.h"
#include "CLuaFunctionRef.h"
#include <CDiscord.h>

inline void LUA_CHECKSTACK(lua_State* L, int size)
{
Expand Down Expand Up @@ -80,6 +81,7 @@ class CLuaArguments
CLuaArgument* PushTextItem(CTextItem* pTextItem);
CLuaArgument* PushTimer(CLuaTimer* pLuaTimer);
CLuaArgument* PushDbQuery(CDbJobData* pJobData);
CLuaArgument* PushDiscordGuild(dpp::guild* guild);

CLuaArgument* PushArgument(const CLuaArgument& argument);
CLuaArgument* PushTable(CLuaArguments* table);
Expand Down
Loading
Loading