-
Notifications
You must be signed in to change notification settings - Fork 25
/
chatcontext.ts
184 lines (163 loc) Β· 6.48 KB
/
chatcontext.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import { MessageFlags } from 'discord-api-types/v10';
import type { AllChannels, Guild, InferWithPrefix, ReturnCache, WebhookMessage } from '../..';
import type { Client, WorkerClient } from '../../client';
import type { If, UnionToTuple, When } from '../../common';
import type { InteractionCreateBodyRequest, InteractionMessageUpdateBodyRequest } from '../../common/types/write';
import {
Message,
type ChatInputCommandInteraction,
type GuildMember,
type InteractionGuildMember,
} from '../../structures';
import { BaseContext } from '../basecontex';
import type { RegisteredMiddlewares } from '../decorators';
import type { OptionResolver } from '../optionresolver';
import type { Command, ContextOptions, OptionsRecord, SubCommand } from './chat';
import type { CommandMetadata, ExtendContext, GlobalMetadata, UsingClient } from './shared';
export interface CommandContext<T extends OptionsRecord = {}, M extends keyof RegisteredMiddlewares = never>
extends BaseContext,
ExtendContext {}
export class CommandContext<
T extends OptionsRecord = {},
M extends keyof RegisteredMiddlewares = never,
> extends BaseContext {
message!: If<InferWithPrefix, Message | undefined, undefined>;
interaction!: If<InferWithPrefix, ChatInputCommandInteraction | undefined, ChatInputCommandInteraction>;
messageResponse?: If<InferWithPrefix, Message | undefined>;
constructor(
readonly client: UsingClient,
data: ChatInputCommandInteraction | Message,
readonly resolver: OptionResolver,
readonly shardId: number,
readonly command: Command | SubCommand,
) {
super(client);
if (data instanceof Message) {
this.message = data as never;
} else {
this.interaction = data;
}
}
options: ContextOptions<T> = {} as never;
metadata: CommandMetadata<UnionToTuple<M>> = {} as never;
globalMetadata: GlobalMetadata = {};
get proxy() {
return this.client.proxy;
}
get t() {
return this.client.t(this.interaction?.locale ?? this.client.langs?.defaultLang ?? 'en-US');
}
get fullCommandName() {
return this.resolver.fullCommandName;
}
async write<FR extends boolean = false>(
body: InteractionCreateBodyRequest,
fetchReply?: FR,
): Promise<When<FR, WebhookMessage | Message, void | WebhookMessage | Message>> {
if (this.interaction) return this.interaction.write(body, fetchReply);
const options = (this.client as Client | WorkerClient).options?.commands;
return (this.messageResponse = await (this.message! as Message)[
!this.messageResponse && options?.reply?.(this) ? 'reply' : 'write'
](body));
}
async deferReply(ephemeral = false) {
if (this.interaction) return this.interaction.deferReply(ephemeral ? MessageFlags.Ephemeral : undefined);
const options = (this.client as Client | WorkerClient).options?.commands;
return (this.messageResponse = await (this.message! as Message)[options?.reply?.(this) ? 'reply' : 'write'](
options?.deferReplyResponse?.(this) ?? { content: 'Thinking...' },
));
}
async editResponse(body: InteractionMessageUpdateBodyRequest) {
if (this.interaction) return this.interaction.editResponse(body);
return (this.messageResponse = await this.messageResponse!.edit(body));
}
deleteResponse() {
if (this.interaction) return this.interaction.deleteResponse();
return this.messageResponse!.delete();
}
editOrReply<FR extends boolean = false>(
body: InteractionCreateBodyRequest | InteractionMessageUpdateBodyRequest,
fetchReply?: FR,
): Promise<When<FR, WebhookMessage | Message, void | WebhookMessage | Message>> {
if (this.interaction) return this.interaction.editOrReply(body as InteractionCreateBodyRequest, fetchReply);
if (this.messageResponse) {
return this.editResponse(body);
}
return this.write(body as InteractionCreateBodyRequest, fetchReply);
}
async fetchResponse(): Promise<
If<InferWithPrefix, WebhookMessage | Message | undefined, WebhookMessage | undefined>
> {
if (this.interaction) return this.interaction.fetchResponse();
this.messageResponse = await this.messageResponse?.fetch();
return this.messageResponse as undefined;
}
channel(mode?: 'rest' | 'flow'): Promise<If<InferWithPrefix, AllChannels | undefined, AllChannels>>;
channel(mode?: 'cache'): ReturnCache<If<InferWithPrefix, AllChannels | undefined, AllChannels>>;
channel(mode: 'cache' | 'rest' | 'flow' = 'cache') {
if (this.interaction?.channel && mode === 'cache')
return this.client.cache.adapter.isAsync ? Promise.resolve(this.interaction.channel) : this.interaction.channel;
switch (mode) {
case 'cache':
return (
this.client.cache.channels?.get(this.channelId) ||
(this.client.cache.adapter.isAsync ? (Promise.resolve() as any) : undefined)
);
default:
return this.client.channels.fetch(this.channelId, mode === 'rest');
}
}
me(mode?: 'rest' | 'flow'): Promise<GuildMember>;
me(mode?: 'cache'): ReturnCache<GuildMember | undefined>;
me(mode: 'cache' | 'rest' | 'flow' = 'cache') {
if (!this.guildId)
return mode === 'cache' ? (this.client.cache.adapter.isAsync ? Promise.resolve() : undefined) : Promise.resolve();
switch (mode) {
case 'cache':
return (
this.client.cache.members?.get(this.client.botId, this.guildId) ||
(this.client.cache.adapter.isAsync ? (Promise.resolve() as any) : undefined)
);
default:
return this.client.members.fetch(this.guildId, this.client.botId, mode === 'rest');
}
}
guild(mode?: 'rest' | 'flow'): Promise<Guild<'cached' | 'api'> | undefined>;
guild(mode?: 'cache'): ReturnCache<Guild<'cached'> | undefined>;
guild(mode: 'cache' | 'rest' | 'flow' = 'cache') {
if (!this.guildId)
return (mode === 'cache'
? this.client.cache.adapter.isAsync
? Promise.resolve()
: undefined
: Promise.resolve()) as unknown as undefined;
switch (mode) {
case 'cache':
return (
this.client.cache.guilds?.get(this.guildId) ||
(this.client.cache.adapter.isAsync ? (Promise.resolve() as any) : undefined)
);
default:
return this.client.guilds.fetch(this.guildId, mode === 'rest');
}
}
get guildId() {
return this.interaction?.guildId || (this.message! as Message | undefined)?.guildId;
}
get channelId() {
return this.interaction?.channelId || (this.message! as Message).channelId;
}
get author() {
return this.interaction?.user || (this.message! as Message).author;
}
get member(): If<
InferWithPrefix,
GuildMember | InteractionGuildMember | undefined,
InteractionGuildMember | undefined
> {
return this.interaction?.member || ((this.message! as Message)?.member as any);
}
isChat(): this is CommandContext {
return true;
}
}