UnknownJs

adv command

Feb 24th, 2023
863
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const {SlashCommandBuilder} = require('@discordjs/builders');
  2. const {EmbedBuilder, ButtonBuilder, ButtonStyle, ActionRowBuilder, PermissionFlagsBits, ChannelType} = require('discord.js')
  3. const config = require('../../config.json')
  4. const {buildEmbed} = require('../../functions/errorEmbed')
  5. const emojis = require('../../util/emojis.json')
  6. const {checkManager} = require('../../functions/isManager')
  7. const posted = []
  8. const cooldown = []
  9. const missingperms = []
  10. const missingchannel = []
  11. const missingserver = []
  12. const lessamount = []
  13.  
  14. exports.run = async (client, interaction) => {
  15.     const {options, member, guild} = interaction
  16.     const invite = options.getString('invite')
  17.     let manager = await checkManager(client, guild, member)
  18.     if (!manager) return interaction.reply({
  19.         ephemeral: true,
  20.         embeds: [buildEmbed('You are not authorized to run this command.')]
  21.     })
  22.  
  23.     //validating invite from slash command itself
  24.     if (!isInvite(invite)) return interaction.reply({
  25.         ephemeral: true,
  26.         embeds: [buildEmbed('Invalid invite code format, learn more about invites [here](https://support.discord.com/hc/en-us/articles/208866998-Invites-101)')]
  27.     })
  28.     const inv = await client.fetchInvite(invite)
  29.     if (!inv) return interaction.reply({
  30.         ephemeral: true,
  31.         embeds: [buildEmbed('I couldn\'t find that invite, please try again later.')]
  32.     })
  33.     if (inv.guild.id != interaction.guild.id) return interaction.reply({
  34.         ephemeral: true,
  35.         embeds: [buildEmbed(`[Server Invite](${invite}) you have specified doesn't belong this server, please try with this server invite`)]
  36.    })
  37.    await client.rclient.hSet(`guild${guild.id}`, "invitelink", invite)
  38.  
  39.  
  40.    //making buttons for invites
  41.    const bt = []
  42.    const b = new ButtonBuilder()
  43.        .setURL(invite)
  44.        .setStyle(ButtonStyle.Link)
  45.        .setLabel('Invite')
  46.    bt.push(b)
  47.  
  48.    const appeal = options.getString('appeal')
  49.    if (appeal) {
  50.        const bb = new ButtonBuilder()
  51.            .setStyle(ButtonStyle.Link)
  52.            .setLabel('Appeals')
  53.            .setURL(appeal)
  54.        bt.push(bb)
  55.    }
  56.  
  57.    const row = new ActionRowBuilder().addComponents(bt)
  58.  
  59.    //heist amount
  60.    let coins = options.getString('amount')
  61.    let amount = 0
  62.    const conditions = ['k', 'm', 'b'];
  63.    if (conditions.some((el) => coins.includes(el))) {
  64.        let object = {
  65.            k: 'e3',
  66.            m: 'e6',
  67.            b: 'e9',
  68.        }
  69.        amount = coins.replace(/k|m|b/g, function (m) {
  70.            return object[m];
  71.        })
  72.    } else amount = Number(coins)
  73.    amount = Number(amount)
  74.  
  75.    let ad = options.getString('ad')
  76.    ad = ad.split('\\n')
  77.  
  78.    const type = options.getString('adtype')
  79.    if(type != 'heist') {
  80.        return interaction.reply({
  81.            ephemeral: true,
  82.            embeds: [buildEmbed(`Posting advertisements for other than Dankmemer heists is an upcoming feature, please join our [support server](${config.supportserver.Invite}) to know more.`)]
  83.        })
  84.    }
  85.    const s = {
  86.        'heist': "Dankmemer heist",
  87.        'broheist': "Bro heist",
  88.        'minigame': "Minigame"
  89.    }
  90.  
  91.    const em = new EmbedBuilder()
  92.        .setColor(client.config.embedHex)
  93.        .setDescription(`${ad.join('\n')}`)
  94.        .setTitle('Preview of your **' + s[type] + '** ad')
  95.        .setFooter({text: "You have 30 seconds to respond, server invite and appeals link won't be displayed."})
  96.        .setImage("https://media.discordapp.net/attachments/883293909093068833/1044666862170030160/Picsart_22-11-22_18-32-52-233.jpg")
  97.  
  98.     const e = new ActionRowBuilder().addComponents(
  99.         new ButtonBuilder()
  100.             .setStyle(ButtonStyle.Danger)
  101.             .setCustomId(`${interaction.id}-heistad_cancel`)
  102.             .setLabel('Cancel')
  103.             .setEmoji(emojis.fail),
  104.  
  105.         new ButtonBuilder()
  106.             .setStyle(ButtonStyle.Success)
  107.             .setCustomId(`${interaction.id}-heistad_confirm`)
  108.             .setLabel('Confirm')
  109.             .setEmoji(emojis.success)
  110.     )
  111.  
  112.     let msg = await interaction.reply({embeds: [em], components: [e], fetchReply: true})
  113.     const filter = async res => {
  114.         if (res.user.id != interaction.user.id) {
  115.             return interaction.followUp({ephemeral: true, embeds: [buildEmbed("This is not your advertise menu.")]})
  116.         } else {
  117.             await res.deferUpdate();
  118.             return true;
  119.         }
  120.     }
  121.     const collector = await msg.createMessageComponentCollector({time: 30000, filter})
  122.     collector.on('collect', async int => {
  123.         let customId = int.customId
  124.         customId = customId.split('-')[1]
  125.         customId = customId.split('_')[1]
  126.  
  127.         if (customId === 'cancel') {
  128.             await collector.stop()
  129.             msg.edit({embeds: [buildEmbed("Invoke cancelled.")], components: []})
  130.         } else if (customId === 'confirm') {
  131.             await advertise(client, guild, amount, msg)
  132.         }
  133.     })
  134.  
  135. }
  136. exports.help = {
  137.     name: "advertise",
  138.     description: "posts your advertisement among your partner servers",
  139.     usage: ["advertise"],
  140.     example: ["advertise"]
  141. }
  142.  
  143. exports.conf = {
  144.     aliases: [],
  145.     cooldown: 10,
  146.     data: new SlashCommandBuilder()
  147.         .setName(exports.help.name)
  148.         .setDescription(exports.help.description)
  149.         .addStringOption(opt => {
  150.             return opt
  151.                 .setName('adtype')
  152.                 .setDescription('enter what type of advertisement you are posting.')
  153.                 .addChoices(
  154.                     {name: 'Dankmemer Heists', value: 'heist'},
  155.                     {name: 'Brobot Heists', value: 'broheist'},
  156.                     {name: 'Minigames', value: 'minigame'}
  157.                 )
  158.                 .setRequired(true)
  159.         })
  160.         .addStringOption(opt => {
  161.             return opt
  162.                 .setName('amount')
  163.                 .setDescription('enter your heist amount (can be a number like "250" or a shorthand like "50k")')
  164.                 .setRequired(true)
  165.         })
  166.         .addStringOption(opt => {
  167.             return opt
  168.                 .setName('invite')
  169.                 .setDescription('please specify your server invite, this will be displayed with heist ad')
  170.                 .setRequired(true)
  171.         })
  172.         .addStringOption(opt => {
  173.             return opt
  174.                 .setName('ad')
  175.                 .setDescription('specify your ad, make sure to include all details (invite, time ..etc)')
  176.                 .setRequired(true)
  177.         })
  178.         .addStringOption(opt => {
  179.             return opt
  180.                 .setName('appeal')
  181.                 .setDescription('specify ban appeals link here, It could be server or forms')
  182.                 .setRequired(false)
  183.         })
  184. }
  185.  
  186. function isInvite(string) {
  187.     const regex = /(https?:\/\/)?(www\.)?(discord\.(gg|io|me|li)|discordapp\.com\/invite)\/.+[a-z]/
  188.     return regex.test(string)
  189. }
  190.  
  191.  
  192. async function sleep(ms) {
  193.     return new Promise(resolve => setTimeout(resolve, ms));
  194. }
  195.  
  196. async function advertise(client, guild, amount, mess) {
  197.     await mess.edit({embeds: [buildEmbed(`${emojis.loading} please wait while your heist ad is being posted.`)]})
  198.     const partners = await client.rclient.hGetAll(`partdeals${guild.id}`)
  199.  
  200.     for(const partner in partners) {
  201.         try {
  202.             const pguild = await client.guilds.fetch(partner)
  203.             const deal = await client.rclient.hGet(`partdeals${guild.id}`, `${pguild.id}`)
  204.             let chid = deal.split('/')[0]
  205.             try {
  206.                 let channel = await pguild.channels.fetch(chid)
  207.                 console.log(channel)
  208.                 if(!checkPerms(client, channel)) return missingperms.push(pguild.id)
  209.                 if(!await client.rclient.EXISTS(`heistadcd${guild.id}${pguild.id}`)) return cooldown.push(pguild.id)
  210.                 const db = await client.rclient.HGETALL(`guild ${pguild.id}`)
  211.                 const minamt = db.minheistamount ?? 0
  212.                 if(minamt > amount) return lessamount.push(pguild.id)
  213.  
  214.                 let pings = deal.split('/')[1]
  215.                 let mm
  216.                 if(!posted.includes(pguild.id)) {
  217.                     channel.send({content: `${mess}\n${pings}`})
  218.                 }
  219.                 posted.push(pguild.id)
  220.                 if(channel.type === ChannelType.GuildNews) mm.crosspost()
  221.             }
  222.             catch(e) {
  223.                 console.log(e)
  224.                 missingchannel.push(pguild.id)
  225.             }
  226.         }
  227.         catch(e) {
  228.             console.log(e)
  229.             missingserver.push(partner)
  230.         }
  231.  
  232.     }
  233. }
  234.  
  235. function checkPerms(client, channel) {
  236.     //assumptions is channel exist
  237.     let hasperms = false
  238.     if(channel.permissionsFor(client.user.id).has(PermissionFlagsBits.ViewChannel)) {
  239.         if(channel.permissionsFor(client.user.id).has(PermissionFlagsBits.SendMessages)) {
  240.             if(channel.permissionsFor(client.user.id).has(PermissionFlagsBits.MentionEveryone)) {
  241.                 hasperms = true
  242.             }
  243.         }
  244.     }
  245.     return hasperms;
  246. }
Advertisement
Add Comment
Please, Sign In to add comment