How can I ban people with my bot who are outside of the server? (reopening)

I still cannot seem to find away to ban users who are outside of my server and require help Im currently using an embed which you can choose if you want to ban or kick a person, which then goes to the button script to do the rest command script:
const { SlashCommandBuilder, EmbedBuilder, PermissionFlagsBits, ActionRowBuilder, ButtonBuilder, ButtonStyle } = require('discord.js');
const mConfig = require('../../messageConfig.json');
const moderationSchema = require("../../schemas/moderation");

module.exports = {
data: new SlashCommandBuilder()
.setName("moderate")
.setDescription("Moderate a server user.")
.addUserOption((o) => o
.setName("user")
.setDescription("The user you want to moderate.")
.setRequired(true)
)
.toJSON(),

userPermissions: [PermissionFlagsBits.Administrator],
botPermissions: [],

run: async (client, interaction) => {
const { options, guildId, guild, member } = interaction;

const user = options.getUser("user");
const targetMember = await guild.members.fetch(user.id);

const rEmbed = new EmbedBuilder()
.setColor("FF0000")
.setFooter({ text: `${client.user.username} - Advanced Moderation System` });

// Check if moderation system exists
let data = await moderationSchema.findOne({ GuildID: guildId });
if (!data) {
rEmbed.setColor(mConfig.embedColorError)
.setDescription("Moderation system not found! Please configure it using `/moderatesystem configure`.");
return interaction.reply({ embeds: [rEmbed], ephemeral: true });
}

// Prevent moderating oneself
if (targetMember.id === member.id) {
rEmbed.setColor(mConfig.embedColorError)
.setDescription(`${mConfig.unableToInteractWithYourself}`);
return interaction.reply({ embeds: [rEmbed], ephemeral: true });
}

// Check role hierarchy
if (targetMember.roles.highest.position >= member.roles.highest.position) {
rEmbed.setColor(mConfig.embedColorError)
.setDescription(`${mConfig.hasHigherRolePosition}`);
return interaction.reply({ embeds: [rEmbed], ephemeral: true });
}

// Check if the member has the required role
const requiredRole = member.guild.roles.cache.find(role => role.name === "G Bot Permissions");
if (!requiredRole || !member.roles.cache.has(requiredRole.id)) {
const roleEmbed = new EmbedBuilder()
.setColor(mConfig.embedColorError)
.setDescription("You do not have the required role to use this command.");
return interaction.reply({ embeds: [roleEmbed], ephemeral: true });
}

const moderationButtons = new ActionRowBuilder().setComponents(
new ButtonBuilder().setCustomId("banBtn").setLabel("Server Ban").setStyle(ButtonStyle.Danger),
new ButtonBuilder().setCustomId("kickBtn").setLabel("Server Kick").setStyle(ButtonStyle.Danger),
new ButtonBuilder().setCustomId("cancelBtn").setLabel("Cancel").setStyle(ButtonStyle.Secondary),
);

rEmbed.setAuthor({
name: `${targetMember.user.username}`,
iconURL: `${targetMember.user.displayAvatarURL({ dynamic: true })}`
}).setDescription(`What action would you like to perform on: ${targetMember.user.username}?`);

interaction.reply({ embeds: [rEmbed], components: [moderationButtons] });
},
};
const { SlashCommandBuilder, EmbedBuilder, PermissionFlagsBits, ActionRowBuilder, ButtonBuilder, ButtonStyle } = require('discord.js');
const mConfig = require('../../messageConfig.json');
const moderationSchema = require("../../schemas/moderation");

module.exports = {
data: new SlashCommandBuilder()
.setName("moderate")
.setDescription("Moderate a server user.")
.addUserOption((o) => o
.setName("user")
.setDescription("The user you want to moderate.")
.setRequired(true)
)
.toJSON(),

userPermissions: [PermissionFlagsBits.Administrator],
botPermissions: [],

run: async (client, interaction) => {
const { options, guildId, guild, member } = interaction;

const user = options.getUser("user");
const targetMember = await guild.members.fetch(user.id);

const rEmbed = new EmbedBuilder()
.setColor("FF0000")
.setFooter({ text: `${client.user.username} - Advanced Moderation System` });

// Check if moderation system exists
let data = await moderationSchema.findOne({ GuildID: guildId });
if (!data) {
rEmbed.setColor(mConfig.embedColorError)
.setDescription("Moderation system not found! Please configure it using `/moderatesystem configure`.");
return interaction.reply({ embeds: [rEmbed], ephemeral: true });
}

// Prevent moderating oneself
if (targetMember.id === member.id) {
rEmbed.setColor(mConfig.embedColorError)
.setDescription(`${mConfig.unableToInteractWithYourself}`);
return interaction.reply({ embeds: [rEmbed], ephemeral: true });
}

// Check role hierarchy
if (targetMember.roles.highest.position >= member.roles.highest.position) {
rEmbed.setColor(mConfig.embedColorError)
.setDescription(`${mConfig.hasHigherRolePosition}`);
return interaction.reply({ embeds: [rEmbed], ephemeral: true });
}

// Check if the member has the required role
const requiredRole = member.guild.roles.cache.find(role => role.name === "G Bot Permissions");
if (!requiredRole || !member.roles.cache.has(requiredRole.id)) {
const roleEmbed = new EmbedBuilder()
.setColor(mConfig.embedColorError)
.setDescription("You do not have the required role to use this command.");
return interaction.reply({ embeds: [roleEmbed], ephemeral: true });
}

const moderationButtons = new ActionRowBuilder().setComponents(
new ButtonBuilder().setCustomId("banBtn").setLabel("Server Ban").setStyle(ButtonStyle.Danger),
new ButtonBuilder().setCustomId("kickBtn").setLabel("Server Kick").setStyle(ButtonStyle.Danger),
new ButtonBuilder().setCustomId("cancelBtn").setLabel("Cancel").setStyle(ButtonStyle.Secondary),
);

rEmbed.setAuthor({
name: `${targetMember.user.username}`,
iconURL: `${targetMember.user.displayAvatarURL({ dynamic: true })}`
}).setDescription(`What action would you like to perform on: ${targetMember.user.username}?`);

interaction.reply({ embeds: [rEmbed], components: [moderationButtons] });
},
};
38 Replies
d.js toolkit
d.js toolkitโ€ข3mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
Sheepboi546
Sheepboi546OPโ€ข3mo ago
btn script:
d.js docs
d.js docsโ€ข3mo ago
To share long code snippets, use a service like gist, sourcebin, pastebin, or similar instead of posting them as large code blocks or files.
treble/luna
treble/lunaโ€ข3mo ago
also you use a try catch yet never log the error whats the use of it then
Sheepboi546
Sheepboi546OPโ€ข3mo ago
idk why its not on there its my script let me update it
Sheepboi546
Sheepboi546OPโ€ข3mo ago
Pastebin
const { PermissionFlagsBits, EmbedBuilder } = require("discord.js")...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Pastebin
const { SlashCommandBuilder, EmbedBuilder, PermissionFlagsBits, Act...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Sheepboi546
Sheepboi546OPโ€ข3mo ago
i just get unknown member and i've been trying to fix it and yet i just cant seem to find a way because im quite new lol
treble/luna
treble/lunaโ€ข3mo ago
because a member isnt guaranteed to be in the guild in which case the .fetch will reject you do not even need the member object just the id
Sheepboi546
Sheepboi546OPโ€ข3mo ago
how would I change the script to do that then#
treble/luna
treble/lunaโ€ข3mo ago
by just using the id directly in the ban call
d.js docs
d.js docsโ€ข3mo ago
:method: GuildBanManager#create() @14.16.2 Bans a user from the guild.
// Ban a user by id (or with a user/guild member object)
guild.bans.create('84484653687267328')
.then(banInfo => console.log(`Banned ${banInfo.user?.tag ?? banInfo.tag ?? banInfo}`))
.catch(console.error);
// Ban a user by id (or with a user/guild member object)
guild.bans.create('84484653687267328')
.then(banInfo => console.log(`Banned ${banInfo.user?.tag ?? banInfo.tag ?? banInfo}`))
.catch(console.error);
Sheepboi546
Sheepboi546OPโ€ข3mo ago
Alright thank you Alright im still get "Unknown Member" errors and my embeds arent working so I don't know what to do
treble/luna
treble/lunaโ€ข3mo ago
show the updated code
Sheepboi546
Sheepboi546OPโ€ข3mo ago
i've actually just lost it i forgot to save my code so its gone wow ๐Ÿ˜ญ mind looking into https://discord.com/channels/222078108977594368/1284215275918594232/1284216717396148306 these too and just have a look at it when you can would be greatly apperciated
treble/luna
treble/lunaโ€ข3mo ago
well i gave you an answer
Sheepboi546
Sheepboi546OPโ€ข3mo ago
yeah but i've tried and it keep saying unknown member and a few other errors so
treble/luna
treble/lunaโ€ข3mo ago
so what is your updated code then
Sheepboi546
Sheepboi546OPโ€ข3mo ago
i lost it because i forgot to save and i aint going into the pain of rewriting my whole script again
treble/luna
treble/lunaโ€ข3mo ago
well then whats your question if you;re not gonna use that code again
Sheepboi546
Sheepboi546OPโ€ข3mo ago
^^ I mean this I still have that but I did the updated code and I lost that so im back to that basically at square 1
treble/luna
treble/lunaโ€ข3mo ago
still this
Sheepboi546
Sheepboi546OPโ€ข3mo ago
alright i'll give it anothe rgo i'll get back to you if anythiny happens
Sheepboi546
Sheepboi546OPโ€ข3mo ago
@one fluffy floofy wolvinny ๐ŸŒˆ https://pastebin.com/v1ZqaucE https://pastebin.com/GTrXr5z6
Pastebin
const { SlashCommandBuilder, EmbedBuilder, PermissionFlagsBits, Act...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Pastebin
const { PermissionFlagsBits, EmbedBuilder } = require("discord.js")...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Sheepboi546
Sheepboi546OPโ€ข3mo ago
updated if I've forgetten something obvious then oops also ignore the kickBtn i'll handle that leter
Sheepboi546
Sheepboi546OPโ€ข3mo ago
No description
treble/luna
treble/lunaโ€ข3mo ago
you never reply to the interaction only when you encounter some sort of error
Sheepboi546
Sheepboi546OPโ€ข3mo ago
How shall I fix that then?
treble/luna
treble/lunaโ€ข3mo ago
by replying to the intreraction thus using interaction.update instead of message.edit
Sheepboi546
Sheepboi546OPโ€ข3mo ago
Kk updated it still not working
treble/luna
treble/lunaโ€ข3mo ago
so whats your current code
Sheepboi546
Sheepboi546OPโ€ข3mo ago
the pastebin i sent
treble/luna
treble/lunaโ€ข3mo ago
thats not your updated code
Sheepboi546
Sheepboi546OPโ€ข3mo ago
its the exact same apart from the interaction.update
d.js docs
d.js docsโ€ข3mo ago
If you aren't getting any errors, try to place console.log checkpoints throughout your code to find out where execution stops. - Once you do, log relevant values and if-conditions - More sophisticated debugging methods are breakpoints and runtime inspections: learn more
Sheepboi546
Sheepboi546OPโ€ข3mo ago
alright it breaks on the button script where
const userId = interaction.customId.split("_")[1];
const userId = interaction.customId.split("_")[1];
is
treble/luna
treble/lunaโ€ข3mo ago
are you sure that shouldnt stop your code
Sheepboi546
Sheepboi546OPโ€ข3mo ago
No description
No description
Sheepboi546
Sheepboi546OPโ€ข3mo ago
ignore the twos that was what i was testing Alright I changed it And yeah itโ€™s stopping at the That code I said
Want results from more Discord servers?
Add your server