-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
75 lines (71 loc) · 3.31 KB
/
main.js
File metadata and controls
75 lines (71 loc) · 3.31 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
const config = require('./config.json');
const discord = require('discord.js');
const {Webhook} = require('simple-discord-webhooks');
const client = new discord.Client({intents: [discord.Intents.FLAGS.GUILD_MESSAGES, discord.Intents.FLAGS.GUILDS, discord.Intents.FLAGS.GUILD_WEBHOOKS, discord.Intents.FLAGS.GUILD_MEMBERS]});
client.on('ready', () => {
console.log('READY AS ' + client.user.tag);
});
client.on('messageCreate', async (msg) => {
if (msg.system || msg.author.bot) return;
if (!msg.guild || msg.guild.id !== config.guildID) return;
if (!msg.member.roles.cache.has(config.requiredRole)) return;
const user = config.users.find(u => u.id === msg.author.id);
if (!user) return msg.reply({
components: [{
type: 'ACTION_ROW',
components: [{
customId: 'dismiss',
emoji: '❎',
type: 'BUTTON',
label: 'Dismiss',
style: 'SECONDARY'
}, {customId: 'optout', emoji: '🚫', type: 'BUTTON', label: 'Opt-Out', style: 'DANGER'}]
}],
content: 'You have the participating DID-Role, but no configuration about your identities was found. Please contact <@413429490342035466> to finish setup fully.',
allowedMentions: {parse: []}
});
const possibleFormats = [];
for (const identify of user.names) {
possibleFormats.push({format: user.format.replaceAll('{{name}}', identify.toLowerCase()), name: identify});
}
let matched = null;
let matchedFormat = null;
for (const format of possibleFormats) {
if (matched) continue;
if (msg.content.toLowerCase().includes(format.format)) {
matched = format.name;
matchedFormat = format.format;
}
}
if (!matched) return;
const webhooks = await msg.channel.fetchWebhooks();
let webhook = webhooks.find(w => w.owner.id === client.user.id);
if (!webhook) webhook = await msg.channel.createWebhook(client.user.username);
const w = new Webhook(webhook.url, matched, msg.author.avatarURL());
const regEx = new RegExp(matchedFormat, 'ig');
let content = '';
if ((msg.reference || {}).messageId) {
const m = await msg.channel.messages.fetch(msg.reference.messageId);
content = content + content + `> ${m.content}\n[replied to](${m.url}) <@${m.author.id}>\n\n`;
}
content = content + msg.content.replace(regEx, '');
for (const attachment of msg.attachments.values()) {
content = content + '\n' + attachment.proxyURL;
}
await w.send(content, [], {parse: []});
await msg.delete();
});
client.on('interactionCreate', async (interaction) => {
if (!interaction.isButton()) return;
if (!interaction.member.roles.cache.has(config.requiredRole)) return interaction.reply({
ephemeral: true,
content: 'Sorry, but you are not opted in. Please contact <@413429490342035466>.'
});
if (interaction.message.author.id !== client.user.id) return;
if (interaction.customId === 'dismiss') await interaction.message.delete();
if (interaction.customId === 'optout') await interaction.member.roles.remove(config.requiredRole);
await interaction.reply({content: 'Action executed successfully', ephemeral: true});
});
client.login(config.token).catch((e) => {
console.error('COULD NOT LOG-IN: ' + e);
});