Skip to content

Commit

Permalink
feat(discord): return full edited message, add nickname
Browse files Browse the repository at this point in the history
  • Loading branch information
XxLittleCxX committed Mar 8, 2021
1 parent 29ee7d1 commit dfcb7da
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/adapter-discord/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ export class DiscordBot extends Bot<'discord'> {
timestamp: new Date(msg.timestamp).valueOf(),
author: adaptUser(msg.author),
}
result.author.nickname = msg.member?.nick
if (msg.message_reference) {
const quoteMsg = await this.getMessageFromServer(msg.message_reference.channel_id, msg.message_reference.message_id)
result.quote = await adaptMessage(this, quoteMsg)
Expand Down
5 changes: 5 additions & 0 deletions packages/adapter-discord/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ export interface DiscordMessage {
message_reference?: MessageReference
mention_roles: string
mentions: User[]
member?: {
user?: User
nick?: string
roles: string[]
}
}

export interface MessageReference {
Expand Down
16 changes: 8 additions & 8 deletions packages/adapter-discord/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export async function adaptMessage(bot: DiscordBot, meta: DC.DiscordMessage, ses
session.author = adaptAuthor(meta.author)
session.userId = meta.author.id
}
if (meta.member?.nick) {
session.author.nickname = meta.member?.nick
}
// https://discord.com/developers/docs/reference#message-formatting
session.content = ''
if (meta.content) {
Expand Down Expand Up @@ -116,13 +119,6 @@ async function adaptMessageCreate(bot: DiscordBot, meta: DC.DiscordMessage, sess
await adaptMessageSession(bot, meta, session)
}

async function adaptMessageModify(bot: DiscordBot, meta: DC.DiscordMessage, session: Partial<Session.Payload<Session.MessageAction>>) {
await adaptMessageSession(bot, meta, session)
session.groupId = meta.guild_id
session.subtype = meta.guild_id ? 'group' : 'private'
session.channelId = meta.channel_id
}

export async function adaptSession(bot: DiscordBot, input: DC.Payload) {
const session: Partial<Session.Payload<Session.MessageAction>> = {
selfId: bot.selfId,
Expand All @@ -135,7 +131,11 @@ export async function adaptSession(bot: DiscordBot, input: DC.Payload) {
if (session.userId === bot.selfId) return
} else if (input.t === 'MESSAGE_UPDATE') {
session.type = 'message-updated'
await adaptMessageModify(bot, input.d as DC.DiscordMessage, session)
const d = input.d as DC.DiscordMessage
const msg = await bot.getMessageFromServer(d.channel_id, d.id)
// Unlike creates, message updates may contain only a subset of the full message object payload
// https://discord.com/developers/docs/topics/gateway#message-update
await adaptMessageCreate(bot, msg, session)
if (!session.content) return
if (session.userId === bot.selfId) return
} else if (input.t === 'MESSAGE_DELETE') {
Expand Down

0 comments on commit dfcb7da

Please sign in to comment.