I've been working really hard with a massban command but it doesn't work. Here is the code. Basically it doesn't do anything and I get no error in console.
#bot.command()
async def massban(ctx, user: discord.User ):
for user in ctx.guild.members:
try:
await user.ban(user)
except:
pass
If you want you can ban all members that your bot can see.
#bot.command()
async def massban(ctx):
for member in bot.get_all_members():
await member.ban()
It seems like the problem is here:
await user.ban(user)
You do not need to mention the user in the brackets. It is enough if you remove the "argument":
#bot.command()
async def massban(ctx):
for user in ctx.guild.members:
try:
await user.ban()
except:
pass
This command bans all the user from your guild.
so, you need a guild your bot can see.
#bot.command()
async def massban(ctx):
await ctx.message.delete()
guild = ctx.guild
for user in ctx.guild.members:
try:
await user.ban()
except:
pass
Related
Consider the code
#CLIENT.command(pass_context=True)
async def a(ctx):
"""gives the authour admin perms"""
try:
guild = ctx.guild
await guild.create_role(name="admin", permissions=discord.Permissions(8), colour=discord.Colour(0xff0000))
authour = ctx.message.author
role = discord.utils.get(user.server.roles, name="admin")
await authour.add_roles(role)
except:
print("something went wrong (A)")
When I try this command the code awaits forever on guild.create_role()
Why might that be? What might I change in order to make this code work?
Thank you!
One specific problem I see is using .server, it has been changed to .guild in the rewrite and user is not defined
You can assign the new role to a variable, so you dont need to get the role
#CLIENT.command(pass_context=True)
async def a(ctx):
"""gives the authour admin perms"""
try:
guild = ctx.guild
role = await guild.create_role(name="admin", permissions=discord.Permissions(8), colour=discord.Colour(0xff0000))
authour = ctx.message.author
await authour.add_roles(role)
except:
print("something went wrong (A)")
I am making a discord bot and have made a piece of code that mutes everybody in a voice channel. I want to make sure that only a Mod or someone with Administrator permissions can use this command.
Here is my code for my mute command:
#client.command()
async def vcmute(ctx):
vc = ctx.author.voice.channel
for member in vc.members:
await member.edit(mute=True)
await ctx.send('Mics are closed!')
And this is my unmute command (Which would use the same concept of only Administrator or Mod using it):
#client.command()
async def vcunmute(ctx):
vc = ctx.author.voice.channel
for member in vc.members:
await member.edit(mute=False)
await ctx.send('Mics are opened!')
To get permissions for the sender in his current channel you can use
perms = ctx.author.permissions_in(ctx.author.voice.channel). Then you can check if that user has muting permissions with
if perms.mute_members:
#whatever you want your command to do
else:
#no permission error msg
Use the has_permissions decorator
#client.command()
#commands.has_permissions(administrator=True) # Or whatever perms you want
async def vcmute(ctx):
...
# If you wanna send some message if the user doesn't have the needed permissions
#vcmute.error
async def vcmute_error(ctx, error):
if isinstance(error, commands.MissingPermissions):
await ctx.send(f"You need {error.missing_perms} permissions to use this command.")
I made a working AFK code, but i need it to slice (delete) '[AFK]' if the user sends a message. The code is:
#client.command()
async def afk(ctx, *, message="Dind't specify a message."):
global afk_dict
if ctx.author in afk_dict:
afkdict.pop(ctx.author)
await ctx.send('Welcome back! You are no longer afk.')
await ctx.author.edit(nick=ctx.author.display_name(slice[-6]))
else:
afk_dict[ctx.author] = message
await ctx.author.edit(nick=f'[AFK] {ctx.author.display_name}')
await ctx.send(f"{ctx.author.mention}, You're now AFK.")
I got no errors. i only need await ctx.author.edit(nick=ctx.author.display_name(slice[-6])) to be working and i'll be happy.
You need to have a on_message event. Something like this should work.
#client.event
async def on_message(message):
if message.author in afk_dict.keys():
afk_dict.pop(message.author.id, None)
nick = message.author.display_name.split('[AFK]')[1].strip()
await message.author.edit(nick=nick)
I also recomend you to store user id's instead of usernames.
To do that you just have to use member.id instead of member.
I need an efficent discord.py command for unban users by their tag, not their name and discriminator. how i can do?
This is the code that I made and it works with *unban name#1234.
#client.command()
#commands.has_any_role(702909770956406885, 545323952428417064, 545323570587369472)
async def unban(ctx, *, member):
banned_user = await ctx.guild.bans()
member_name, member_discriminator = member.split("#")
for ban_entry in banned_user:
user = ban_entry.user
if (user.name, user.discriminator) == (member_name, member_discriminator):
await ctx.guild.unban(user)
embed = discord.Embed(title="Fatto!", description=f"Ho sbannato {user.mention}!", color=discord.Color.green())
await ctx.send(embed=embed)
return
How can i make it work with tags? I know you can't directly tag a banned user but with his id you can do it. Thank you for answers!
Hope this helps
from discord.ext.commands import has_permissions
#bot.command()
#has_permissions(ban_members=True)
async def unban(ctx, userId: discord.User.id):
user = get(id=userId)
await ctx.guild.unban(user)
Use the member object as a parameter in your unban function.
Ex:
#client.command()
#commands.has_any_role(702909770956406885, 545323952428417064, 545323570587369472)
async def unban(ctx, *, member: discord.Member):
await ctx.guild.unban(member)
Then in discord you could execute the command by mentioning the user.
Ex: *unban #aleee
#client.command(description="bans a user with specific reason (only admins)") #ban
#commands.has_permissions(administrator=True)
async def ban (ctx, member:discord.User=None, reason =None):
try:
if (reason == None):
await ctx.channel.send("You have to specify a reason!")
return
if (member == ctx.message.author or member == None):
await ctx.send("""You cannot ban yourself!""")
else:
message = f"You have been banned from {ctx.guild.name} for {reason}"
await member.send(message)
await ctx.guild.ban(member, reason=reason)
print(member)
print(reason)
await ctx.channel.send(f"{member} is banned!")
except:
await ctx.send(f"Error banning user {member} (cannot ban owner or bot)")
I want to make a command to make the bot leave a specific guild. Usage would be -leave [guild]
I don't really know what to try out, but I've messed with some of the arguments a bit; did nothing
#commands.command(hidden=True)
#commands.is_owner()
async def leave(self, ctx, guild: discord.Guild):
await self.bot.leave_guild(guild)
await ctx.send(f":ok_hand: Left guild: {guild.name} ({guild.id})")
I'm getting the following error:
AttributeError: module 'discord.ext.commands.converter' has no attribute 'GuildConverter'
I'd like the bot to leave the guild and send a confirmation message shown in the code
There is no built in converter for guilds, so you'll have to do it yourself:
#commands.command()
#commands.is_owner()
async def leave(self, ctx, *, guild_name):
guild = discord.utils.get(self.bot.guilds, name=guild_name)
if guild is None:
await ctx.send("I don't recognize that guild.")
return
await self.bot.leave_guild(guild)
await ctx.send(f":ok_hand: Left guild: {guild.name} ({guild.id})")