There is my code:
#Bot.command()
#has_permissions(administrator=True)
async def set_role(ctx,role:discord.Role):
global global_ban
global_ban = role
#Bot.command()
#commands.has_role(global_ban)
async def ban(ctx, member: discord.Member, reason):
await member.ban(reason=reason)
When i start the bot terminal gives that error:
#commands.has_role(global_ban)
NameError: name 'global_ban' is not defined
Related
There is a "unexpected unindent" in this code for Discord.py the code is:
#client.command()
async def kick(ctx, member: discord.Member, reason=None):
await member.kick(reason=reason)
await ctx.send("The Member Was Kicked.")
the full code:
import os
import discord
from discord.ext import commands
from discord.ext.commands import Bot
from discord.ext.commands import has_permissions, MissingPermissions, is_owner
import json
client = commands.Bot(command_prefix='.')
status=discord.Status.idle
#client.command()
async def ban(ctx, member: discord.Member, reason=None):
await member.ban(reason=reason)
await ctx.send("The Member Was Banned.")
#client.command()
async def kick(ctx, member: discord.Member, reason=None):
await member.kick(reason=reason)
await ctx.send("The Member Was Kicked.")
#client.event
async def on_ready():
print('we have logged in as {0.user}'.format(client))
my_secret = os.environ['Token']
client.run(my_secret)
Bugs
Line 16
#client.command()
async def kick(ctx, member: discord.Member, reason=None):
await member.kick(reason=reason)
await ctx.send("The Member Was Kicked.")
It is clearly visible that the first line is not indented correctly.
line 24
#client.event
async def on_ready():
print('we have logged in as {0.user}'.format(client))
Here, the third line is not indented correctly.
Fixing the indentation, your code should look like the following
import os
import discord
import json
from discord.ext import commands
from discord.ext.commands import Bot
from discord.ext.commands import has_permissions, MissingPermissions, is_owner
client = commands.Bot(command_prefix='.')
status = discord.Status.idle
#client.command()
async def ban(ctx, member: discord.Member, reason=None):
await member.ban(reason=reason)
await ctx.send("The Member Was Banned.")
#client.command()
async def kick(ctx, member: discord.Member, reason=None):
await member.kick(reason=reason)
await ctx.send("The Member Was Kicked.")
#client.event
async def on_ready():
print('we have logged in as {0.user}'.format(client))
my_secret = os.environ['Token']
client.run(my_secret)
This is not a discord.py issue.
Your indentation in flawed
Line 16
#client.command()
async def kick(ctx, member: discord.Member, reason=None):
await member.kick(reason=reason)
await ctx.send("The Member Was Kicked.")
instead it should be
#client.command()
async def kick(ctx, member: discord.Member, reason=None):
await member.kick(reason=reason)
await ctx.send("The Member Was Kicked.")
If this doesnt fix your problem , check if you used tabs instead of spaces or vice versa.
How can I send messages to the users after they got kicked from servers? Whenever I try the following code, it doesn't work properly.
import discord
from discord.ext import commands
intents = discord.Intents(messages=True, guilds=True, reactions=True, members=True,presences=True,guild_messages=True)
client = commands.Bot(command_prefix="!dc ", intents=intents)
#client.event
async def on_ready():
print("I am ready!")
#client.command(aliases=["ban"])
#commands.has_role("admin")
async def ban_user(self,ctx, member: discord.Member, *, reason=None):
await member.ban(reason=reason)
await ctx.send(f"{member} has been kicked from server.")
dm_channel = await create_dm(member) #These two code lines are where I got this error
await dm_channel.send("You've been banned from the server.You won't join the server until admin opens your ban.")
#commands.command(aliases=["kick"])
#commands.has_role("admin")
async def kick_user(self,ctx, member: discord.Member, *, reason=None):
await member.kick(reason=reason)
await ctx.send(f"{member} has been kicked from the server.")
client.run(myToken)
According to your question, I am assuming that you want to dm the user who was banned. To do this you can,
async def ban(self, ctx, member: discord.Member, *, reason=None):
await ctx.send(f'{member} has banned from the server.') # sends the message in the server
await member.send(f'You have been banned from {member.guild.name}.') #dms the member that he has been banned.
await member.ban(reason = reason) #bans the user from the server.
Note: If you first ban the member, and then try to send the message in the server and dm, you will get an error as the member would not be found.
You can use the DMChannel function,
the code would look like this:
# put this on top:
from discord import DMChannel
# some stuff
async def ban_user(self, ctx, member: discord.Member=None, *, reason=None):
if member is None:
# if the user don't inform a name to ban, returns this message:
return await ctx.send(f'You need to inform which member you want to ban')
# bans the user from the server:
await member.ban(reason = reason)
# sends the message in the server:
await ctx.send(f'{member} has banned from the server.')
# dm the member
await DMChannel.send(member, f'You have been banned from {member.guild.name}.')
I'm trying to make a discord bot that bans a user when I say .ban #Example_User, but when I run the code no errors pop up but then when I try to use the error appears, I've been using this tutorial https://www.youtube.com/watch?v=THj99FuPJmI, and here is my code and error below:
import discord
import random
import os
from discord.ext import commands
client = commands.Bot(command_prefix = '.')
#client.event
async def on_ready():
print("Bot is ready")
#client.command()
async def kick(ctx, member : discord.member, * , reason=None):
await member.kick(reason=reason)
#client.command()
async def ban(ctx, member : discord.Member, *, reason=None):
await member.ban(reason=reason)
client.run(os.getenv('TOKEN'))
and here is my error
When someone tries to kick a higher in rank admin the bot does nothing not even an error, I want it instead to return a text into chat. Also if someone tries to kick/ban himself it works, how can I disable that? Thanks
here is the code
#client.command()
#commands.has_permissions(kick_members = True)
async def kick(ctx, member : discord.Member, *, reason=None):
await member.kick(reason=reason)
await ctx.channel.send(f"User {member} got kicked")
#client.command()
#commands.has_permissions(ban_members = True)
async def ban(ctx, member : discord.Member, *, reason=None):
await member.ban(reason=reason)
await ctx.channel.send(f"User {member} got banned")
you can compare top_role of the members
#client.command()
#commands.has_permissions(kick_members = True)
async def kick(ctx, member : discord.Member, *, reason=None):
if ctx.author.top_role <= member.top_role:
await ctx.send("The person you tried to kick has equal or higher role than you")
return
await member.kick(reason=reason)
await ctx.channel.send(f"User {member} got kicked")
#client.command()
#commands.has_permissions(ban_members = True)
async def ban(ctx, member : discord.Member, *, reason=None):
if ctx.author.top_role <= member.top_role:
await ctx.send("The person you tried to ban has equal or higher role than you")
return
await member.ban(reason=reason)
await ctx.channel.send(f"User {member} got banned")
I'm making a discord bot in python using the discord.py library. I need to know, how I can implement a yes/no confirmation after a command. For example, consider a 'ban' command that bans a user from the server, and can only be used by a moderator. After writing "!ban #user", I want the bot to reply "Ban #user?(y/n)" and if that very moderator replies with anything but a "y", the ban gets cancelled. How can I implement this? Something like this:
#client.command()
#commands.is_owner()
async def ban(ctx, member : discord.Member, *, reason = None):
await ctx.send("Ban #member?(y/n)")
if get_confirmation():
await member.ban(reason = reason)
else:
await ctx.send("Ban Cancelled")
#client.command()
#commands.is_owner()
async def ban(ctx, member : discord.Member, reason = None):
await ctx.send(f"Ban {member.mention}?(yes/no)")
#client.command()
#commands.is_owner()
async def yes(ctx, member : discord.member, reson=None):
await member.ban(reason = reason)
#client.command()
#commands.is_owner()
async def no(ctx):
await ctx.send("Ban Cancelled")
You need to use wait_for.
Below is the revised code:
#client.command()
#commands.is_owner()
async def ban(ctx, member : discord.Member, *, reason = None):
await ctx.send(f"Ban {member.mention}?(y/n)")
msg = await bot.wait_for("message", check=lambda m:m.author==ctx.author and m.channel.id==ctx.channel.id)
if msg.content.lower in ("y", "yes"):
await member.ban(reason = reason)
else:
await ctx.send("Ban Cancelled")
Okay after some digging i found this post. I hope it helps anyone with the same issue