Skip to content
Merged
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/engine/client/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3029,7 +3029,7 @@ void CClient::Update()
else
GameClient()->OnUpdate();

Discord()->Update();
Discord()->Update(g_Config.m_TcDiscordRPC);
Steam()->Update();
if(Steam()->GetConnectAddress())
{
Expand Down
55 changes: 44 additions & 11 deletions src/engine/client/discord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,27 @@ class CDiscord : public IDiscord
IDiscordActivityEvents m_ActivityEvents;
IDiscordActivityManager *m_pActivityManager;

FDiscordCreate m_pfnDiscordCreate;
bool m_Enabled;

public:
bool Init(FDiscordCreate pfnDiscordCreate)
{
m_pCore = 0;
m_pfnDiscordCreate = pfnDiscordCreate;
m_Enabled = false;
return InitDiscord();
}
bool InitDiscord()
{
if(m_pCore)
{
m_pCore->destroy(m_pCore);
m_pCore = 0;
m_pActivityManager = 0;
}
if(!m_Enabled)
return false;

mem_zero(&m_ActivityEvents, sizeof(m_ActivityEvents));

m_ActivityEvents.on_activity_join = &CDiscord::OnActivityJoin;
Expand All @@ -54,7 +71,8 @@ class CDiscord : public IDiscord
Params.event_data = this;
Params.activity_events = &m_ActivityEvents;

int Error = pfnDiscordCreate(DISCORD_VERSION, &Params, &m_pCore);
int Error = m_pfnDiscordCreate(DISCORD_VERSION, &Params, &m_pCore);

if(Error != DiscordResult_Ok)
{
dbg_msg("discord", "error initializing discord instance, error=%d", Error);
Expand All @@ -72,18 +90,27 @@ class CDiscord : public IDiscord
return false;
}

void Update() override
void Update(bool Enabled) override
{
// update every 5 seconds, rate limit is 5 updates per 20 seconds
if(m_UpdateActivity && time_get() > m_LastActivityUpdate + time_freq() * 5)
bool NeedsUpdate = m_Enabled != Enabled;
m_Enabled = Enabled;

if(NeedsUpdate)
InitDiscord();

if(m_pCore && m_Enabled)
{
m_UpdateActivity = false;
m_LastActivityUpdate = time_get();
// update every 5 seconds, rate limit is 5 updates per 20 seconds
if(m_UpdateActivity && time_get() > m_LastActivityUpdate + time_freq() * 5)
{
m_UpdateActivity = false;
m_LastActivityUpdate = time_get();

m_pActivityManager->update_activity(m_pActivityManager, &m_Activity, 0, 0);
}
m_pActivityManager->update_activity(m_pActivityManager, &m_Activity, 0, 0);
}

m_pCore->run_callbacks(m_pCore);
m_pCore->run_callbacks(m_pCore);
}
}

void ClearGameInfo() override
Expand Down Expand Up @@ -186,6 +213,12 @@ class CDiscord : public IDiscord
IClient *m_pClient = pSelf->Kernel()->RequestInterface<IClient>();
m_pClient->Connect(pSecret);
}

~CDiscord()
{
if(m_pCore)
m_pCore->destroy(m_pCore);
}
};

static IDiscord *CreateDiscordImpl()
Expand All @@ -212,7 +245,7 @@ static IDiscord *CreateDiscordImpl()

class CDiscordStub : public IDiscord
{
void Update() override {}
void Update(bool Enabled) override {}
void ClearGameInfo() override {}
void SetGameInfo(const CServerInfo &ServerInfo, const char *pMapName, bool Registered) override {}
void UpdateServerInfo(const CServerInfo &ServerInfo, const char *pMapName) override {}
Expand Down
2 changes: 1 addition & 1 deletion src/engine/discord.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class IDiscord : public IInterface
{
MACRO_INTERFACE("discord")
public:
virtual void Update() = 0;
virtual void Update(bool Enabled) = 0;

virtual void ClearGameInfo() = 0;
virtual void SetGameInfo(const CServerInfo &ServerInfo, const char *pMapName, bool Registered) = 0;
Expand Down
6 changes: 3 additions & 3 deletions src/engine/shared/config_variables_tclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,6 @@ MACRO_CONFIG_INT(TcAutoVoteWhenFar, tc_auto_vote_when_far, 0, 0, 1, CFGFLAG_CLIE
MACRO_CONFIG_STR(TcAutoVoteWhenFarMessage, tc_auto_vote_when_far_message, 128, "", CFGFLAG_CLIENT | CFGFLAG_SAVE, "Message to send when auto far vote happens, leave empty to disable")
MACRO_CONFIG_INT(TcAutoVoteWhenFarTime, tc_auto_vote_when_far_time, 5, 0, 20, CFGFLAG_CLIENT | CFGFLAG_SAVE, "How long until auto vote far happens")

// Integration
// MACRO_CONFIG_INT(TcDiscordRPC, tc_discord_rpc, 1, 0, 1, CFGFLAG_CLIENT | CFGFLAG_SAVE, "Toggle discord RPC (requires restart)") // broken

// Font
MACRO_CONFIG_STR(TcCustomFont, tc_custom_font, 255, "DejaVu Sans", CFGFLAG_CLIENT | CFGFLAG_SAVE, "Custom font face")

Expand Down Expand Up @@ -264,3 +261,6 @@ MACRO_CONFIG_INT(TcExecuteOnJoinDelay, tc_execute_on_join_delay, 2, 7, 50000, CF

// Custom Communities
MACRO_CONFIG_STR(TcCustomCommunitiesUrl, tc_custom_communities_url, 256, "https://raw.githubusercontent.com/SollyBunny/ddnet-custom-communities/refs/heads/main/custom-communities-ddnet-info.json", CFGFLAG_CLIENT | CFGFLAG_SAVE, "URL to fetch custom communities from (must be https), empty to disable")

// Discord RPC
MACRO_CONFIG_INT(TcDiscordRPC, tc_discord_rpc, 1, 0, 1, CFGFLAG_CLIENT | CFGFLAG_SAVE, "Toggle discord RPC (requires restart)") // broken
Loading