'How to make bot grab perma server invite link
I'm working on a bot project that has invite feature, currently I'm having trouble with this command that allow bot to grab server perma invite link (that made by owner/admin) and send the invite link into channel where the command is executed or send the link into user dm after they do the command
Here's my code that I used
and here's the result I got https://i.stack.imgur.com/AkoV8.png
const db = require('quick.db');
module.exports = {
config: {
name: "addpermaserverinvite",
category: "misc",
description: "Get the permanent invite link of your server!",
accessableby: ["moderators", "admins", "owner"],
usage: "p!addpermaserverinvite",
aliases: ["permaserverinvite"],
}}[enter image description here][1]
module.exports.run = async (bot, message, args) => {
let invite = new MessageEmbed()
.setColor('ORANGE')
.setTitle('Here is your permanent invite link!')
.setDescription(`${message.guild.name}'s permanent invite link is: ${message.guild.invite}`)
.setFooter('PhoeniX Bot Invite Link')
.setTimestamp()
message.channel.send(invite)
}
Solution 1:[1]
Assuming that there is only 1 invite created by the owner, you can use the below code. If there is more than one, this will not work
...
module.exports.run = async (bot, message, args) => {
const owwerId = message.guild.ownerId
const invite = await message.guild.invites.fetch().then(invites => invites.find(inv => inv.inviterId === owwerId))
const inviteEmbed = new MessageEmbed()
.setColor('ORANGE')
.setTitle('Here is your permanent invite link!')
.setDescription(`${message.guild.name}'s permanent invite link is: https://discord.gg/invite/${invite.code}`)
.setFooter('PhoeniX Bot Invite Link')
.setTimestamp()
// if using discord v12
message.channel.send(inviteEmbed)
// to DM it
message.author.send(inviteEmbed)
// if using discord v13
message.channel.send({
embeds: [inviteEmbed]
})
// to DM it
message.author.send({
embeds: [inviteEmbed]
})
}
Solution 2:[2]
Firstly, you can filter invites with option maxAge
which is time expiration option and maxUses
as how many uses should be. If you want to filter it by infinite expiration time, you can filter it as maxAge: 0
.
For fetching invites, you should have "MANAGE_SERVERS" at least.
let invites = await message.guild.invites.fetch()
let infiniteInvite = invites.filter(invite => invite.maxAge === 0 && invite.maxUses === 0).first()
if(infiniteInvite){
message.channel.send({embeds: [new MessageEmbed().setDescription(`Your invite link for server **${message.guild.name}**: [Go!](https://discord.gg/${infiniteInvite.code})`)]})
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Dharman |