1
1
import {
2
- type APIMessage ,
3
2
type BaseMessageOptions ,
4
3
ButtonStyle ,
5
4
CommandInteraction ,
8
7
Message ,
9
8
MessageComponentInteraction ,
10
9
PermissionFlagsBits ,
11
- PermissionsBitField ,
12
- ThreadChannel ,
13
10
time ,
11
+ ThreadChannel ,
14
12
Webhook ,
15
13
} from "discord.js"
16
14
import { getSelf } from "../guilds/getSelf"
@@ -19,34 +17,39 @@ import { fetchWebhooks } from "../webhooks/fetchWebhooks"
19
17
import { fetchMessage } from "./fetchMessage"
20
18
import { restoreMessage } from "./restoreMessage"
21
19
22
- export const fetchAndRestoreMessage = async (
20
+ export enum RestoreMode {
21
+ // Just restore the message
22
+ Restore ,
23
+ // Require that the message is from a webhook, and include the webhook
24
+ QuickEdit ,
25
+ // Act like quick edit if the message is from a webhook, and like restore otherwise
26
+ Open ,
27
+ }
28
+
29
+ export const restoreMessageAndReply = async (
23
30
interaction : CommandInteraction | MessageComponentInteraction ,
24
- channelId : string ,
25
- messageId : string ,
26
- quickEdit = false ,
31
+ message : Message ,
32
+ mode : RestoreMode = RestoreMode . Restore ,
27
33
) => {
28
- const message = await fetchMessage ( interaction , channelId , messageId )
29
- if ( ! message ) return
30
-
31
- const selfPermissions =
32
- "guild" in message . channel && message . channel . guild
33
- ? message . channel . permissionsFor ( await getSelf ( message . channel . guild ) )
34
- : new PermissionsBitField ( PermissionsBitField . Default )
35
-
36
34
let webhook : Webhook | undefined = undefined
37
35
const components : BaseMessageOptions [ "components" ] = [ ]
36
+
38
37
if (
39
38
message . webhookId &&
40
- selfPermissions . has ( PermissionFlagsBits . ManageWebhooks )
39
+ message . inGuild ( ) &&
40
+ // Check permissions for the bot
41
+ message . channel
42
+ . permissionsFor ( await getSelf ( message . channel . guild ) )
43
+ . has ( PermissionFlagsBits . ManageWebhooks )
41
44
) {
45
+ // Now check permissions for the member triggering this
42
46
const member =
43
47
interaction . member instanceof GuildMember
44
48
? interaction . member
45
49
: await interaction . guild ?. members . fetch ( interaction . user . id )
46
50
47
51
if (
48
52
member &&
49
- "guild" in message . channel &&
50
53
message . channel
51
54
. permissionsFor ( member )
52
55
. has ( PermissionFlagsBits . ManageWebhooks )
@@ -58,23 +61,23 @@ export const fetchAndRestoreMessage = async (
58
61
const webhooks = await fetchWebhooks ( root ! )
59
62
60
63
webhook = webhooks . find ( ( webhook ) => webhook . id === message . webhookId )
61
- if ( webhook && ! quickEdit ) {
64
+ if ( webhook && mode == RestoreMode . Restore ) {
62
65
components . push ( {
63
66
type : ComponentType . ActionRow ,
64
67
components : [
65
68
{
66
69
type : ComponentType . Button ,
67
70
style : ButtonStyle . Secondary ,
68
71
label : "Quick Edit" ,
69
- customId : `@discohook/restore-quick-edit/${ channelId } -${ messageId } ` ,
72
+ customId : `@discohook/restore-quick-edit/${ message . channelId } -${ message . id } ` ,
70
73
} ,
71
74
] ,
72
75
} )
73
76
}
74
77
}
75
78
}
76
79
77
- if ( ! webhook && quickEdit ) {
80
+ if ( mode == RestoreMode . QuickEdit && ! webhook ) {
78
81
await reply ( interaction , {
79
82
content :
80
83
"I can't find the webhook this message belongs to, therefore " +
@@ -84,25 +87,10 @@ export const fetchAndRestoreMessage = async (
84
87
}
85
88
86
89
if ( message . content || message . embeds . length > 0 ) {
87
- const response = await restoreMessage (
88
- message ,
89
- quickEdit ? webhook : undefined ,
90
- )
91
- await reply ( interaction , {
92
- embeds : [
93
- {
94
- title : "Restored message" ,
95
- description :
96
- `The restored message can be found at ${ response . url } . This link ` +
97
- `will expire ${ time ( new Date ( response . expires ) , "R" ) } .` ,
98
- } ,
99
- ] ,
100
- components,
101
- } )
102
- return
103
- }
104
-
105
- if ( ! webhook ) {
90
+ message = message
91
+ } else if ( webhook ) {
92
+ message = await webhook . fetchMessage ( message . id )
93
+ } else {
106
94
await reply ( interaction , {
107
95
content :
108
96
"I can't read the message because of Discord's privacy restrictions. " +
@@ -112,20 +100,30 @@ export const fetchAndRestoreMessage = async (
112
100
return
113
101
}
114
102
115
- const webhookMessage = await webhook . fetchMessage ( messageId )
116
- const response = await restoreMessage (
117
- webhookMessage as Message | APIMessage ,
118
- quickEdit ? webhook : undefined ,
119
- )
103
+ const editTarget = mode == RestoreMode . Restore ? undefined : webhook
104
+ const response = await restoreMessage ( message , editTarget )
105
+
120
106
await reply ( interaction , {
121
107
embeds : [
122
108
{
123
- title : "Restored message" ,
109
+ title : editTarget ? "Opened for editing" : "Restored message" ,
124
110
description :
125
- `The restored message can be found at ${ response . url } . This link ` +
111
+ `The message editor can be found at ${ response . url } . This link ` +
126
112
`will expire ${ time ( new Date ( response . expires ) , "R" ) } .` ,
127
113
} ,
128
114
] ,
129
115
components,
130
116
} )
131
117
}
118
+
119
+ export const fetchAndRestoreMessage = async (
120
+ interaction : CommandInteraction | MessageComponentInteraction ,
121
+ channelId : string ,
122
+ messageId : string ,
123
+ mode : RestoreMode = RestoreMode . Restore ,
124
+ ) => {
125
+ const message = await fetchMessage ( interaction , channelId , messageId )
126
+ if ( ! message ) return
127
+
128
+ restoreMessageAndReply ( interaction , message , mode )
129
+ }
0 commit comments