Home > front end >  Trouble making a command to force bot to leave a server. (Discord,js)
Trouble making a command to force bot to leave a server. (Discord,js)

Time:10-05

const Discord = require('discord.js')
const client = new Discord.Client({ws: {intents: Discord.Intents.ALL}});
const botowner = 'xxx'

exports.run = async (bot,message,args) => {
   let guildid = message.guild.id
 if (message.author.id = botowner) {
    toleave = client.get_server(guildid)
    await client.leave_server(toleave)
 } else {message.channel.send("You are not the bot owner")}
    
}

exports.help = {
    name: ['leavediscordserver']
    }

Whenever I run this code the following shows up:enter image description here

Not sure why this is happening.

CodePudding user response:

var guildID = client.guilds.cache.get(args[0]) //your argument would be the server id
    if (message.author.id == botowner) { // use == to check if author and bot owner id match
    await guildID.leave() // waiting for bot to leave the server
 } else {message.channel.send("You are not the bot owner")}
    
}

CodePudding user response:

The error message tells you all you need to know. You attempt to use a function called get_server(), but this function does not exist ergo you cannot use it.

A discord server in Discord.js is referred to as a Guild so to get your guild you can just call const guild = client.guilds.cache.fetch(guildid) and to leave it all you have to do is call guild.leave().

As a separate issue, a comparison in an if statement is made with 2 equal signs == not 1 as you did in your if (message.author.id = botowner) line.

  • Related