Discord.js v12 Message On Ban - discord

I don't know the syntax to do an action when a user is banned.
It's also fine for discord.js v11.5.2.

<GuildMember>.ban();
Get a guild member instance by something like msg.mentions.members.first()
Docs:
https://discord.js.org/#/docs/main/stable/class/GuildMember?scrollTo=ban
https://discord.js.org/#/docs/main/stable/examples/moderation

Related

can my bot reply with a message when pinged only

when I ping my discord bot it shows what I made it say, but when someone replies to the bot it still sends the message, I want it to reply only when pinged.
You could add this into your messageCreateevent:
if (message.mentions.has(client.user.id)) return message.channel.send({ content: `My Prefix is ${prefix}` });
You should define your prefix first so it wont get called as undefined and for the bot to show your prefix.
For more information about Discord.js mention: discord.js documentation

How make massban in discord.py?

I used this command, but the bot not ban anyone and does not write an error to the console.
#bot.command()
async def massban(ctx):
for user in ctx.guild.members:
try:
await user.ban()
except:
pass
As Fixator also mentioned, you won't be able to see the error as far as you're excepting every error and passing it.
#bot.command()
async def massban(ctx):
for user in ctx.guild.members:
try:
await user.ban()
except Exception as error:
print(error)
pass
That's how you can catch the error without interrupting the for-loop.
Please also doublecheck the permissions of your bot and the member listing.
If you sure that bot has a ban perms on server.
Check if bot has enough cache for that operation. For example, send len(guild.members) into channel before iterating over members. If it says 1-2, you, most likely, dont have enough intents.
Bot can't ban users that has roles above bot's top role:

Guild delete event returns undefined

So my discord bot smartwiki was recently verified and I have this event where it just sends undefined idk why.. advaith told it comes cause one of the bot server is on an outage but I tried removing bot from some guilds and those guilds weren't on an outage but I get undefined (I have presence intent btw) also thsi isn't cause of intents cuz my guild create works fine this did too but it got undefined after my bot went verified
Code:
client.on('guildDelete', guild =>{
client.user.setActivity(`Serving ${client.guilds.cache.size} Servers with a total of ${client.users.cache.size} users and ${client.channels.cache.size} Channels!`);
const Logs = '809839910675546122'
client.channels.cache.get(Logs).send(
new Discord.MessageEmbed()
.setTitle("I Was Removed From a Guild :c")
.addField("Guild Name" , `${guild.name}`)
.addField("Guild Members" , `${guild.members.cache.size}`)
.addField("Guild Id" , `${guild.id}`)
.addField("Guild Owner" , `<#${guild.ownerID}> | Id: ${guild.ownerID}`)
.setFooter(`SmartWiki is Currently in ${client.guilds.cache.size}guilds!`)
.setTimestamp()
.setThumbnail("https://cdn.discordapp.com/attachments/776925179947384884/808370331856535582/standard_1.gif")
.setColor('RED')
)
})
Image: https://imgur.com/a/IjdioBC
Getting the same thing recently with my bot. Don't know why. Seems to appear before the bot logs in and also after, has been for a few days now. Only on bootup though. Odd...

How do i get a guild by its invite link? discord.js

I've been looking all over the internet and I can't find a answer to my question,
I am trying to get a guild by it's invite link, so like I have a discord invite link such as https://discord.com/invite/ainvitecodegoeshere, Now I want my discord.js bot to get it's guild ID with using only the invite code, Is that possible? If so how can I do it?
I know its a pretty old post, but you can get the guild ID trough the discord api. You can find a link to the documentation here
If you have the invite link, you can use the api by fetching https://discord.com/api/invites/[code] where [code] is the invite code.
It will return some guild information including the guild ID, even if your bot is not in it. To get the guild object, you can use:
const guild = client.guilds.cache.get('guildID')
const Invite = "InviteCode"; // Example: bM2Ae2
client.guilds.cache.forEach(guild => { // Looping through all the guilds your bot is in.
guild.fetchInvites().then(invites => { // Fetching the invites of the guild.
invites.forEach(invite => { // Looping through all the invites of the guild.
if (invite.code == Invite) { // Checking if the invite code matches.
message.channel.send(`Found guild : ${guild.name} | ${guild.id}`);
}
})
})
});
I'd personally use a plain loop instead since you cannot stop the .forEach() loop, and it'll continue to loop through guilds even after it finds the guild you're looking for.
Note: Your bot must be in the guild you want to find.

How can I check if a user is a bot?

I am making a Discord bot and I am using the event guildMemberAdd like this:
bot.on("guildMemberAdd", function(member) {
});
How can I check if the member is a bot or not?
member is of type GuildMember and has a user property of type User, which has a boolean bot property to indicate whether or not the user is a bot. So member.user.bot will tell you if the added member is a bot.

Resources