fix: truncate long activity fields to prevent Discord rejection #18
Reference in New Issue
Block a user
Delete Branch "fix/truncate-long-fields"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
truncateTextandtruncateURLhelpers applied insendActivity()before sending to Discord:Name,Details,State,LargeText): truncated to 127 chars +…(Unicode ellipsis) if over 128 runes. Uses[]runefor correct multi-byte character handling.DetailsURL,StateURL,LargeURL,SmallURL): omitted entirely if over 256 characters (a truncated URL would be broken).Fixes #16
Inspired by the same fix in Feishin:
Test plan
truncateText(short, exactly 128, over 128, multi-byte, empty)truncateURL(short, exactly 256, over 256, empty)sendActivitywith long fields verifying truncation and URL omissionDownload the plugin for this PR: discord-rich-presence.zip
Built from
bb7d6aaa5eon 2026-03-04T17:45:29ZSummary of Changes
Hello, I'm Gemini Code Assist1 ! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request addresses an issue where Discord silently rejects Rich Presence updates when certain text or URL fields exceed their character limits. The solution involves introducing helper functions to automatically truncate long text fields and omit overly long URL fields before sending activity data to Discord, thereby ensuring that Rich Presence updates are consistently displayed.
Highlights
truncateTextuses[]runefor correct handling of multi-byte characters, ensuring accurate truncation regardless of character encoding.truncateTextandtruncateURL, along with an integration test forsendActivityto verify the new truncation and omission logic.🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.
Changelog
maxTextLengthandmaxURLLengthconstants for Discord API field limits.truncateTextfunction to shorten strings to 128 runes, appending an ellipsis if truncated.truncateURLfunction to return an empty string if a URL exceeds 256 characters, otherwise returns the original URL.truncateTextandtruncateURLto relevant fields within thesendActivityfunction to enforce Discord's character limits.sendActivityto verify that long text fields are truncated and long URLs are omitted as expected.truncateTextcovering various scenarios like short strings, exact length, over length, multi-byte characters, and empty strings.truncateURLcovering short URLs, exact length, over length, and empty strings.Activity
truncateTextandtruncateURLhelper functions were introduced to handle Discord API field length constraints.sendActivityfunction was updated to apply these new truncation and omission rules to all relevant text and URL fields.truncateTextandtruncateURLfunctions, covering edge cases like multi-byte characters and exact length limits.sendActivityto confirm that the truncation and URL omission logic works correctly when sending activity updates.Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point by creating a comment using either
/gemini <command>or@gemini-code-assist <command>. Below is a summary of the supported commands on the current page./gemini review/gemini summary/gemini helpCustomization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a
.gemini/folder in the base of the repository. Detailed instructions can be found here.Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩︎
Pull request overview
Prevents Discord Rich Presence updates from being silently rejected by enforcing Discord’s field length limits before sending presence payloads.
Changes:
truncateText(128-rune limit with ellipsis) andtruncateURL(omit URLs > 256 chars) helpers.sendActivity()prior to sending to Discord.sendActivity()behavior with oversized fields.Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
rpc.gosendActivity()to keep presence updates within Discord limits.rpc_test.gosendActivity()truncates/omits fields as intended.💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Code Review
This pull request addresses an issue where long field values in Discord presence updates could lead to silent rejection, by introducing
truncateTextandtruncateURLhelper functions. No security vulnerabilities were found. The truncation logic and URL omission appear sound, and the unit tests are comprehensive. My only suggestion is to improve the robustness of the new integration test forsendActivityto make the validation less fragile.@@ -438,0 +482,4 @@Name: longName,Type: 2,Details: longTitle,DetailsURL: longURL,The test validation using
strings.Containsis fragile. It could lead to false positives if the checked substring appears elsewhere in the JSON payload, or if JSON encoding adds escape characters. For example, it doesn't strictly check that the URL fields are omitted, only that the long URL string isn't present.A more robust approach would be to unmarshal the JSON message and assert on the specific field values. This would ensure the correct fields are being truncated and that the URL fields are correctly omitted (by checking for an empty string value, or that the key is not present due to
omitempty).Consider refactoring the check like this: