Search code examples
node.jsmongodbdiscord.jsquick.db

Discord.js v14.3.0 creating an invitation to a given guild


i want create invite bot, but a lot errors. I use discord.js v14.3.0. Idk how repair this, in discord.js support any ideas. (for my bad english sorry but i from poland)

My code:

const Discord = require("discord.js")
const { QuickDB } = require("quick.db");
const db2 = new QuickDB();
const mongodb = require("mongodb");

const username = encodeURIComponent('censored');
const password = encodeURIComponent("censored")
const dbname = 'censored';
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
const clusterUrl = "censored";
const authMechanism = "DEFAULT";
const url = `mongodb://${username}:${password}@${clusterUrl}/${username}`;
const client = new mongodb.MongoClient(url);    
const db = client.db(dbname)
client.connect().then(() => {
    console.log("Data: Connected")
}).catch(err => console.error(err)) 


module.exports = { 
    data: new SlashCommandBuilder()
        .setName("invite")
        .setDescription("Create invite to download server"),
        //.addStringOption(opt => opt.setChoices()),
    execute: async function(interaction, guilds, client, invites) {
        if(interaction instanceof Discord.CommandInteraction) {
        //Co sie wykonuje

const guild = interaction.client.guilds.cache.get(`1013773663910232184`)
        
       // let invite = client.guild("1013773663910232184").id
        //.createInvite({ maxAge: 180000, maxUses: 1 })
        const invitecode = guild.invites.create( { maxAge: 18000, maxUses: 1} )
        db.collection('invites').insertOne({
            user_id: `${interaction.member.id}`,
            invite_link: `${invitecode.url}`
        })
        interaction.reply({ content: `This is your invite: ${invitecode.url} , DO NOT SEND THIS LINK TO ANYONE. Invite expires in 3 minutes.`, ephemeral: true})
        

        }   

        }
    }

Console always send this error: Ideas?

    if (!id) throw new Error(ErrorCodes.GuildChannelResolve);
                   ^

Error [GuildChannelResolve]: Could not resolve channel to a guild channel.
    at GuildInviteManager.create (C:\Users\smile\Desktop\pieroconfigs\pieroconfigsmain\node_modules\discord.js\src\managers\GuildInviteManager.js:183:20)
    at Object.execute (C:\Users\smile\Desktop\pieroconfigs\pieroconfigsmain\handlers\commands\invite.js:34:42)
    at Client.<anonymous> (C:\Users\smile\Desktop\pieroconfigs\pieroconfigsmain\handlers\cmdManager.js:40:31)
    at Client.emit (node:events:539:35)
    at Client.emit (node:domain:475:12)
    at InteractionCreateAction.handle (C:\Users\smile\Desktop\pieroconfigs\pieroconfigsmain\node_modules\discord.js\src\client\actions\InteractionCreate.js:81:12)   
    at Object.module.exports [as INTERACTION_CREATE] (C:\Users\smile\Desktop\pieroconfigs\pieroconfigsmain\node_modules\discord.js\src\client\websocket\handlers\INTERACTION_CREATE.js:4:36)
    at WebSocketManager.handlePacket (C:\Users\smile\Desktop\pieroconfigs\pieroconfigsmain\node_modules\discord.js\src\client\websocket\WebSocketManager.js:352:31)  
    at WebSocketShard.onPacket (C:\Users\smile\Desktop\pieroconfigs\pieroconfigsmain\node_modules\discord.js\src\client\websocket\WebSocketShard.js:481:22)
    at WebSocketShard.onMessage (C:\Users\smile\Desktop\pieroconfigs\pieroconfigsmain\node_modules\discord.js\src\client\websocket\WebSocketShard.js:321:10) {       
  code: 'GuildChannelResolve'
}

Ideas?

I use discord.js v14 (14.3.0).

I want the bot to create an invitation to a given server with the settings.

Edit:

const { SlashCommandBuilder } = require("@discordjs/builders")
const Discord = require("discord.js")
const { QuickDB } = require("quick.db");
const db2 = new QuickDB();
const mongodb = require("mongodb");

const username = encodeURIComponent('censored');
const password = encodeURIComponent("censored")
const dbname = 'censored';
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
const clusterUrl = "censored";
const authMechanism = "DEFAULT";
const url = `mongodb://${username}:${password}@${clusterUrl}/${username}`;
const client = new mongodb.MongoClient(url);    
const db = client.db(dbname)
client.connect().then(() => {
    console.log("Data: Connected")
}).catch(err => console.error(err)) 


module.exports = { 
    data: new SlashCommandBuilder()
        .setName("invite")
        .setDescription("Create invite to download server"),

    execute: async function(interaction, guilds, client, invites) {
        if(interaction instanceof Discord.CommandInteraction) {


const guild = interaction.client.guilds.cache.get(`1013773663910232184`)
if(await db.collection('blacklist').findOne({
    user_id: `${interaction.member.id}`
 })){
    return interaction.reply({ content: `You cant acces to this command! (Error code: 502)`, ephemeral: true})
 }

 const invitecodegetindatabase = await db.collection('invites').findOne({   user_id: `${interaction.member.id}`,})

 if(invitecodegetindatabase) {
console.log("Znaleziono #1")
 }

// if(await db.collection('invites').findOne({
 //   user_id: `${interaction.member.id}`
 //})) {
  //  return interaction.reply({ content: `You always have invite code! This is your invite code: ${invitecodegetindatabase}`})
 //}

     
        const invitecode = await guild.invites.create('1013793499189100574', { maxAge: 18000, maxUses: 1} );
        db.collection('invites').insertOne({
            user_id: `${interaction.member.id}`,
            invite_link: `${invitecode.url}`
     })
        interaction.reply({ content: `This is your invite: ${invitecode.url} , DO NOT SEND THIS LINK TO ANYONE. Invite expires in 3 minutes.`, ephemeral: true})
        
        


console.log(`${invitecodegetindatabase}`)

        }   

        }
    }

Solution

  • From the docs it can be seen that the GuildInviteManager#create method takes 2 arguments: channel and [options]

    const invitecode = guild.invites.create('put channel id here', { maxAge: 18000, maxUses: 1} );
    

    It couldn't resolve to a channel because there was no channel specified (since invites to a guild are done on a channel basis).