Add Discord Rich Presence ChatBot integration

- Add DiscordRichPresence NuGet package (v1.143.0)
- Create DiscordRpc.cs ChatBot with configurable presence display
- Wire config in Settings.cs ChatBotConfig class
- Register bot in McClient.cs RegisterBots()
- Add translation strings to Translations.resx and Designer
- Add config comments to ConfigComments.resx and Designer
- Support placeholders: {server_host}, {server_port}, {username},
  {health}, {max_health}, {food}, {dimension}, {gamemode},
  {x}, {y}, {z}, {player_count}, {protocol}

Co-authored-by: milutinke <441903+milutinke@users.noreply.github.com>
Agent-Logs-Url: https://github.com/MCCTeam/Minecraft-Console-Client/sessions/43388021-4264-47e6-8403-7a1bca66d86f
This commit is contained in:
copilot-swe-agent[bot] 2026-03-23 00:03:55 +00:00
parent 0a463f2380
commit a2d032b549
8 changed files with 546 additions and 0 deletions

View file

@ -0,0 +1,309 @@
using System;
using System.Diagnostics;
using DiscordRPC;
using DiscordRPC.Logging;
using MinecraftClient.Mapping;
using MinecraftClient.Scripting;
using Tomlet.Attributes;
namespace MinecraftClient.ChatBots
{
/// <summary>
/// Displays a Discord Rich Presence status showing the player's
/// current Minecraft session information (server, health, dimension, etc.).
/// Requires a Discord Application ID from https://discord.com/developers/applications
/// </summary>
public class DiscordRpc : ChatBot
{
public static Configs Config = new();
[TomlDoNotInlineObject]
public class Configs
{
[NonSerialized]
private const string BotName = "DiscordRpc";
public bool Enabled = false;
[TomlInlineComment("$ChatBot.DiscordRpc.ApplicationId$")]
public string ApplicationId = string.Empty;
[TomlInlineComment("$ChatBot.DiscordRpc.PresenceDetails$")]
public string PresenceDetails = "Playing on {server_host}:{server_port}";
[TomlInlineComment("$ChatBot.DiscordRpc.PresenceState$")]
public string PresenceState = "{dimension} - HP: {health}/{max_health}";
[TomlInlineComment("$ChatBot.DiscordRpc.LargeImageKey$")]
public string LargeImageKey = "mcc_icon";
[TomlInlineComment("$ChatBot.DiscordRpc.LargeImageText$")]
public string LargeImageText = "Minecraft Console Client";
[TomlInlineComment("$ChatBot.DiscordRpc.SmallImageKey$")]
public string SmallImageKey = string.Empty;
[TomlInlineComment("$ChatBot.DiscordRpc.SmallImageText$")]
public string SmallImageText = string.Empty;
[TomlInlineComment("$ChatBot.DiscordRpc.ShowElapsedTime$")]
public bool ShowElapsedTime = true;
[TomlInlineComment("$ChatBot.DiscordRpc.ShowPlayerCount$")]
public bool ShowPlayerCount = true;
[TomlInlineComment("$ChatBot.DiscordRpc.UpdateIntervalSeconds$")]
public int UpdateIntervalSeconds = 10;
public void OnSettingUpdate()
{
ApplicationId ??= string.Empty;
PresenceDetails ??= string.Empty;
PresenceState ??= string.Empty;
LargeImageKey ??= string.Empty;
LargeImageText ??= string.Empty;
SmallImageKey ??= string.Empty;
SmallImageText ??= string.Empty;
if (UpdateIntervalSeconds < 1)
{
UpdateIntervalSeconds = 10;
LogToConsole(BotName, Translations.bot_DiscordRpc_invalid_interval);
}
}
}
private DiscordRpcClient? rpcClient;
private int tickCounter;
private int updateIntervalTicks;
private Timestamps? sessionTimestamps;
private float lastHealth;
public override void Initialize()
{
if (string.IsNullOrWhiteSpace(Config.ApplicationId))
{
LogToConsole(Translations.bot_DiscordRpc_missing_app_id);
UnloadBot();
return;
}
try
{
rpcClient = new DiscordRpcClient(Config.ApplicationId.Trim())
{
Logger = Settings.Config.Logging.DebugMessages
? new ConsoleLogger(LogLevel.Trace)
: new ConsoleLogger(LogLevel.None)
};
rpcClient.OnReady += (_, e) =>
{
LogToConsole(string.Format(Translations.bot_DiscordRpc_connected, e.User.Username));
};
rpcClient.OnConnectionFailed += (_, e) =>
{
LogToConsole(string.Format(Translations.bot_DiscordRpc_connection_failed, e.FailedPipe));
};
rpcClient.Initialize();
updateIntervalTicks = Settings.DoubleToTick(Config.UpdateIntervalSeconds);
if (Config.ShowElapsedTime)
sessionTimestamps = Timestamps.Now;
lastHealth = Handler.GetHealth();
SetPresence();
LogToConsole(Translations.bot_DiscordRpc_initialized);
}
catch (Exception e)
{
LogToConsole(string.Format(Translations.bot_DiscordRpc_init_error, e.Message));
LogDebugToConsole(e.StackTrace ?? string.Empty);
UnloadBot();
}
}
public override void OnUnload()
{
if (rpcClient is { IsDisposed: false })
{
rpcClient.ClearPresence();
rpcClient.Dispose();
}
rpcClient = null;
}
public override void AfterGameJoined()
{
if (Config.ShowElapsedTime)
sessionTimestamps = Timestamps.Now;
SetPresence();
}
public override void Update()
{
tickCounter++;
if (tickCounter < updateIntervalTicks)
return;
tickCounter = 0;
SetPresence();
}
public override void OnHealthUpdate(float health, int food)
{
lastHealth = health;
}
public override bool OnDisconnect(DisconnectReason reason, string message)
{
if (rpcClient is { IsDisposed: false })
rpcClient.ClearPresence();
return false;
}
private void SetPresence()
{
if (rpcClient is null or { IsDisposed: true })
return;
try
{
string details = ReplacePlaceholders(Config.PresenceDetails);
string state = ReplacePlaceholders(Config.PresenceState);
var presence = new RichPresence
{
Details = TruncateForDiscord(details, 128),
State = TruncateForDiscord(state, 128)
};
// Assets (images)
var assets = new Assets();
bool hasAssets = false;
if (!string.IsNullOrWhiteSpace(Config.LargeImageKey))
{
assets.LargeImageKey = Config.LargeImageKey.Trim();
assets.LargeImageText = TruncateForDiscord(
ReplacePlaceholders(Config.LargeImageText), 128);
hasAssets = true;
}
if (!string.IsNullOrWhiteSpace(Config.SmallImageKey))
{
assets.SmallImageKey = Config.SmallImageKey.Trim();
assets.SmallImageText = TruncateForDiscord(
ReplacePlaceholders(Config.SmallImageText), 128);
hasAssets = true;
}
if (hasAssets)
presence.Assets = assets;
// Timestamps
if (Config.ShowElapsedTime && sessionTimestamps is not null)
presence.Timestamps = sessionTimestamps;
// Player count as party
if (Config.ShowPlayerCount)
{
string[] onlinePlayers = GetOnlinePlayers();
int playerCount = onlinePlayers.Length;
if (playerCount > 0)
{
presence.Party = new Party
{
ID = $"mcc_{GetServerHost()}_{GetServerPort()}",
Size = playerCount,
Max = Math.Max(playerCount, playerCount)
};
}
}
rpcClient.SetPresence(presence);
}
catch (Exception e)
{
LogDebugToConsole(string.Format(Translations.bot_DiscordRpc_update_error, e.Message));
}
}
private string ReplacePlaceholders(string template)
{
if (string.IsNullOrEmpty(template))
return string.Empty;
string serverHost = GetServerHost();
int serverPort = GetServerPort();
string username = GetUsername();
float health = Handler.GetHealth();
int food = Handler.GetSaturation();
Location location = GetCurrentLocation();
string[] onlinePlayers = GetOnlinePlayers();
int gamemode = GetGamemode();
int protocolVersion = GetProtocolVersion();
string dimensionName = "Unknown";
try
{
var dim = World.GetDimension();
dimensionName = dim.Name ?? "Unknown";
// Clean up the dimension name for display
if (dimensionName.StartsWith("minecraft:"))
dimensionName = dimensionName["minecraft:".Length..];
dimensionName = dimensionName switch
{
"overworld" => "Overworld",
"the_nether" => "The Nether",
"the_end" => "The End",
_ => dimensionName
};
}
catch
{
// World may not be available
}
string gamemodeStr = gamemode switch
{
0 => "Survival",
1 => "Creative",
2 => "Adventure",
3 => "Spectator",
_ => "Unknown"
};
return template
.Replace("{server_host}", serverHost)
.Replace("{server_port}", serverPort.ToString())
.Replace("{username}", username)
.Replace("{health}", ((int)Math.Ceiling(health)).ToString())
.Replace("{max_health}", "20")
.Replace("{food}", food.ToString())
.Replace("{dimension}", dimensionName)
.Replace("{gamemode}", gamemodeStr)
.Replace("{x}", ((int)location.X).ToString())
.Replace("{y}", ((int)location.Y).ToString())
.Replace("{z}", ((int)location.Z).ToString())
.Replace("{player_count}", onlinePlayers.Length.ToString())
.Replace("{protocol}", protocolVersion.ToString());
}
private static string TruncateForDiscord(string value, int maxLength)
{
if (string.IsNullOrEmpty(value))
return string.Empty;
return value.Length <= maxLength ? value : value[..(maxLength - 3)] + "...";
}
}
}

View file

@ -430,6 +430,7 @@ namespace MinecraftClient
if (Config.ChatBot.ScriptScheduler.Enabled) { BotLoad(new ScriptScheduler()); }
if (Config.ChatBot.TelegramBridge.Enabled) { BotLoad(new TelegramBridge()); }
if (Config.ChatBot.ItemsCollector.Enabled) { BotLoad(new ItemsCollector()); }
if (Config.ChatBot.DiscordRpc.Enabled) { BotLoad(new DiscordRpc()); }
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("MCC_FILE_INPUT")))
BotLoad(new FileInputBot());
}

View file

@ -31,6 +31,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Brigadier.NET" Version="1.2.13" />
<PackageReference Include="DiscordRichPresence" Version="1.143.0" />
<PackageReference Include="DnsClient" Version="1.8.0" />
<PackageReference Include="DSharpPlus" Version="4.5.1" />
<PackageReference Include="DynamicExpresso.Core" Version="2.19.3" />

View file

@ -898,6 +898,105 @@ namespace MinecraftClient {
}
}
/// <summary>
/// Looks up a localized string similar to Show a Discord Rich Presence status with your current Minecraft session info..
/// </summary>
internal static string ChatBot_DiscordRpc {
get {
return ResourceManager.GetString("ChatBot.DiscordRpc", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Your Discord Application ID..
/// </summary>
internal static string ChatBot_DiscordRpc_ApplicationId {
get {
return ResourceManager.GetString("ChatBot.DiscordRpc.ApplicationId", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to The top line of the Rich Presence display. Supports placeholders..
/// </summary>
internal static string ChatBot_DiscordRpc_PresenceDetails {
get {
return ResourceManager.GetString("ChatBot.DiscordRpc.PresenceDetails", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to The second line of the Rich Presence display. Supports placeholders..
/// </summary>
internal static string ChatBot_DiscordRpc_PresenceState {
get {
return ResourceManager.GetString("ChatBot.DiscordRpc.PresenceState", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to The key of the large image asset uploaded to your Discord application..
/// </summary>
internal static string ChatBot_DiscordRpc_LargeImageKey {
get {
return ResourceManager.GetString("ChatBot.DiscordRpc.LargeImageKey", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Tooltip text for the large image. Supports placeholders..
/// </summary>
internal static string ChatBot_DiscordRpc_LargeImageText {
get {
return ResourceManager.GetString("ChatBot.DiscordRpc.LargeImageText", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to The key of the small image asset uploaded to your Discord application (leave empty to hide)..
/// </summary>
internal static string ChatBot_DiscordRpc_SmallImageKey {
get {
return ResourceManager.GetString("ChatBot.DiscordRpc.SmallImageKey", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Tooltip text for the small image. Supports placeholders..
/// </summary>
internal static string ChatBot_DiscordRpc_SmallImageText {
get {
return ResourceManager.GetString("ChatBot.DiscordRpc.SmallImageText", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Show elapsed session time in the Discord presence..
/// </summary>
internal static string ChatBot_DiscordRpc_ShowElapsedTime {
get {
return ResourceManager.GetString("ChatBot.DiscordRpc.ShowElapsedTime", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Show the online player count as a party size in the Discord presence..
/// </summary>
internal static string ChatBot_DiscordRpc_ShowPlayerCount {
get {
return ResourceManager.GetString("ChatBot.DiscordRpc.ShowPlayerCount", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to How often (in seconds) to refresh the Discord presence. Minimum: 1.
/// </summary>
internal static string ChatBot_DiscordRpc_UpdateIntervalSeconds {
get {
return ResourceManager.GetString("ChatBot.DiscordRpc.UpdateIntervalSeconds", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Relay messages between players and servers, like a mail plugin
///This bot can store messages when the recipients are offline, and send them when they join the server

View file

@ -828,6 +828,39 @@ If the connection to the Minecraft game server is blocked by the firewall, set E
<data name="ChatBot.ItemsCollector" xml:space="preserve">
<value>A Chat Bot that collects items on the ground</value>
</data>
<data name="ChatBot.DiscordRpc" xml:space="preserve">
<value>Show a Discord Rich Presence status with your current Minecraft session info.\nRequires a Discord Application ID from https://discord.com/developers/applications\nYou can customize what is shown using placeholders: {server_host}, {server_port}, {username}, {health}, {max_health}, {food}, {dimension}, {gamemode}, {x}, {y}, {z}, {player_count}, {protocol}</value>
</data>
<data name="ChatBot.DiscordRpc.ApplicationId" xml:space="preserve">
<value>Your Discord Application ID. Create one at https://discord.com/developers/applications</value>
</data>
<data name="ChatBot.DiscordRpc.PresenceDetails" xml:space="preserve">
<value>The top line of the Rich Presence display. Supports placeholders.</value>
</data>
<data name="ChatBot.DiscordRpc.PresenceState" xml:space="preserve">
<value>The second line of the Rich Presence display. Supports placeholders.</value>
</data>
<data name="ChatBot.DiscordRpc.LargeImageKey" xml:space="preserve">
<value>The key of the large image asset uploaded to your Discord application.</value>
</data>
<data name="ChatBot.DiscordRpc.LargeImageText" xml:space="preserve">
<value>Tooltip text for the large image. Supports placeholders.</value>
</data>
<data name="ChatBot.DiscordRpc.SmallImageKey" xml:space="preserve">
<value>The key of the small image asset uploaded to your Discord application (leave empty to hide).</value>
</data>
<data name="ChatBot.DiscordRpc.SmallImageText" xml:space="preserve">
<value>Tooltip text for the small image. Supports placeholders.</value>
</data>
<data name="ChatBot.DiscordRpc.ShowElapsedTime" xml:space="preserve">
<value>Show elapsed session time in the Discord presence.</value>
</data>
<data name="ChatBot.DiscordRpc.ShowPlayerCount" xml:space="preserve">
<value>Show the online player count as a party size in the Discord presence.</value>
</data>
<data name="ChatBot.DiscordRpc.UpdateIntervalSeconds" xml:space="preserve">
<value>How often (in seconds) to refresh the Discord presence. Minimum: 1</value>
</data>
<data name="ChatBot.WebSocketBot" xml:space="preserve">
<value>Remotely control the client using Web Sockets.\n# This is useful if you want to implement an application that can remotely and asynchronously execute procedures in MCC.\n# Example implementation written in JavaScript: https://github.com/milutinke/MCC.js.git\n# The protocol specification will be available in the documentation soon.</value>
</data>

View file

@ -1095,6 +1095,69 @@ namespace MinecraftClient {
}
}
/// <summary>
/// Looks up a localized string similar to Please provide a valid Discord Application ID!.
/// </summary>
internal static string bot_DiscordRpc_missing_app_id {
get {
return ResourceManager.GetString("bot.DiscordRpc.missing_app_id", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Connected to Discord as {0}.
/// </summary>
internal static string bot_DiscordRpc_connected {
get {
return ResourceManager.GetString("bot.DiscordRpc.connected", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Failed to connect to Discord (pipe {0}). Is Discord running?.
/// </summary>
internal static string bot_DiscordRpc_connection_failed {
get {
return ResourceManager.GetString("bot.DiscordRpc.connection_failed", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Discord Rich Presence initialized successfully..
/// </summary>
internal static string bot_DiscordRpc_initialized {
get {
return ResourceManager.GetString("bot.DiscordRpc.initialized", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Failed to initialize Discord Rich Presence: {0}.
/// </summary>
internal static string bot_DiscordRpc_init_error {
get {
return ResourceManager.GetString("bot.DiscordRpc.init_error", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Error updating Discord presence: {0}.
/// </summary>
internal static string bot_DiscordRpc_update_error {
get {
return ResourceManager.GetString("bot.DiscordRpc.update_error", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Invalid update interval, must be at least 1 second. Using default of 10 seconds..
/// </summary>
internal static string bot_DiscordRpc_invalid_interval {
get {
return ResourceManager.GetString("bot.DiscordRpc.invalid_interval", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to The bot is already farming!.
/// </summary>
@ -2089,6 +2152,15 @@ namespace MinecraftClient {
}
}
/// <summary>
/// Looks up a localized string similar to DiscordRpc.
/// </summary>
internal static string botname_DiscordRpc {
get {
return ResourceManager.GetString("botname.DiscordRpc", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Farmer.
/// </summary>

View file

@ -463,6 +463,27 @@ cooldown: {6}</value>
<data name="bot.DiscordBridge.unknown_error" xml:space="preserve">
<value>An unknown error has occured!</value>
</data>
<data name="bot.DiscordRpc.missing_app_id" xml:space="preserve">
<value>Please provide a valid Discord Application ID! Get one at https://discord.com/developers/applications</value>
</data>
<data name="bot.DiscordRpc.connected" xml:space="preserve">
<value>Connected to Discord as {0}</value>
</data>
<data name="bot.DiscordRpc.connection_failed" xml:space="preserve">
<value>Failed to connect to Discord (pipe {0}). Is Discord running?</value>
</data>
<data name="bot.DiscordRpc.initialized" xml:space="preserve">
<value>Discord Rich Presence initialized successfully.</value>
</data>
<data name="bot.DiscordRpc.init_error" xml:space="preserve">
<value>Failed to initialize Discord Rich Presence: {0}</value>
</data>
<data name="bot.DiscordRpc.update_error" xml:space="preserve">
<value>Error updating Discord presence: {0}</value>
</data>
<data name="bot.DiscordRpc.invalid_interval" xml:space="preserve">
<value>Invalid update interval, must be at least 1 second. Using default of 10 seconds.</value>
</data>
<data name="bot.farmer.already_running" xml:space="preserve">
<value>The bot is already farming!</value>
</data>
@ -770,6 +791,9 @@ Add the ID of this chat to "Authorized_Chat_Ids" field in the configuration file
<data name="botname.DiscordBridge" xml:space="preserve">
<value>DiscordBridge</value>
</data>
<data name="botname.DiscordRpc" xml:space="preserve">
<value>DiscordRpc</value>
</data>
<data name="botname.Farmer" xml:space="preserve">
<value>Farmer</value>
</data>

View file

@ -1466,6 +1466,13 @@ namespace MinecraftClient
ChatBots.ItemsCollector.Config.OnSettingUpdate();
}
}
[TomlPrecedingComment("$ChatBot.DiscordRpc$")]
public ChatBots.DiscordRpc.Configs DiscordRpc
{
get { return ChatBots.DiscordRpc.Config; }
set { ChatBots.DiscordRpc.Config = value; ChatBots.DiscordRpc.Config.OnSettingUpdate(); }
}
}
}