Starting a thread from a message should not require name #5326
Unanswered
MarcusOtter
asked this question in
API Feature Requests & Ideas
Replies: 2 comments 8 replies
-
Beta Was this translation helpful? Give feedback.
7 replies
-
If anyone has to manually implement this algorithm this is what I came up with using Discord.js. I'm sure I missed a bunch of weird details and off by one errors (coming from Discord themselves) but as far as I could tell this has the same results as the client.
// This is an approximation of Discord's own algorithm done in the Discord client
// It does not, however, strip away invalid characters like dots and slashes.
private generateDefaultThreadName(message: Message): string {
// I also include alt text from attachments, which Discord does not do.
const attachmentText = message.attachments.first()?.description;
if (attachmentText && attachmentText.length > 0) return clampWithElipse(attachmentText, 40);
const embedTitle = message.embeds.length > 0 && message.embeds[0].title;
if (embedTitle && embedTitle.length > 0) return clampWithElipse(embedTitle, 40);
const words = message.content.split(/\s/);
let output = "";
for (const word of words) {
if (output.length + word.length > 41) break;
output += word + " ";
}
if (words.length > 0 && output.length === 0) {
output = clampWithElipse(words[0], 40);
}
// For example, if you posted an attachment without alt text and without content
if (output.length === 0) {
output = "New Thread";
}
// Discord probably has an off by one error with message content,
// making them possible to be 41 chars instead of 40 like embed titles
return clampWithElipse(output.trim(), 41);
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
When you are creating a thread on a message in the client, the
name
of the thread is optional:However, in the API, the
name
is not optional. It would be nice to make it optional so that we can use the same default name algorithm that Discord does, without having to reverse engineer it and hard-code it. Especially since Discord's implementation of a default name might change in the future, creating a disconnect between the bot and Discord. The algorithm is also not that trivial as it may seem at first, because it also handles default names for images and embeds.Right now, there is no way for bots to confidently use the same behavior as Discord does.
Beta Was this translation helpful? Give feedback.
All reactions