I’m working on a Discord bot and need help with making it exit specific guilds. Right now I have a basic command that uses client.guilds.cache.get(guildId).leave()
but this only works when I’m actually a member of that particular guild.
The issue is that my bot joined some servers where I don’t have membership anymore, but I still want to remove the bot from those places. I have the server IDs saved so I know exactly which ones to target.
Has anyone figured out how to create a function that can make your bot leave any guild it’s connected to, even if you personally aren’t in that server? I’ve been stuck on this for a while and would love some guidance on the proper approach.
Sounds like you’re hitting cached data issues. When the bot cache doesn’t have fresh guild info, get() returns undefined or outdated stuff. I hit this same problem last year with my mod bot. Skip the cache and use client.guilds.fetch(guildId)
first to grab current data, then call leave() on that guild object. This fixed it for me when I needed to pull my bot from servers I wasn’t in anymore. Your bot stays connected to guilds even if you’re not there personally, so leave() should work fine once you’ve got the right guild reference.
hey, that method should work fine, even if ur not in the guild. bots can leave any server regardless of ur status. try wrapping the leave() call in error handling. also, fetch the guild fresh - might be a stale cache issue.
The leave command isn’t your problem. Your bot can leave any guild it’s in - doesn’t matter if you’re still a member or not. You’re probably hitting a permissions error or the guild object is null when you try to access it. I’ve dealt with this before when cleaning up old servers my utility bot was stuck in. Add some error handling around your leave operation and log what’s actually failing. Sometimes the guild ID is invalid or the bot already left through some other way. Also check if your bot token has the right scope, though basic guild stuff shouldn’t need special permissions beyond what your bot already has.