简体   繁体   中英

I am trying to make a discord bot automatically send a one-time-use invite when a user reacts to my message. I'm a bit new and could use help :)

I copied the skeleton of another user, and tried editing a few things but I just can't get the bot to a spot where when I react with the message it automatically generates the code and sends it. My intentions are to react to a permanent message and have the reactee receive a DM from the bot with a unique link. Ideally they can only receive the link one time, even if they leave and join the channel again. I'm sure I've got some big errors in here for my functionality, I'd appreciate some guidance!

const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
const mySecret = process.env['token']

client.once('ready', () => {
    console.log('I am awake');
});

client.on('message', message => { 
    if(reaction.message.name === "\:thumbsup:" || message.author.bot) 
    return;
    
    const args = message.content.slice(prefix.length).split(' ');
    const command = args.shift().toLowerCase();

    const replyWithInvite = async (message) => {
        let invite = await message.channel.createInvite(
            {
                maxAge: 10 * 60 * 1000, // maximum time for the invite, in milliseconds
                maxUses: 1 // maximum times it can be used
            },
            `Requested with command by ${message.author.tag}`
        )
        .catch(console.log);

        message.author.send(invite ? `Here's your invite: ${invite}` : "There has been an error during the creation of the invite.");
    }

    if (command === 'invite') {
        
        replyWithInvite(message);
    }
});

client.login(mySecret);```

The first problem in ur code is you're event. in

const { Client, Intents } = require('discordjs');
require('dotenv').config() // If u're using environment variables for ur token

const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MEMBERS, Intents.FLAGS.GUILD_BANS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_BANS], partials: ['MESSAGE', 'CHANNEL', 'REACTION'] });

client.once('ready', () => {
    console.log('I am awake');
});

client.on('messageReactionAdd', async (reaction, user) => {
    // Check if the reaction is on ur intended message or just some random message on the server
    if (reaction.message.id != urMessageid) return;
  //check if the reaction is thumbsup or not
  if (reaction.emoji.name != 'thumbsup') return;
  // Create the invite now
  const defaultChannel = reaction.message.guild.channels.find(c=> c.permissionsFor(guild.me).has("SEND_MESSAGES"));
  let invite = await defaultChannel.createInvite({
                maxAge: 10 * 60 * 1000, // maximum time for the invite, in milliseconds
                maxUses: 1 ,// maximum times it can be used
                reason: `Requested with command by ${user.tag}`
            }).then(invite => invite).catch(error => console.log(error));
  user.send(`Here's your invite ${invite}`).catch(error => console.log(error));
});

client.login(process.env.TOKEN);

You can find some examples on reactions on the Discordjs V12 guide . Also on a side note for future references you shouldnt use the message event since its deprecated . You can use client#messageCreate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM