Skip to content
Open
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
18 changes: 18 additions & 0 deletions docs/discord.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,24 @@ Your character's name is never shown, in any setting.

The setting applies immediately and is stored in `config.ini`.

## Messages from Discord

> Requires a server with a Discord chat bridge, like OpenMU's (its guild and
> alliance chat can be bound to Discord channels).

A message written in a Discord channel that the server bridges into the game
appears in the chat of its scope - guild or alliance - with that chat's
colours. Its sender is shown as `[Discord] <name>`: with OpenMU's bridge the
name is the character the Discord user is linked to (OpenMU sends it as
`@<name>`, which no character can be called). A server that sends the Discord
name itself can show it with up to 32 characters.

- Discord users are not characters: right-clicking their line does not start a
whisper, and they are not added to your whisper list.
- **F2** switches the chat log between all messages and whispers. Once a
message from Discord has arrived, it also offers a view with only the
messages from Discord: all → whispers → Discord → all.

## For server operators

Each server uses its own Discord application, so the name your players see
Expand Down
12 changes: 12 additions & 0 deletions src/source/Network/Server/WSclient.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "stdafx.h"
#include "App/Control/ControlTaps.h"
#include "Core/Utilities/Log/MuLogger.h"
#include "UI/Chat/ExternalChat.h"
#include "UI/Chat/Chat.h"
#include "UI/Chat/ChatMessages.h"
#include "UI/Combat/SiegeUpdates.h"
Expand Down Expand Up @@ -1819,12 +1820,23 @@ void ReceiveChat(const BYTE* ReceiveBuffer)
{
for (int i = 0; i < messageSize - 2; i++)
Text[i] = Text[i + 2];
if (UI::Chat::External::IsBridgedSender(ID))
{
// Recorded for the control socket as a Discord line instead.
UI::Chat::External::ShowBridged(UI::Chat::External::Scope::Alliance, ID, Text);
return;
}
UI::Chat::PostChat(ID, Text, mu::ui::window::TYPE_UNION_MESSAGE);
}
else if (Text[0] == L'@')
{
for (int i = 0; i < messageSize - 1; i++)
Text[i] = Text[i + 1];
if (UI::Chat::External::IsBridgedSender(ID))
{
UI::Chat::External::ShowBridged(UI::Chat::External::Scope::Guild, ID, Text);
return;
}
UI::Chat::PostChat(ID, Text, mu::ui::window::TYPE_GUILD_MESSAGE);
}
else if (Text[0] == L'$')
Expand Down
74 changes: 74 additions & 0 deletions src/source/UI/Chat/ExternalChat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include "stdafx.h"
#include "UI/Chat/ExternalChat.h"

#include "App/Control/ControlTaps.h"
#include "Network/Server/WSclient.h"
#include "UI/Core/WindowSystem.h"

namespace
{
constexpr const wchar_t* DiscordBadge = L"[Discord] ";
constexpr const char* DiscordChatKind = "discord";
} // namespace

namespace UI::Chat::External
{
std::wstring SenderLabel(Source source, std::wstring_view displayName)
{
switch (source)
{
case Source::Discord:
default:
return DiscordBadge + std::wstring(displayName.substr(0, MaxDisplayNameLength));
}
}

mu::ui::window::MESSAGE_TYPE ScopeMessageType(Scope scope)
{
switch (scope)
{
case Scope::Guild:
return mu::ui::window::TYPE_GUILD_MESSAGE;
case Scope::Alliance:
return mu::ui::window::TYPE_UNION_MESSAGE;
case Scope::World:
default:
return mu::ui::window::TYPE_CHAT_MESSAGE;
}
}

std::wstring ShownText(std::wstring_view text)
{
return std::wstring(text.substr(0, MAX_CHAT_SIZE));
}

// Unlike an in-game message, no speech balloon (nobody stands there) and no
// entry in the whisper allow-list (nobody to whisper).
void Show(const Message& message)
{
const std::wstring sender = SenderLabel(message.source, message.displayName);
const std::wstring text = ShownText(message.text);
if (text.empty())
{
return;
}

g_pChatListBox->AddExternalText(sender, text, ScopeMessageType(message.scope));
App::Control::Events::RecordChatLine(sender.c_str(), text.c_str(), DiscordChatKind);
}

bool IsBridgedSender(std::wstring_view sender)
Comment thread
sven-n marked this conversation as resolved.
{
return sender.size() > 1 && sender.front() == BridgedSenderPrefix;
}

void ShowBridged(Scope scope, std::wstring_view sender, std::wstring_view text)
{
Message message;
message.source = Source::Discord;
message.scope = scope;
message.displayName = std::wstring(sender.substr(1));
message.text = std::wstring(text);
Show(message);
}
} // namespace UI::Chat::External
70 changes: 70 additions & 0 deletions src/source/UI/Chat/ExternalChat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Chat messages written outside the game - today in a Discord channel that
// the server bridges into guild, alliance or world chat (#700).
//
// They are shown as such: the sender is marked with where they wrote, keeps
// a name longer than a character's, and can't be whispered, because there
// is no character behind it.
#pragma once

#include "UI/HUD/ChatLogWindow.h"

#include <cstddef>
#include <string>
#include <string_view>

namespace UI::Chat::External
{
enum class Source
{
Discord,
};

// Which in-game chat the message belongs to; it takes that chat's colours.
enum class Scope
{
Guild,
Alliance,
World,
};

// Discord's display names are at most 32 characters long.
inline constexpr std::size_t MaxDisplayNameLength = 32;

struct Message
{
Source source = Source::Discord;
Scope scope = Scope::World;
std::wstring displayName;
std::wstring text;
};

// What the chat log shows as the sender: "[Discord] name", the name cut to
// MaxDisplayNameLength characters.
[[nodiscard]] std::wstring SenderLabel(Source source, std::wstring_view displayName);

// The chat log type whose colours the scope uses.
[[nodiscard]] mu::ui::window::MESSAGE_TYPE ScopeMessageType(Scope scope);

// The text as shown: cut to the length of an in-game chat message.
[[nodiscard]] std::wstring ShownText(std::wstring_view text);

// Shows the message in the chat log, in its scope and in the Discord view.
void Show(const Message& message);

// OpenMU's chat bridge (MUnique/OpenMU#1161) sends messages written in Discord
// through the ordinary guild and alliance chat, as the linked character with
// this prefix, which OpenMU doesn't allow in character names.
//
// This is a best-effort fallback for servers which don't send the dedicated
// ExternalChatMessage: the prefix is a convention of the server, not a
// guarantee the client can check - a server which allows it in character
// names would have such characters shown as Discord users. When a server
// sends ExternalChatMessage, that is the authoritative signal.
inline constexpr wchar_t BridgedSenderPrefix = L'@';

[[nodiscard]] bool IsBridgedSender(std::wstring_view sender);

// Shows a guild or alliance chat line of a bridged sender as a message from
// Discord, named after the character without the prefix.
void ShowBridged(Scope scope, std::wstring_view sender, std::wstring_view text);
} // namespace UI::Chat::External
3 changes: 3 additions & 0 deletions src/source/UI/Chat/MessageType.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ enum MESSAGE_TYPE
TYPE_UNION_MESSAGE,
TYPE_GENS_MESSAGE,
TYPE_GM_MESSAGE,
// A view, not a message type: the messages from Discord. Each of them
// keeps the type of its scope (guild, alliance, chat) for its colours.
TYPE_DISCORD_MESSAGE,

NUMBER_OF_TYPES,
TYPE_UNKNOWN = 0xFFFFFFFF
Expand Down
13 changes: 1 addition & 12 deletions src/source/UI/HUD/ChatInputBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ void mu::ui::window::CChatInputBox::Init()
m_bShowSystemMessages = true;
m_bShowChatLog = true;
m_bWhisperSend = true;

m_bShowMessageElseNormal = false;
}

bool mu::ui::window::CChatInputBox::Create(
Expand Down Expand Up @@ -197,16 +195,7 @@ bool mu::ui::window::CChatInputBox::UpdateKeyEvent()
{
if (mu::ui::window::IsPress(VK_F2))
{
m_bShowMessageElseNormal = !m_bShowMessageElseNormal;

if (m_bShowMessageElseNormal)
{
m_pNewUIChatLogWnd->ChangeMessage(mu::ui::window::TYPE_WHISPER_MESSAGE);
}
else
{
m_pNewUIChatLogWnd->ChangeMessage(mu::ui::window::TYPE_ALL_MESSAGE);
}
m_pNewUIChatLogWnd->ChangeMessage(m_pNewUIChatLogWnd->GetNextView());

PlayBuffer(SOUND_CLICK01);
return false;
Expand Down
1 change: 0 additions & 1 deletion src/source/UI/HUD/ChatInputBox.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ namespace mu::ui::window
bool m_bShowSystemMessages;
bool m_bShowChatLog;
bool m_bWhisperSend;
bool m_bShowMessageElseNormal;

void Init();

Expand Down
Loading