【问题标题】:Discord.js generate a Channel InviteDiscord.js 生成频道邀请
【发布时间】:2021-08-19 16:12:22
【问题描述】:

所以我正在尝试生成我的机器人发送给只有他可以使用的用户的不和谐频道邀请,因此一次性使用。但我没有得到它,我需要一些帮助。

这是我的代码:

if (reaction.emoji.name === '1️⃣') {
  await reaction.message.guild.members.cache.get(
       user.id);
  if(array.length >= 1){
    if(message.member.roles.cache.has('529770658914369537')){
      const channel = client.channels.cache.find(channel => channel.name === 'Gaming Lounge')
      let invite = channel.createInvite({
        maxAge: 0, 
        maxUses: 1 
      }).catch(console.error);
      console.log(`Here is your Invite: ${invite}`);
      client.users.cache.get(array[0]).send(
        `Here is your Invite: ${invite}`);
      array.splice(0, 1);
      console.log(array);
      reaction.users.remove(user);
    }
  }
}

【问题讨论】:

  • 您面临什么问题?你需要比“没有得到它”更具体
  • 我不知道如何生成 Discord 邀请
  • 所以我需要一个邀请链接,用户可以使用一次,仅此而已
  • 如果我使用上面的代码,我会收到这条消息:这是您的邀请:[object Promise]
  • 您缺少await

标签: javascript


【解决方案1】:

discord.js:

const discord = require('discord.js');

const bot = new discord.Client();

const prefix = '&';

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

bot.on('message', message => { 
    if(!message.content.startsWith(prefix) || 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);
    }
});

bot.login('<Your-Login-Token>');

在我的例子中,我使用前缀“&invite”,但您可以随意更改它。


discord.py:

这个帖子可能有帮助吗?

https://stackoverflow.com/a/65770179/14476782

【讨论】:

    【解决方案2】:

    你可以使用&lt;message&gt;.channel.createInvite()方法

    const channel = client.channels.cache.find(channel => channel.name === 'Gaming Lounge')
    const channelInvite = await channel.createInvite({ maxAge: 60000, maxUses: 1 }).catch(e => console.log(e));
    message.author.send(channelInvite ? channelInvite : 'Oops something went wrong').catch(e => console.log(e)); // incase the user dms are blocked
    

    【讨论】:

    • 好的,它发送不和谐邀请但 maxuse 不起作用,所以我将邀请发送给几个人,它对所有人都有效。但我需要它只工作一次,然后邀请应该是无效的
    猜你喜欢
    • 1970-01-01
    • 2020-04-01
    • 1970-01-01
    • 2021-11-29
    • 2021-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-20
    相关资源
    最近更新 更多