2016-07-09 13:27:19 +12:00
|
|
|
from discord.ext import commands
|
2017-02-13 10:39:57 +13:00
|
|
|
|
|
|
|
from . import utils
|
2016-09-29 12:39:34 +13:00
|
|
|
|
2016-07-16 02:58:10 +12:00
|
|
|
import discord
|
2016-07-17 00:27:10 +12:00
|
|
|
import re
|
2016-08-26 17:12:54 +12:00
|
|
|
import asyncio
|
2016-09-29 12:39:34 +13:00
|
|
|
import rethinkdb as r
|
2016-07-16 02:58:10 +12:00
|
|
|
|
2017-05-01 11:56:02 +12:00
|
|
|
class Moderation:
|
2016-07-09 13:59:10 +12:00
|
|
|
"""Commands that can be used by a or an admin, depending on the command"""
|
2016-07-16 09:39:26 +12:00
|
|
|
|
2016-07-09 13:27:19 +12:00
|
|
|
def __init__(self, bot):
|
|
|
|
self.bot = bot
|
2016-08-13 05:37:29 +12:00
|
|
|
|
2016-12-15 10:52:11 +13:00
|
|
|
|
|
|
|
|
2017-04-09 15:04:46 +12:00
|
|
|
@commands.command()
|
|
|
|
@commands.guild_only()
|
2017-02-13 10:39:57 +13:00
|
|
|
@utils.custom_perms(kick_members=True)
|
2017-03-06 15:45:44 +13:00
|
|
|
async def kick(self, ctx, member: discord.Member):
|
2016-11-29 17:55:55 +13:00
|
|
|
"""Used to kick a member from this server
|
|
|
|
|
|
|
|
EXAMPLE: !kick @Member
|
|
|
|
RESULT: They're kicked from the server?"""
|
2016-10-10 11:19:20 +13:00
|
|
|
try:
|
2017-03-06 15:45:44 +13:00
|
|
|
await member.kick()
|
|
|
|
await ctx.send("\N{OK HAND SIGN}")
|
2016-10-10 11:19:20 +13:00
|
|
|
except discord.Forbidden:
|
2017-03-06 15:45:44 +13:00
|
|
|
await ctx.send("But I can't, muh permissions >:c")
|
2016-10-10 11:19:20 +13:00
|
|
|
|
2017-04-09 15:04:46 +12:00
|
|
|
@commands.command()
|
|
|
|
@commands.guild_only()
|
2017-02-13 10:39:57 +13:00
|
|
|
@utils.custom_perms(ban_members=True)
|
2016-10-10 11:19:20 +13:00
|
|
|
async def unban(self, ctx, member_id: int):
|
|
|
|
"""Used to unban a member from this server
|
|
|
|
Due to the fact that I cannot find a user without being in a server with them
|
2016-11-29 17:55:55 +13:00
|
|
|
only the ID should be provided
|
|
|
|
|
|
|
|
EXAMPLE: !unban 353217589321750912
|
|
|
|
RESULT: That dude be unbanned"""
|
2016-10-10 11:19:20 +13:00
|
|
|
|
|
|
|
# Lets only accept an int for this method, in order to ensure only an ID is provided
|
|
|
|
# Due to that though, we need to ensure a string is passed as the member's ID
|
|
|
|
try:
|
2017-03-08 21:22:26 +13:00
|
|
|
await self.bot.http.unban(member_id, ctx.guild.id)
|
2017-03-06 15:45:44 +13:00
|
|
|
await ctx.send("\N{OK HAND SIGN}")
|
2016-10-10 11:19:20 +13:00
|
|
|
except discord.Forbidden:
|
2017-03-06 15:45:44 +13:00
|
|
|
await ctx.send("But I can't, muh permissions >:c")
|
2016-10-10 11:19:20 +13:00
|
|
|
except discord.HTTPException:
|
2017-03-06 15:45:44 +13:00
|
|
|
await ctx.send("Sorry, I failed to unban that user!")
|
2016-10-10 11:19:20 +13:00
|
|
|
|
2017-04-09 15:04:46 +12:00
|
|
|
@commands.command()
|
|
|
|
@commands.guild_only()
|
2017-02-13 10:39:57 +13:00
|
|
|
@utils.custom_perms(ban_members=True)
|
2016-10-10 10:56:47 +13:00
|
|
|
async def ban(self, ctx, *, member):
|
|
|
|
"""Used to ban a member
|
|
|
|
This can be used to ban someone preemptively as well.
|
2016-11-29 17:55:55 +13:00
|
|
|
Provide the ID of the user and this should ban them without them being in the server
|
|
|
|
|
|
|
|
EXAMPLE: !ban 531251325312
|
|
|
|
RESULT: That dude be banned"""
|
2016-10-10 10:56:47 +13:00
|
|
|
|
|
|
|
# Lets first check if a user ID was provided, as that will be the easiest case to ban
|
|
|
|
if member.isdigit():
|
2017-03-06 15:45:44 +13:00
|
|
|
try:
|
2017-03-08 21:22:26 +13:00
|
|
|
await self.bot.http.ban(member, ctx.guild.id)
|
2017-03-06 15:45:44 +13:00
|
|
|
await ctx.send("\N{OK HAND SIGN}")
|
|
|
|
except discord.Forbidden:
|
|
|
|
await ctx.send("But I can't, muh permissions >:c")
|
|
|
|
except discord.HTTPException:
|
|
|
|
await ctx.send("Sorry, I failed to ban that user!")
|
|
|
|
finally:
|
|
|
|
return
|
2016-10-10 10:56:47 +13:00
|
|
|
else:
|
|
|
|
# If no ID was provided, lets try to convert what was given using the internal coverter
|
2017-03-08 21:18:28 +13:00
|
|
|
converter = commands.converter.MemberConverter()
|
|
|
|
converter.prepare(ctx, member)
|
2016-10-10 10:56:47 +13:00
|
|
|
try:
|
|
|
|
member = converter.convert()
|
|
|
|
except commands.converter.BadArgument:
|
2017-03-06 15:45:44 +13:00
|
|
|
await ctx.send(
|
2016-11-29 21:05:22 +13:00
|
|
|
'{} does not appear to be a valid member. If this member is not in this server, please provide '
|
|
|
|
'their ID'.format(member))
|
2016-10-10 10:56:47 +13:00
|
|
|
return
|
|
|
|
# Now lets try actually banning the member we've been given
|
|
|
|
try:
|
2017-03-06 15:45:44 +13:00
|
|
|
await member.ban()
|
|
|
|
await ctx.send("\N{OK HAND SIGN}")
|
2016-10-10 11:19:20 +13:00
|
|
|
except discord.Forbidden:
|
2017-03-06 15:45:44 +13:00
|
|
|
await ctx.send("But I can't, muh permissions >:c")
|
2016-11-29 21:05:22 +13:00
|
|
|
except discord.HTTPException:
|
2017-03-06 15:45:44 +13:00
|
|
|
await ctx.send("Sorry, I failed to ban that user!")
|
2016-10-10 10:56:47 +13:00
|
|
|
|
2017-04-09 15:04:46 +12:00
|
|
|
@commands.command()
|
|
|
|
@commands.guild_only()
|
2017-02-13 10:39:57 +13:00
|
|
|
@utils.custom_perms(manage_messages=True)
|
2016-08-13 05:37:29 +12:00
|
|
|
async def purge(self, ctx, limit: int = 100):
|
2016-11-29 17:55:55 +13:00
|
|
|
"""This command is used to a purge a number of messages from the channel
|
|
|
|
|
|
|
|
EXAMPLE: !purge 50
|
|
|
|
RESULT: -50 messages in this channel"""
|
2017-03-08 12:56:24 +13:00
|
|
|
if not ctx.message.channel.permissions_for(ctx.message.guild.me).manage_messages:
|
2017-03-06 15:45:44 +13:00
|
|
|
await ctx.send("I do not have permission to delete messages...")
|
2016-08-28 07:36:07 +12:00
|
|
|
return
|
2017-01-22 11:58:23 +13:00
|
|
|
try:
|
2017-03-08 21:44:27 +13:00
|
|
|
await ctx.message.channel.purge(limit=limit, before=ctx.message)
|
|
|
|
await ctx.message.delete()
|
2017-01-22 11:58:23 +13:00
|
|
|
except discord.HTTPException:
|
2017-03-08 12:56:24 +13:00
|
|
|
await ctx.message.channel.send("Detected messages that are too far "
|
|
|
|
"back for me to delete; I can only bulk delete messages"
|
|
|
|
" that are under 14 days old.")
|
2016-07-31 12:20:55 +12:00
|
|
|
|
2017-04-09 15:04:46 +12:00
|
|
|
@commands.command()
|
|
|
|
@commands.guild_only()
|
2017-02-13 10:39:57 +13:00
|
|
|
@utils.custom_perms(manage_messages=True)
|
2017-04-17 10:58:05 +12:00
|
|
|
async def prune(self, ctx, *specifications):
|
2016-08-26 16:50:09 +12:00
|
|
|
"""This command can be used to prune messages from certain members
|
|
|
|
Mention any user you want to prune messages from; if no members are mentioned, the messages removed will be mine
|
2016-11-29 17:55:55 +13:00
|
|
|
If no limit is provided, then 100 will be used. This is also the max limit we can use
|
|
|
|
|
|
|
|
EXAMPLE: !prune 50
|
|
|
|
RESULT: 50 of my messages are removed from this channel"""
|
2016-08-26 16:50:09 +12:00
|
|
|
# We can only get logs from 100 messages at a time, so make sure we are not above that threshold
|
2017-04-17 10:52:38 +12:00
|
|
|
limit = 100
|
|
|
|
for x in specifications:
|
|
|
|
try:
|
2017-04-17 11:03:32 +12:00
|
|
|
limit = int(x)
|
2017-04-17 10:52:38 +12:00
|
|
|
if limit <= 100:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
limit = 100
|
|
|
|
except (TypeError, ValueError):
|
|
|
|
continue
|
2016-08-26 16:50:09 +12:00
|
|
|
|
|
|
|
# If no members are provided, assume we're trying to prune our own messages
|
|
|
|
members = ctx.message.mentions
|
2017-03-07 11:09:41 +13:00
|
|
|
roles = ctx.message.role_mentions
|
2017-03-08 11:35:30 +13:00
|
|
|
|
2016-08-26 16:50:09 +12:00
|
|
|
if len(members) == 0:
|
2017-03-06 15:45:44 +13:00
|
|
|
members = [ctx.message.guild.me]
|
2017-03-07 11:09:41 +13:00
|
|
|
|
|
|
|
# Our check for if a message should be deleted
|
|
|
|
def check(m):
|
|
|
|
if m.author in members:
|
|
|
|
return True
|
|
|
|
if any(x in m.author.roles for x in roles):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2016-08-28 07:36:07 +12:00
|
|
|
# If we're not setting the user to the bot, then we're deleting someone elses messages
|
|
|
|
# To do so, we need manage_messages permission, so check if we have that
|
2017-03-08 12:56:24 +13:00
|
|
|
if not ctx.message.channel.permissions_for(ctx.message.guild.me).manage_messages:
|
|
|
|
await ctx.send("I do not have permission to delete messages...")
|
2016-08-28 07:36:07 +12:00
|
|
|
return
|
2016-08-26 16:50:09 +12:00
|
|
|
|
|
|
|
# Since logs_from will give us any message, not just the user's we need
|
|
|
|
# We'll increment count, and stop deleting messages if we hit the limit.
|
|
|
|
count = 0
|
2017-03-08 21:44:27 +13:00
|
|
|
async for msg in ctx.message.channel.history(before=ctx.message):
|
2017-03-07 11:09:41 +13:00
|
|
|
if check(msg):
|
2016-09-18 13:30:20 +12:00
|
|
|
try:
|
2017-03-06 15:45:44 +13:00
|
|
|
await msg.delete()
|
2016-09-18 13:30:20 +12:00
|
|
|
count += 1
|
2017-03-07 11:09:41 +13:00
|
|
|
except:
|
2016-09-18 13:30:20 +12:00
|
|
|
pass
|
2016-08-26 16:50:09 +12:00
|
|
|
if count >= limit:
|
|
|
|
break
|
2017-03-08 11:35:30 +13:00
|
|
|
|
2017-03-06 15:45:44 +13:00
|
|
|
msg = await ctx.send("{} messages succesfully deleted".format(count))
|
2017-03-07 11:08:55 +13:00
|
|
|
await asyncio.sleep(5)
|
2016-09-19 09:13:41 +12:00
|
|
|
try:
|
2017-03-06 15:45:44 +13:00
|
|
|
await msg.delete()
|
2017-03-08 11:35:30 +13:00
|
|
|
await ctx.message.delete()
|
|
|
|
except:
|
2016-09-19 09:13:41 +12:00
|
|
|
pass
|
2016-08-26 16:50:09 +12:00
|
|
|
|
2016-07-31 12:20:55 +12:00
|
|
|
|
2016-07-09 13:31:18 +12:00
|
|
|
def setup(bot):
|
2017-05-01 11:56:02 +12:00
|
|
|
bot.add_cog(Moderation(bot))
|