-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.js
More file actions
255 lines (240 loc) · 7.71 KB
/
context.js
File metadata and controls
255 lines (240 loc) · 7.71 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/**
* Context abstraction for DiscordBot. Provides message, event, session, and reply helpers.
* @class
*/
class Context {
/**
* Create a new Context instance.
* @param {DiscordBot} bot - The bot instance.
* @param {object} event - Discord event/message object.
* @param {string} senderId - Sender's Discord user ID.
*/
constructor(bot, event, senderId) {
this.bot = bot
this.chat = { id: senderId }
this.text = event.content
this.event = event
this.session = {}
this.scene = null
// Discord-specific: attachments, embeds, etc.
this.attachments = event.attachments || []
this.images = this.attachments.filter((a) => a.type === 'image')
this.files = this.attachments.filter((a) => a.type === 'file')
}
// --- Basic Reply ---
/**
* Reply to the current event/message.
* @param {string|object} textOrPayload - Message text or Discord.js payload object.
* @param {Array|object|null} [buttons=null] - Button(s) to attach.
* @returns {Promise<object>} Discord.js message response.
*/
async reply(textOrPayload, buttons = null) {
let payload
if (typeof textOrPayload === 'string') {
payload = { content: textOrPayload }
} else if (textOrPayload && textOrPayload.content) {
payload = { ...textOrPayload }
} else {
payload = { content: String(textOrPayload) }
}
if (buttons)
payload.components = Array.isArray(buttons) ? buttons : [buttons]
return this.event.channel.send(payload)
}
// --- Reply with Photo ---
/**
* Reply with a photo attachment.
* @param {string|Buffer} urlOrBuffer - URL or Buffer for the photo.
* @param {string} [caption=''] - Caption text.
* @param {Array|object|null} [buttons=null] - Button(s) to attach.
* @returns {Promise<object>} Discord.js message response.
*/
async replyWithPhoto(urlOrBuffer, caption = '', buttons = null) {
let fileData = urlOrBuffer
let fileName = 'photo.jpg'
if (typeof urlOrBuffer === 'string' && urlOrBuffer.startsWith('http')) {
const axios = (await import('axios')).default
const response = await axios.get(urlOrBuffer, {
responseType: 'arraybuffer',
})
fileData = Buffer.from(response.data)
const urlParts = urlOrBuffer.split('/')
fileName = urlParts[urlParts.length - 1] || fileName
}
const payload = {
content: caption,
files: [{ attachment: fileData, name: fileName }],
}
if (buttons)
payload.components = Array.isArray(buttons) ? buttons : [buttons]
return this.event.channel.send(payload)
}
// --- Reply with Document ---
/**
* Reply with a document attachment.
* @param {string|Buffer} urlOrBuffer - URL or Buffer for the document.
* @param {string} [filename='file'] - Filename for the document.
* @param {string} [caption=''] - Caption text.
* @param {Array|object|null} [buttons=null] - Button(s) to attach.
* @returns {Promise<object>} Discord.js message response.
*/
async replyWithDocument(
urlOrBuffer,
filename = 'file',
caption = '',
buttons = null
) {
let fileData = urlOrBuffer
if (typeof urlOrBuffer === 'string' && urlOrBuffer.startsWith('http')) {
const axios = (await import('axios')).default
const response = await axios.get(urlOrBuffer, {
responseType: 'arraybuffer',
})
fileData = Buffer.from(response.data)
}
const payload = {
content: caption,
files: [{ attachment: fileData, name: filename }],
}
if (buttons)
payload.components = Array.isArray(buttons) ? buttons : [buttons]
return this.event.channel.send(payload)
}
// --- Reply with PDF ---
/**
* Reply with a PDF attachment.
* @param {string|Buffer} urlOrBuffer - URL or Buffer for the PDF.
* @param {string} [filename='file.pdf'] - Filename for the PDF.
* @param {string} [caption=''] - Caption text.
* @param {Array|object|null} [buttons=null] - Button(s) to attach.
* @returns {Promise<object>} Discord.js message response.
*/
async replyWithPDF(
urlOrBuffer,
filename = 'file.pdf',
caption = '',
buttons = null
) {
let fileData = urlOrBuffer
if (typeof urlOrBuffer === 'string' && urlOrBuffer.startsWith('http')) {
const axios = (await import('axios')).default
const response = await axios.get(urlOrBuffer, {
responseType: 'arraybuffer',
})
fileData = Buffer.from(response.data)
}
const payload = {
content: caption,
files: [
{ attachment: fileData, name: filename, description: 'PDF file' },
],
}
if (buttons)
payload.components = Array.isArray(buttons) ? buttons : [buttons]
return this.event.channel.send(payload)
}
// --- Kick Member ---
/**
* Kick a member from the server.
* @param {string|object} target - User ID or GuildMember object.
* @param {string} [reason='Kicked by bot'] - Reason for kick.
* @returns {Promise<boolean>} True if successful, false otherwise.
*/
async kickMember(target, reason = 'Kicked by bot') {
let member = null
if (target && typeof target.kick === 'function') {
member = target
} else if (typeof target === 'string' && this.event.guild) {
try {
member = await this.event.guild.members.fetch(target)
} catch {}
}
if (member && typeof member.kick === 'function') {
try {
await member.kick(reason)
return true
} catch {
return false
}
}
return false
}
// --- Ban Member ---
/**
* Ban a member from the server.
* @param {string|object} target - User ID or GuildMember object.
* @param {string} [reason='Banned by bot'] - Reason for ban.
* @returns {Promise<boolean>} True if successful, false otherwise.
*/
async banMember(target, reason = 'Banned by bot') {
let member = null
if (target && typeof target.ban === 'function') {
member = target
} else if (typeof target === 'string' && this.event.guild) {
try {
member = await this.event.guild.members.fetch(target)
} catch {}
}
if (member && typeof member.ban === 'function') {
try {
await member.ban({ reason })
return true
} catch {
return false
}
}
return false
}
// --- Delete Message ---
/**
* Delete a message.
* @param {string|object} [target] - Message ID or Discord.js Message object.
* @returns {Promise<boolean>} True if successful, false otherwise.
*/
async deleteMessage(target) {
let message = target || this.event.message
if (typeof message === 'string' && this.event.channel) {
try {
message = await this.event.channel.messages.fetch(message)
} catch {}
}
if (message && typeof message.delete === 'function') {
try {
await message.delete()
return true
} catch {
return false
}
}
return false
}
// --- Chat Invite Link (TelegrafJS style) ---
/**
* Create a chat invite link for the current channel.
* @param {object} [options={}] - Invite options (maxAge, maxUses, etc).
* @returns {Promise<string>} Invite URL.
*/
async chatInviteLink(options = {}) {
if (
!this.event.channel ||
typeof this.event.channel.createInvite !== 'function'
) {
throw new Error(
'Channel does not support invites or bot lacks permission'
)
}
const invite = await this.event.channel.createInvite({
maxAge: options.maxAge ?? 3600,
maxUses: options.maxUses ?? 1,
temporary: options.temporary ?? false,
unique: options.unique ?? true,
reason: options.reason ?? 'Created by bot',
})
return invite.url
}
}
/**
* Context abstraction for DiscordBot events and replies.
* @type {Context}
*/
export default Context