feat: User#send
This commit is contained in:
		
							parent
							
								
									718a4658eb
								
							
						
					
					
						commit
						b64bc29fbe
					
				
					 4 changed files with 47 additions and 2 deletions
				
			
		|  | @ -3,6 +3,7 @@ import { Channel } from '../structures/channel.ts' | ||||||
| import { Embed } from '../structures/embed.ts' | import { Embed } from '../structures/embed.ts' | ||||||
| import { Message } from '../structures/message.ts' | import { Message } from '../structures/message.ts' | ||||||
| import type { TextChannel } from '../structures/textChannel.ts' | import type { TextChannel } from '../structures/textChannel.ts' | ||||||
|  | import type { User } from '../structures/user.ts' | ||||||
| import type { | import type { | ||||||
|   ChannelPayload, |   ChannelPayload, | ||||||
|   GuildChannelPayload, |   GuildChannelPayload, | ||||||
|  | @ -21,6 +22,21 @@ export class ChannelsManager extends BaseManager<ChannelPayload, Channel> { | ||||||
|     super(client, 'channels', Channel) |     super(client, 'channels', Channel) | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   async getUserDM(user: User | string): Promise<string | undefined> { | ||||||
|  |     return this.client.cache.get( | ||||||
|  |       'user_dms', | ||||||
|  |       typeof user === 'string' ? user : user.id | ||||||
|  |     ) | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   async setUserDM(user: User | string, id: string): Promise<void> { | ||||||
|  |     await this.client.cache.set( | ||||||
|  |       'user_dms', | ||||||
|  |       typeof user === 'string' ? user : user.id, | ||||||
|  |       id | ||||||
|  |     ) | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   // Override get method as Generic
 |   // Override get method as Generic
 | ||||||
|   async get<T = Channel>(key: string): Promise<T | undefined> { |   async get<T = Channel>(key: string): Promise<T | undefined> { | ||||||
|     const data = await this._get(key) |     const data = await this._get(key) | ||||||
|  | @ -99,7 +115,7 @@ export class ChannelsManager extends BaseManager<ChannelPayload, Channel> { | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     const payload: any = { |     const payload: any = { | ||||||
|       content: content, |       content: content ?? option?.content, | ||||||
|       embed: option?.embed, |       embed: option?.embed, | ||||||
|       file: option?.file, |       file: option?.file, | ||||||
|       files: option?.files, |       files: option?.files, | ||||||
|  | @ -165,7 +181,7 @@ export class ChannelsManager extends BaseManager<ChannelPayload, Channel> { | ||||||
|     const newMsg = await this.client.rest.api.channels[channelID].messages[ |     const newMsg = await this.client.rest.api.channels[channelID].messages[ | ||||||
|       typeof message === 'string' ? message : message.id |       typeof message === 'string' ? message : message.id | ||||||
|     ].patch({ |     ].patch({ | ||||||
|       content: text, |       content: text ?? option?.content, | ||||||
|       embed: option?.embed !== undefined ? option.embed.toJSON() : undefined, |       embed: option?.embed !== undefined ? option.embed.toJSON() : undefined, | ||||||
|       // Cannot upload new files with Message
 |       // Cannot upload new files with Message
 | ||||||
|       // file: option?.file,
 |       // file: option?.file,
 | ||||||
|  |  | ||||||
|  | @ -6,6 +6,8 @@ import { ImageURL } from './cdn.ts' | ||||||
| import type { ImageSize, ImageFormats } from '../types/cdn.ts' | import type { ImageSize, ImageFormats } from '../types/cdn.ts' | ||||||
| import { DEFAULT_USER_AVATAR, USER_AVATAR } from '../types/endpoint.ts' | import { DEFAULT_USER_AVATAR, USER_AVATAR } from '../types/endpoint.ts' | ||||||
| import type { DMChannel } from './dmChannel.ts' | import type { DMChannel } from './dmChannel.ts' | ||||||
|  | import { AllMessageOptions } from './textChannel.ts' | ||||||
|  | import { Message } from './message.ts' | ||||||
| 
 | 
 | ||||||
| export class User extends SnowflakeBase { | export class User extends SnowflakeBase { | ||||||
|   id: string |   id: string | ||||||
|  | @ -94,4 +96,25 @@ export class User extends SnowflakeBase { | ||||||
|   async createDM(): Promise<DMChannel> { |   async createDM(): Promise<DMChannel> { | ||||||
|     return this.client.createDM(this) |     return this.client.createDM(this) | ||||||
|   } |   } | ||||||
|  | 
 | ||||||
|  |   async resolveDM(): Promise<DMChannel> { | ||||||
|  |     const dmID = await this.client.channels.getUserDM(this.id) | ||||||
|  |     const dm = | ||||||
|  |       (dmID !== undefined | ||||||
|  |         ? await this.client.channels.get<DMChannel>(dmID) | ||||||
|  |         : undefined) ?? | ||||||
|  |       (await this.createDM().then((chan) => | ||||||
|  |         this.client.channels.setUserDM(this.id, chan.id).then(() => chan) | ||||||
|  |       )) | ||||||
|  |     // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
 | ||||||
|  |     return dm! | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   async send( | ||||||
|  |     content: string | AllMessageOptions, | ||||||
|  |     options?: AllMessageOptions | ||||||
|  |   ): Promise<Message> { | ||||||
|  |     const dm = await this.resolveDM() | ||||||
|  |     return dm.send(content, options) | ||||||
|  |   } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -204,6 +204,7 @@ export interface AllowedMentionsPayload { | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export interface MessageOptions { | export interface MessageOptions { | ||||||
|  |   content?: string | ||||||
|   tts?: boolean |   tts?: boolean | ||||||
|   embed?: Embed |   embed?: Embed | ||||||
|   file?: MessageAttachment |   file?: MessageAttachment | ||||||
|  |  | ||||||
|  | @ -256,6 +256,11 @@ client.on('messageCreate', async (msg: Message) => { | ||||||
|     msg.reply(buf) |     msg.reply(buf) | ||||||
|   } else if (msg.content === '!addrole') { |   } else if (msg.content === '!addrole') { | ||||||
|     msg.member?.roles.add('837255383759716362') |     msg.member?.roles.add('837255383759716362') | ||||||
|  |   } else if (msg.content === '!dm') { | ||||||
|  |     console.log('wtf') | ||||||
|  |     msg.author.send('UwU').then((m) => { | ||||||
|  |       msg.reply(`Done, ${m.id}`) | ||||||
|  |     }) | ||||||
|   } else if (msg.content === '!timer') { |   } else if (msg.content === '!timer') { | ||||||
|     msg.channel.send('3...').then((msg) => { |     msg.channel.send('3...').then((msg) => { | ||||||
|       setTimeout(() => { |       setTimeout(() => { | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue