unexpected unindent (Discord.py) - python

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.

Related

Why is this happening with discord.ext and replit?

I am trying to run discord.py but it is not working. This is the error
/home/runner/Mafia-Bot/venv/lib/python3.8/site-packages/nextcord/health_check.py:23: DistributionWarning: discord.py is installed which is incompatible with nextcord. Please remove this library by using `pip3 uninstall discord.py`
warn(message, DistributionWarning, stacklevel=0)
/home/runner/Mafia-Bot/venv/lib/python3.8/site-packages/nextcord/health_check.py:23: DistributionWarning: discord is installed which is incompatible with nextcord. Please remove this library by using `pip3 uninstall discord`
warn(message, DistributionWarning, stacklevel=0)
Traceback (most recent call last):
File "main.py", line 31, in <module>
async def Hello(ctx):
File "/home/runner/Mafia-Bot/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 1262, in decorator
result = command(*args, **kwargs)(func)
File "/home/runner/Mafia-Bot/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 1432, in decorator
raise TypeError('Callback is already a command.')
TypeError: Callback is already a command.
It is for a video game but is not connected.
Code:
import discord
from discord.ext import commands
import os
from discord import Member
from discord.ext.commands import MissingPermissions
from discord.ext.commands import has_permissions
intents = discord.Intents.default()
intents.members=True
client = commands.Bot(command_prefix="~$", intents=intents)
#######Events######
#client.event
async def on_ready():
print("Bot is up and ready!")
await client.change_presence(activity=discord.Game('ERLC'))
#client.event
async def on_member_join(member):
await member.send("Welcome to the mafia!")
######Commands#######
#client.command(pass_context=True)
#has_permissions(manage_roles=True)
#client.command()
async def Hello(ctx):
await ctx.send('Hi')
#client.command()
#has_permissions(kick_members=True)
async def kick(ctx, member: discord.Member, *, reason=None):
await member.kick(reason=reason)
await ctx.send(f"User {member} has been kicked.")
#kick.error
async def kick_error(ctx, error):
if isinstance(error, commands.MissingPermissions):
await ctx.send("You do not have permissions to kick.")
#client.command()
#has_permissions(ban_members=True)
async def ban(ctx, member: discord.Member, *, reason=None):
await member.send("You have been banned")
await member.ban(reason=reason)
await ctx.send(f"User {member} has been banned.")
#ban.error
async def ban_error(ctx, error):
if isinstance(error, commands.MissingPermissions):
await ctx.send("You do not have permissions to ban.")
client.run(os.getenv("TOKEN"))
Its probably easy to fix but I can't, I have only been coding for a year. I have to add alot of details but thats all I can say.

name 'create_dm' is not defined

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}.')

How i can set banner role with user input?

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

How do I fix this discord ban bot using python

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

How can I make the bot say something when someone tries to kick a higher admin or himself?

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")

Categories

Resources