Discord.py on guild leave - discord

So I was wondering if it is possible for a bot to send a message when the bot has been removed from a server, so far I have this and it hasn't worked
async def on_guild_leave(guild):
channel = client.get_channel(993919891902042197)
await channel.send(f"bot has left name: {guild.name}, owner: {guild.owner}, guild owner ID: {guild.owner.id}, guild ID:{guild.id} and we have {len(client.guilds)} servers")```

The correct event is on_guild_remove().
Please read https://discordpy.readthedocs.io/en/stable/api.html#discord.on_guild_remove for more information on the event.
The working code would be like this:
async def on_guild_remove(guild):
# do something...
Take note that Intents.guilds must be enabled in order for this event to be detected, as stated in the documentation linked above.

Related

Discord.py wait_for command when user adds reaction

So I need some help!
So far I have the code do the following... Someone inputs the !stage command and then their stage(int)... It sends a message in another where the FTO will react to the message. When reacted the FTO will get a DM with info!
So my question is how do I use wait_for for the user who originally sent the command to be contacted by the bot when the FTO clicks the reactions. Like I need the bot to send that original command user a DM with who clicked the reaction.
#client.command()
async def stage(ctx, *, stage):
ftochannel = client.get_channel(1043289756794093639)
logchannel = client.get_channel(1037550844935147622)
embed=discord.Embed(title=f"{ctx.author.mention} has requested ride along for stage **{stage}**",description=f"If you want to assist this member with their requested stage react below!", color=0x660066)
log=discord.Embed(title=f"Discord Log // User Requested Ride Along in BCSO Discord",description=f'Ran By: {ctx.author.mention}\nActual Command Finish: {ctx.author.mention} has just requested the following stage: {stage}! ',color=0x660066)
emoji = "\N{Large Yellow Circle}"
msg = await ftochannel.send(embed=embed)
await msg.add_reaction(emoji)
await ctx.send("When an FTO member answers me You will get a message from me with more information! Please be patient!")
await logchannel.send(embed=log)
#client.event
async def on_raw_reaction_add(payload):
if str(payload.emoji) == "\N{Large Yellow Circle}":
member = payload.member
sendEmbedToUser = discord.Embed(title=f'Hello', description=f'You have accepted THIS PERSONS Stage RIDE ALONG!\nPlease Arrive in <#1038926472259313732> within **10 Minutes**!\n\nHere is some helpful documentation for your ride along:\n[LINK SHORTNER 1]\n[LINK SHORTNER 1]\n[LINK SHORTNER 1]\n[LINK SHORTNER 1]')
await member.send(embed=sendEmbedToUser)
[]
With some help from friends I no know I need to used wait_for somewhere in the stage command but I have no clue how to do it!

Discord.py doesn't update on member remove

I am making a discord bot that changes a voice channels' name when a user joins or leaves to the amount of members on the server. My issue is that when a user leaves, it doesn't update. Any help is appreciated.
#bot.event
async def on_raw_member_remove(member):
channel = discord.utils.get(member.guild.channels, id=973603264639668248)
await channel.edit(name=f'Member Count: {member.guild.member_count}')
The right event for this would be on_member_remove.
You can also get the channel in a much easier way and edit it.
See a possible new code:
#bot.event
async def on_member_remove(member: discord.Member):
channel = bot.get_channel(Channel_ID_here)
await channel.edit(name=f"Member Count: {len(member.guild.members)}")
The reason it doesn't work is because you are using on_raw_member_remove
You should be using on_member_leave as the member is not getting removed.

How to get permissions in discord js when we do direct message

I am trying to get a list of the permission that the user has in Discord. If it sends the message in the channel, it’s fine as we can use message.member.hasPermission, etc.
But what if the message is DM? I want my users to send a DM to the bot and the bot be able to check and see if the user has certain permissions.
I cannot find anything anywhere. I keep getting redirected to message.member, or message.guild which both are null when it’s in DM.
In DM no one has permissions. All you have is the permission to see messages and send messages which aren’t shown visually, or to bots. To make sure that it isn’t DM, just return if the channel type is dm, or guild is null.
if(!message.guild) return;
//before checking "perms"
If you want the permissions for a certain guild if the message is from DM, use this code
if(message.channel.type === 'dm') {
let guild = await client.guilds.fetch('THE ID OF THE GUILD YOU WANT TO SEE THE USER’S PERMS IN')
let member = await guild.members.fetch(message.author.id);
//you can now access member.permissions
}
Keep in mind await must be in an async callback.
You did not provide any code so I cannot give any more code than this.
You can fetch the member with the guild's context.
Fetching is recommended, as Guild#member() relies on cache and is also deprecated.
Member#hasPermission() will be deprecated as well, using MemberRoleManager#has() is recommended
The following example uses async/await, ensure you're inside an async function
// Inside async function
const guild = await client.guilds.fetch('guild-id');
const member = await guild.members.fetch(message.author.id);
const hasThisPermission = member.roles.cache.has('permission');

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:

Is there a way to check if discord bot joins a server?

I'm making a discord bot and I want to check if it joins a server so I can set prefixes, send messages etc.
I tried searching for this but I didn't find anything that could help.
I thought it could be an event so tried something like:
client.on('join_guild', (guild) => {
prefix = "!"
});
But, of course, it didn't work.
Is there something like the code shown above?
I think you're looking for the guildCreate event. It gets triggered once your bot joins a new guild. Example code:
client.on("guildCreate", (guild) => {
// This event triggers when the bot joins a guild.
console.log(`Joined new guild: ${guild.name}`);
});

Resources