Skip to content

Commit

Permalink
feat(discord): addition at support
Browse files Browse the repository at this point in the history
  • Loading branch information
XxLittleCxX committed Mar 7, 2021
1 parent c517bf0 commit 29ee7d1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
17 changes: 17 additions & 0 deletions packages/adapter-discord/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ export interface DiscordMessage {
attachments: Attachment[]
embeds: Embed[]
message_reference?: MessageReference
mention_roles: string
mentions: User[]
}

export interface MessageReference {
Expand Down Expand Up @@ -207,3 +209,18 @@ export interface DiscordChannel {
guild_id?: string
name?: string
}

export interface Role {
id: string
name: string
color: number;
hoist: boolean
position: number
permissions: string
managed: boolean
meantionable: boolean
tags?: {
bot_id: string
integration_id: string
}
}
17 changes: 11 additions & 6 deletions packages/adapter-discord/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Session, segment, MessageInfo, AuthorInfo, GroupInfo, UserInfo, ChannelInfo } from 'koishi-core'
import { AuthorInfo, ChannelInfo, GroupInfo, MessageInfo, segment, Session, UserInfo } from 'koishi-core'
import { DiscordBot } from './bot'
import * as DC from './types'

Expand Down Expand Up @@ -28,7 +28,7 @@ export const adaptAuthor = (author: DC.Author): AuthorInfo => ({
nickname: author.username,
})

export function adaptMessage(bot: DiscordBot, meta: DC.DiscordMessage, session: MessageInfo = {}) {
export async function adaptMessage(bot: DiscordBot, meta: DC.DiscordMessage, session: MessageInfo = {}) {
if (meta.author) {
session.author = adaptAuthor(meta.author)
session.userId = meta.author.id
Expand All @@ -37,8 +37,13 @@ export function adaptMessage(bot: DiscordBot, meta: DC.DiscordMessage, session:
session.content = ''
if (meta.content) {
session.content = meta.content
.replace(/<@!(.+?)>/, (_, id) => segment.at(id))
.replace(/<@&(.+?)>/, (_, id) => segment.at(id))
.replace(/<@[!&](.+?)>/, (_, id) => {
if (meta.mention_roles.includes(id)) {
return segment('at', { role: id })
} else {
return segment('at', { id })
}
})
.replace(/<:(.*):(.+?)>/, (_, name, id) => segment('face', { id: id, name }))
.replace(/<a:(.*):(.+?)>/, (_, name, id) => segment('face', { id: id, name, animated: true }))
.replace(/@everyone/, () => segment('at', { type: 'all' }))
Expand Down Expand Up @@ -91,7 +96,7 @@ export function adaptMessage(bot: DiscordBot, meta: DC.DiscordMessage, session:
}

async function adaptMessageSession(bot: DiscordBot, meta: DC.DiscordMessage, session: Partial<Session.Payload<Session.MessageAction>> = {}) {
adaptMessage(bot, meta, session)
await adaptMessage(bot, meta, session)
session.messageId = meta.id
session.timestamp = new Date(meta.timestamp).valueOf() || new Date().valueOf()
session.subtype = meta.guild_id ? 'group' : 'private'
Expand All @@ -105,10 +110,10 @@ async function adaptMessageSession(bot: DiscordBot, meta: DC.DiscordMessage, ses
}

async function adaptMessageCreate(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
await adaptMessageSession(bot, meta, session)
}

async function adaptMessageModify(bot: DiscordBot, meta: DC.DiscordMessage, session: Partial<Session.Payload<Session.MessageAction>>) {
Expand Down

0 comments on commit 29ee7d1

Please sign in to comment.