簡體   English   中英

我正在嘗試讓 discord 機器人在用戶對我的消息做出反應時自動發送一次性邀請。 我有點新,可以使用幫助:)

[英]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 :)

我復制了另一個用戶的骨架,並嘗試編輯一些東西,但我無法讓機器人到達某個位置,當我對消息作出反應時,它會自動生成代碼並發送。 我的意圖是對永久消息做出反應,並讓被反應者從機器人那里收到帶有唯一鏈接的 DM。 理想情況下,他們只能收到一次鏈接,即使他們離開並再次加入頻道也是如此。 我確定我的功能在這里有一些大錯誤,我會很感激一些指導!

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);```

你的代碼中的第一個問題是你是事件。

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);

您可以在Discordjs V12 指南中找到一些關於反應的示例。 另外在供將來參考的旁注中,您不應該使用message事件,因為它已被棄用 您可以使用客戶端#messageCreate

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM