d2m username colors #107

Open
mschae wants to merge 8 commits from mschae/out-of-your-element:role-colors into main
3 changed files with 35 additions and 10 deletions
Showing only changes of commit 1e717a1e59 - Show all commits

also write accent color to profile

mschae23 2026-11-07 00:01:17 +01:00
Signed by untrusted user: mschae
GPG key ID: A87721D300DE88AB

View file

@ -107,6 +107,10 @@ async function userToGlobalProfile(user) {
globalProfile.avatar_url = await file.uploadDiscordFileToMxc(avatarPath)
}
if (user.accent_color) {
globalProfile.name_color = `#${user.accent_color.toString(16).padStart(6, "0")}`
}
return globalProfile
}
@ -140,14 +144,14 @@ async function memberToStateContent(user, member, guild) {
content.avatar_url = await file.uploadDiscordFileToMxc(avatarPath)
}
const colorRole = member ? guild.roles.find(role => role.colors.primary_color && member.roles.includes(role.id)) : undefined
if (colorRole) {
const encoded = `#${colorRole.colors.primary_color.toString(16).padStart(6, "0")}`
content["eu.she-a.color"] = {
"on_dark": encoded,
"on_light": encoded
}
}
const colorRole = member ? guild.roles.find(role => role.colors.primary_color && member.roles.includes(role.id)) : undefined
if (colorRole) {
const encoded = `#${colorRole.colors.primary_color.toString(16).padStart(6, "0")}`
content["eu.she-a.color"] = {
"on_dark": encoded,
"on_light": encoded
}
}
return content
}
@ -248,9 +252,9 @@ async function syncUser(user, member, channel, guild, roomID, interactionMetadat
/**
* @param {string} roomID
* @param {string} mxid
* @param {{displayname: string, avatar_url?: string}} content
* @param {{displayname: string, avatar_url?: string, "eu.she-a.color"?: {on_light: string, on_dark: string}}} content
* @param {number | null} powerLevel
* @param {{allowOverwrite?: boolean, globalProfile?: {displayname: string, avatar_url?: string}}} [options]
* @param {{allowOverwrite?: boolean, globalProfile?: {displayname: string, avatar_url?: string, name_color?: string}}} [options]
*/
async function _sendSyncUser(roomID, mxid, content, powerLevel, options) {
const currentHash = _hashProfileContent(content, powerLevel ?? 0)
@ -272,6 +276,10 @@ async function _sendSyncUser(roomID, mxid, content, powerLevel, options) {
if (await supportsMsc4069) {
actions.push(api.profileSetDisplayname(mxid, options?.globalProfile?.displayname || content.displayname, true))
actions.push(api.profileSetAvatarUrl(mxid, options?.globalProfile?.avatar_url || content.avatar_url, true))
if (options?.globalProfile?.name_color || content["eu.she-a.color"]?.on_light) {
// @ts-ignore
actions.push(api.profileSetNameColor(mxid, options?.globalProfile?.name_color ?? content["eu.she-a.color"]?.on_light, true))
}
}
await Promise.all(actions)
// Update cached hash

View file

@ -156,6 +156,8 @@ async function onPacket(client, message, listen) {
// Role doesn't already exist.
guild.roles.push(...newRoles)
}
// lower position values mean lower priority (`@everyone` has position 0)
guild.roles.sort((a, b) => b.position - a.position)
}
} else if (message.t === "GUILD_MEMBER_UPDATE") {

View file

@ -449,6 +449,20 @@ async function profileSetAvatarUrl(mxid, avatar_url, inhibitPropagate) {
}
}
/**
* @param {string} mxid
* @param {string} color
* @param {boolean} [inhibitPropagate]
*/
async function profileSetNameColor(mxid, color, inhibitPropagate) {
const params = {}
if (inhibitPropagate) params["org.matrix.msc4069.propagate"] = false
await mreq.mreq("PUT", path(route`/client/v3/profile/${mxid}/eu.she-a.color`, mxid, params), {
"on_light": color,
"on_dark": color
})
}
async function ping() {
// not using mreq so that we can read the status code
const res = await fetch(mreq.baseUrl + route`/client/v1/appservice/${reg.id}/ping`, {
@ -673,6 +687,7 @@ module.exports.redactEvent = redactEvent
module.exports.sendTyping = sendTyping
module.exports.profileSetDisplayname = profileSetDisplayname
module.exports.profileSetAvatarUrl = profileSetAvatarUrl
module.exports.profileSetNameColor = profileSetNameColor
module.exports.ping = ping
module.exports.previewUrlIfEnabled = previewUrlIfEnabled
module.exports.getMedia = getMedia