#commands.command()
#commands.guild_only()
#commands.has_guild_permissions(ban_members=True)
async def ban(self, ctx, member: discord.Member, *, reason=None):
if member.top_role >= ctx.author.top_role:
await ctx.send(f"You can only ban members below your role.")
return
channel = self.bot.get_channel(864083000211406849)
embed = discord.Embed(title=f"{ctx.author.name} banned: {member.name}", description=reason)
await channel.send(embed=embed)
await ctx.send(embed=embed)
await member.send(f"You've been banned from : `{ctx.guild}` for `reason`: {reason}.")
await ctx.guild.ban(user=member, reason=reason)
#ban.error
async def ban_error(self,ctx, error):
if isinstance(error, commands.CheckFailure):
title=f"{ctx.author.mention} you do not have the required permission(s)/role(s) to run this command!"
await ctx.send(title)
So I want my code to basically kick a member that has left my server using their ID. Ive looked in to previous questions but it seems to be no help for me.
You are converting the member input to a discord.Member, which needs to be in the server. Instead convert it to a discord.User, who does not need to be in your server.
async def ban(self, ctx, member: discord.User, *, reason=None):
Please keep in mind that your top role lookup and comparison will not work when you use the discord.User, you will have to get the actual member beforehand, like this:
server_member = ctx.guild.get_member(member.id)
if server_member != None:
if server_member.top_role >= ctx.author.top_role:
await ctx.send(f"You can only ban members below your role.")
return
Also you will get an error trying to message someone who does not share a server with your bot, or has blocked your bot etc., I would just use a try/except block for this:
try:
await member.send(f"You've been banned from : `{ctx.guild}` for `reason`: {reason}.")
except discord.errors.Forbidden:
pass
Related
none of my commands are working. the only thing functioning is the ping to respond bot event. asides from that nothing else is working. here is the code. please help me. ive import important dependencies and more but could not include it here
#
#bot.event
async def on_ready():
await bot.change_presence(status=discord.Status.online, activity=discord.Game("waiting for a ping"))
print("Bot is ready!")
#
#bot.event
async def on_member_join(member):
channel = bot.get_channel(956655371634753607)
embedVar = discord.Embed(title="Ariza Bot", color= 1974050)
embedVar.add_field(name="Welcome", value=f"{member} Thank you For joining our discord server!\nEnjoy!", inline=False)
await channel.send(embed=embedVar)
role = member.guild.get_role(956583850975297576)
await member.add_roles(role)
#
#bot.command(name='kick')
#commands.has_role("Administrator")
async def kick(self, ctx, member : commands.MemberConverter, *, reason=None):
await member.kick(reason=reason)
await ctx.send(f"{member} has been kicked, reason: {reason}")
#kick.error
async def kick_error(self, ctx, error):
if isinstance(error, MissingPermissions):
await ctx.send("You don't have permission to kick members.")
#
#bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.MissingPermissions):
await ctx.send("You cant do that!")
#
#bot.command(name='clear')
#commands.has_permissions(manage_messages=True)
async def clear(ctx, amount: int):
authors = {}
async for message in ctx.channel.history(limit=amount + 1):
if message.author not in authors:
authors[message.author] = 1
else:
authors[message.author] += 1
await message.delete()
#
#bot.command(name='warn')
#commands.has_role('Admin')
async def warn(ctx,member: discord.Member,reason: str):
channel = await member.create_dm()
embedVar = discord.Embed(title="Ariza Bot", color= 1974050)
embedVar.add_field(name="Warning", value=f"Warned: You Were Warned by {ctx.author}\nTo: {member}\n Reason: {reason}\nSuggestion: Please Don't Do this Again.", inline=False)
await channel.send(embed=embedVar)
await ctx.channel.send(embed=embedVar)
I will only talk about the kick command for the answer now. But the logic applies to the other commands as well.
Try removing the if statements in the error handlers. Do this:
#kick.error
async def kick_error(ctx, error):
await ctx.send(error)
A response is only sent if commands.MissingPermissions is raised. If some other error is raised, nothing happens. So removing the if conditions will help you know what error is happening in the command. I think it is a TypeError since member is of type discord.Member and not of commands.MemberConverter. Also mention reason is of type str, since it is declared as None which can raise TypeError.
You need add this before your commands or else it wont read them
await bot.process_commands(message)
My Goals:
When the user tried to use the command but mentioning a roles, it will sends an errors to the users.
My Current Codes:
#bot.command()
async def say(ctx, *, message: str):
try:
take_roles = ctx.guild
roles = [role.mention for role in take_roles.roles]
if message in roles:
await ctx.send(
f"{ctx.author.mention}, Hm? Trying to Abusing Me? Mention `#roles` is **NOT** Allowed on Say Command!"
)
else:
await ctx.message.delete()
await ctx.send(message)
You were trying to find the whole message in roles. A better idea is to look for role mentions inside of the message.
#bot.command()
async def say(ctx, *, message: str):
for role in ctx.guild.roles:
if role.mention in message:
await ctx.send(f"{ctx.author.mention}, Hm? Trying to Abusing Me? Mention `#roles` is **NOT** Allowed on Say Command!")
return # stops function if the role mention is in message
# if it didn't match any roles:
await ctx.message.delete()
await ctx.send(message)
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}.')
Mute code below. If a mod doesnt specify what member to mute, how can the bot tell them that? Thanks!
#bot.command()
#commands.has_permissions(manage_roles=True)
async def mute(ctx, member: discord.Member, reason=None):
if member.id == ctx.author.id:
await ctx.send(f"{ctx.author.mention}, YOU HAVE BEEN STOPPED BY ME YAY, you can't mute yourself!")
return
if discord.Member == None:
await ctx.send(f"{ctx.author.mention}, HEY you nugget, you need to specify who you want to mute!")
return
role = discord.utils.get(ctx.guild.roles, name='muted')
if role in ctx.guild.roles:
await member.add_roles(role)
embed=discord.Embed(title="Muted", description=f"Offender: {member.mention} has been muted until further notice!", color=0x00FFFF)
embed.add_field(name="Reason", value=f'{reason}', inline=True)
embed.add_field(name="Moderator", value=f'{ctx.author.name}', inline=True)
await ctx.send(embed=embed)
else:
await ctx.send(f"{ctx.author.mention}, Make sure your muted role is called `muted` in all lowercase! Then try again.")
Your thought was right, but you need to approach it a little differently.
Take a look at the following code:
#bot.command()
#commands.has_permissions(manage_roles=True)
async def mute(ctx, member: discord.Member, reason=None):
if member.id == ctx.message.author.id:
await ctx.send("You can't mute yourself")
if member:
# Do what you want if the member was given
What we only did is work with if statements.
If you want you can also use return await so the bot will no longer proceed.
You can also build in an error handler for different things. Have a look at the following code:
#mute.error
async def mute_error(ctx, error):
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send("No member given")
You have to put the code under the actual mute command code.
I'm pretty new to discord bots so i wondered if someone here could help me, I wrote a code for kicking and i want to add something that DM's the person before he gets kicked but after the !kick command was given.
#bot.command()
async def kick(ctx, member: discord.Member=None):
if not member:
await ctx.send('Please mention a member')
return
await member.kick()
await ctx.send(f'{member.display_name}\'s was kicked from the server')
You just need to create a dm channel and send the message.
Try adding the two lines below:
#bot.command()
async def kick(ctx, member: discord.Member = None):
if member is None:
await ctx.send(f'{ctx.author.mention} Please mention a member')
return
channel = await member.create_dm() # line 1 create channel
await channel.send('You are getting kicked') # line 2 send the message
await member.kick()
await ctx.send(f'{member.display_name}\'s was kicked from the server')