-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.ts
185 lines (169 loc) · 8.4 KB
/
index.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
185
import { config } from 'dotenv';
config();
import { Client, TextChannel, Snowflake, Message, Collection, GuildMember, IntentsBitField, PermissionsBitField } from 'discord.js';
const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
const client = new Client({
intents: [IntentsBitField.Flags.Guilds, IntentsBitField.Flags.GuildMessages, IntentsBitField.Flags.GuildMembers, IntentsBitField.Flags.MessageContent],
});
let channel: TextChannel|null = null;
let initialMessageID: Snowflake|null = null;
const content =
`**Type the number of the problem you're experiencing and I'll do my best to help you!**
\`1\` - I want to setup the bot (join/leave messages, prefix, etc...)
\`2\` - I want to use the web dashboard
\`3\` - I want to know how to use the bot
\`4\` - ManageInvite says my server needs to be upgraded
\`5\` - My problem is not in the list`;
const hasLeftWarningMessage = ':warning: The member who opened the ticket has just left the server, so the ticket can probably be closed!';
const ticketToolID = '557628352828014614';
const usersTicketChannels = new Collection<Snowflake, Snowflake>();
client.on('ready', async () => {
console.log(`Ready. Logged as ${client.user!.tag}.`);
channel = client.channels.cache.get(process.env.SUPPORT_CHANNEL_ID!) as TextChannel;
await channel.guild.members.fetch();
channel.messages.fetch().then(async (messages) => {
initialMessageID = messages.last()?.id!;
if(!initialMessageID){
initialMessageID = (await channel!.send(content)).id;
}
setInterval(() => {
channel!.messages.fetch().then((fetchedMessages) => {
const messagesToDelete = fetchedMessages.filter((m) => (Date.now() - m.createdTimestamp) > 60000 && m.id !== initialMessageID);
channel!.bulkDelete(messagesToDelete);
});
}, 10000);
});
const ticketsNotResolved: TextChannel[] = [];
for(const ticketChannelID of channel.guild.channels.cache.filter((channel) => channel.name.includes('ticket-') && channel.isTextBased()).map((channel) => channel.id)){
const ticketChannel = client.channels.cache.get(ticketChannelID) as TextChannel;
const messages = await ticketChannel.messages.fetch();
const creationMessage = messages.find((message) => message.author.id === ticketToolID && message.embeds.length > 0 && message.content && message.content.includes('Welcome'));
const userID = creationMessage?.mentions.users.first()!.id;
const hasLeft = !ticketChannel.guild.members.cache.has(userID!);
if(creationMessage && userID){
usersTicketChannels.set(userID, ticketChannelID);
} else {
ticketsNotResolved.push(ticketChannel);
}
if(hasLeft && !messages.some((message) => message.content === hasLeftWarningMessage)){
client.emit('guildMemberRemove', { id: userID } as GuildMember);
}
}
console.log(`\n${usersTicketChannels.size} ticket channels resolved. (missing ${ticketsNotResolved.map((channel) => `#${channel.name}`).join(' | ')})`);
});
interface MessageData {
messageID: Snowflake;
timeout: any;
}
const relatedMessages: Collection<Snowflake, MessageData> = new Collection();
const sendAndDeleteAfter = (message: Message, content: string) => {
message.channel.send(content).then((m) => {
const timeout = setTimeout(() => {
relatedMessages.delete(message.id);
message.delete();
m.delete();
}, 60000);
relatedMessages.set(message.id, {
messageID: m.id,
timeout
});
});
};
client.on('messageDelete', (message) => {
const relatedMessageData = relatedMessages.get(message.id);
if(relatedMessageData){
channel!.messages.fetch(relatedMessageData.messageID).then((m) => {
m.delete();
clearTimeout(relatedMessageData.timeout);
relatedMessages.delete(message.id);
});
}
})
client.on('messageCreate', (message) => {
if(message.partial) return;
if(message.author.bot) return;
if(message.channel.id === process.env.SUPPORT_CHANNEL_ID){
switch(message.content){
case "1":
sendAndDeleteAfter(
message,
`Hello ${message.author.toString()}, you can configure join messages, leave messages or join messages in direct messages by using the dashboard: **<https://manage-invite.xyz/>** (recommended) or the \`/configjoin\`, \`/configleave\` and \`/configdmjoin\` commands.`
);
break;
case "2":
sendAndDeleteAfter(
message,
`Hello ${message.author.toString()}, you can access the web dashboard by using this link: **<https://manage-invite.xyz>**.`
);
break;
case "3":
sendAndDeleteAfter(
message,
`Hello ${message.author.toString()}, to get the list of all the commands, you can start typing \`/\` and click on ManageInvite.\nYou can also type \`1\` to know how to configure the bot!`
);
break;
case "4":
sendAndDeleteAfter(
message,
`Hello ${message.author.toString()}, this is because you didn't buy ManageInvite. Indeed, our services are no longer free, to keep a good speed and uptime for users who really need a stable bot. Nevertheless, we want ManageInvite to remain accessible to all, that's why the price has been lowered from $5 to $2 per month. You want to try the bot? Ask for a 7-day trial period in <#${process.env.TRIAL_CHANNEL_ID}>! It's totally free, just send the link to your server.`,
);
break;
case "5":
(client.channels.cache.get(process.env.CONTACT_CHANNEL_ID!) as TextChannel).permissionOverwrites.edit(message.author, {
ViewChannel: true
});
sendAndDeleteAfter(
message,
`Hello ${message.author.toString()}, you now have access to the <#${process.env.CONTACT_CHANNEL_ID}> channel. You can click the :envelope: reaction to open a ticket!`
);
break;
default:
sendAndDeleteAfter(
message,
`Hello, ${message.author.toString()}, you must type a valid number corresponding to the issue you are experiencing.`
);
}
} else if(
(/(discord\.(gg|io|me|li)\/.+|(discord|discordapp)\.com\/invite\/.+)/i.test(message.content))
&& !((message.channel as TextChannel).name.startsWith("ticket-"))
&& !(message.member!.permissions.has(PermissionsBitField.Flags.ManageMessages))
){
message.delete();
message.reply("you are not able to send server invites.").then((m) => {
m.delete().then(() => {
setTimeout(() => {
message.delete();
}, 5000);
});
});
}
});
client.on('channelCreate', async (channel) => {
if(!channel.isTextBased()) return;
const createdChannel = channel as TextChannel;
if(!createdChannel.name.includes('ticket-')) return;
await delay(5000);
const creationMessage = createdChannel.messages.cache.find((message) => message.author.id === ticketToolID && message.embeds.length > 0 && message.content && message.content.includes('Welcome'));
const userID = creationMessage?.mentions.users.first()!.id;
const hasLeft = !createdChannel.guild.members.cache.has(userID!);
if(creationMessage && userID){
usersTicketChannels.set(userID, createdChannel.id);
}
if(hasLeft && !createdChannel.messages.cache.some((message) => message.content === hasLeftWarningMessage)){
client.emit('guildMemberRemove', { id: userID } as GuildMember);
}
});
client.on('channelDelete', (channel) => {
const userID = usersTicketChannels.findKey((channelID) => channelID === channel.id);
if(userID){
usersTicketChannels.delete(userID);
}
});
client.on('guildMemberRemove', async (member) => {
const ticketChannelID = usersTicketChannels.get(member.id) as Snowflake;
const ticketChannel = client.channels.cache.get(ticketChannelID) as TextChannel;
if(ticketChannel){
ticketChannel.send(hasLeftWarningMessage);
}
});
client.login(process.env.TOKEN);