Untitled
unknown
javascript
a year ago
6.5 kB
5
Indexable
const { Client, GatewayIntentBits } = require('discord.js'); const { REST } = require('@discordjs/rest'); const { Routes } = require('discord-api-types/v10'); const axios = require('axios'); require('dotenv').config(); console.log(`discord token ${process.env.BOT_TOKEN}!`); const token = process.env.BOT_TOKEN; const clientId = process.env.CLIENT_ID; const guildId = process.env.GUILD_ID; const listChannelId = process.env.LIST_CHANNEL_ID; // Replace with your channel ID const postChannelId = process.env.POST_CHANNEL_ID; const mentionRoleId = process.env.MENTION_ROLE_ID; const allowedRoleIds = ['1204540657730068631']; const analyticsURL = process.env.ANALYTICS_ENDPOINT const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent ] }); let numbersList = new Set(); let submissionsOpen = false; let timer; let islandUrlDictionary; client.once('ready', async () => { console.log(`Logged in as ${client.user.tag}!`); const rest = new REST({ version: '10' }).setToken(token); const channel = client.channels.cache.get(listChannelId); //prepare island list for announcements if (!channel) { console.error('Could not find the channel.'); return; } channel.messages.fetch({ limit: 1 }).then(messages => { let lastMessage = messages.first(); if (lastMessage) { console.log(`Last message: ${lastMessage.content}`); islandUrlDictionary = createDictionaryFromMessage(lastMessage.content); } else { console.log('No messages found.'); } }).catch(console.error); try { await rest.put( Routes.applicationGuildCommands(clientId, guildId), { body: [ { name: 'start', description: 'Start the number submission and begin the timer', }, { name: 'submit', description: 'Submit your number', options: [{ type: 4, name: 'number', description: 'The number to submit', required: true, }], }, { name: 'announce', description: 'Announce the next island in the channel', options: [{ type: 4, name: 'number', description: 'The island number to announce', required: true, }], }, { name: 'end', description: 'End the number submission and post the list', }, ], }, ); console.log('Successfully reloaded application (/) commands.'); } catch (error) { console.error(error); } }); client.on('interactionCreate', async interaction => { if (!interaction.isCommand()) return; const { commandName } = interaction; if (commandName === 'start' && isUserAllowed(interaction)) { if (!submissionsOpen) { submissionsOpen = true; numbersList.clear(); timer = setTimeout(() => { submissionsOpen = false; postList().catch(console.error); }, 10 * 60 * 1000); // 10 min close await interaction.reply('Submissions are now open.'); } else { await interaction.reply({ content: 'Submissions are already open.', ephemeral: true }); } } else if (commandName === 'submit') { if (submissionsOpen) { const number = interaction.options.getInteger('number'); if (numbersList.has(number)) { await interaction.reply({ content: `Number ${number} has already been submitted!`, ephemeral: true }); } else { numbersList.add(number); await interaction.reply({ content: `Number ${number} submitted!`, ephemeral: true }); } } else { await interaction.reply({ content: 'Submissions are not open at this time.', ephemeral: true }); } } else if (commandName === 'end' && isUserAllowed(interaction)) { clearTimeout(timer); submissionsOpen = false; await postList(); await interaction.reply('Submissions are now closed and the list has been posted.'); }else if (commandName === 'announce' && isUserAllowed(interaction)) { const number = interaction.options.getInteger('number'); islandUrl = islandUrlDictionary[number]; if(islandUrl === '') { await interaction.reply('no island found with this number'); }else{ postData = { island_url: islandUrl, //set with activeisland }; try { response = await axios.post(analyticsURL, postData); console.log('Data posted successfully'); console.log(response.data); // Response from the server await interaction.reply('analytics successfully started'); } catch (error) { console.error('Error during the POST request', error); await interaction.reply('analytics server is down, announcement will still be posted'); } postNextActiveIsland(islandUrl); } } }); function createDictionaryFromMessage(message) { const dictionary = {}; const lines = message.split('\n'); for (const line of lines) { const parts = line.split('. '); if (parts.length === 2) { const number = parts[0].trim(); const url = parts[1].trim(); dictionary[number] = url; } } return dictionary; } async function postNextActiveIsland(islandUrl) { const channel = await client.channels.cache.get(postChannelId); channel.send(`<@&${mentionRoleId}> Next active Island:\n- ${islandUrl}`); } async function postList() { const formattedList = shuffle(Array.from(numbersList)).join('\n- '); const channel = await client.channels.cache.get(postChannelId); channel.send(`<@&${mentionRoleId}> Final randomized list:\n- ${formattedList}`); numbersList.clear(); submissionsOpen = true; // Reset } function isUserAllowed(interaction) { return interaction.member.permissions.has('ADMINISTRATOR') || interaction.member.roles.cache.some(role => allowedRoleIds.includes(role.id)); } function shuffle(array) { for (let i = array.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [array[i], array[j]] = [array[j], array[i]]; } return array; } client.login(token);
Editor is loading...
Leave a Comment