2018-12-27 21:36:18 +00:00
|
|
|
import discord
|
|
|
|
import config
|
2018-12-29 19:07:58 +00:00
|
|
|
from datetime import datetime
|
2018-12-27 21:36:18 +00:00
|
|
|
from discord.ext import commands
|
2019-02-28 22:10:30 +00:00
|
|
|
from discord.ext.commands import Cog
|
2018-12-27 21:36:18 +00:00
|
|
|
from helpers.checks import check_if_staff
|
|
|
|
from helpers.robocronp import add_job
|
|
|
|
from helpers.userlogs import userlog
|
|
|
|
from helpers.restrictions import add_restriction
|
|
|
|
|
|
|
|
|
2019-02-28 22:10:30 +00:00
|
|
|
class ModTimed(Cog):
|
2018-12-27 21:36:18 +00:00
|
|
|
def __init__(self, bot):
|
|
|
|
self.bot = bot
|
|
|
|
|
|
|
|
def check_if_target_is_staff(self, target):
|
|
|
|
return any(r.id in config.staff_role_ids for r in target.roles)
|
|
|
|
|
|
|
|
@commands.guild_only()
|
|
|
|
@commands.bot_has_permissions(ban_members=True)
|
|
|
|
@commands.check(check_if_staff)
|
|
|
|
@commands.command()
|
|
|
|
async def timeban(self, ctx, target: discord.Member,
|
2018-12-29 19:07:58 +00:00
|
|
|
duration: str, *, reason: str = ""):
|
|
|
|
"""Bans a user for a specified amount of time, staff only."""
|
2018-12-27 21:36:18 +00:00
|
|
|
# Hedge-proofing the code
|
|
|
|
if target == ctx.author:
|
|
|
|
return await ctx.send("You can't do mod actions on yourself.")
|
|
|
|
elif self.check_if_target_is_staff(target):
|
|
|
|
return await ctx.send("I can't ban this user as "
|
|
|
|
"they're a member of staff.")
|
|
|
|
|
2018-12-29 19:07:58 +00:00
|
|
|
expiry_timestamp = self.bot.parse_time(duration)
|
|
|
|
expiry_datetime = datetime.utcfromtimestamp(expiry_timestamp)
|
|
|
|
duration_text = self.bot.get_relative_timestamp(time_to=expiry_datetime,
|
|
|
|
include_to=True,
|
|
|
|
humanized=True)
|
|
|
|
|
|
|
|
userlog(target.id, ctx.author, f"{reason} (Timed, until "
|
|
|
|
f"{duration_text})",
|
2018-12-27 21:36:18 +00:00
|
|
|
"bans", target.name)
|
|
|
|
|
2019-01-07 08:49:19 +00:00
|
|
|
safe_name = await commands.clean_content().convert(ctx, str(target))
|
2018-12-27 21:36:18 +00:00
|
|
|
|
|
|
|
dm_message = f"You were banned from {ctx.guild.name}."
|
|
|
|
if reason:
|
|
|
|
dm_message += f" The given reason is: \"{reason}\"."
|
2018-12-29 19:07:58 +00:00
|
|
|
dm_message += f"\n\nThis ban will expire {duration_text}."
|
2018-12-27 21:36:18 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
await target.send(dm_message)
|
|
|
|
except discord.errors.Forbidden:
|
|
|
|
# Prevents ban issues in cases where user blocked bot
|
|
|
|
# or has DMs disabled
|
|
|
|
pass
|
|
|
|
|
|
|
|
await target.ban(reason=f"{ctx.author}, reason: {reason}",
|
|
|
|
delete_message_days=0)
|
|
|
|
chan_message = f"⛔ **Timed Ban**: {ctx.author.mention} banned "\
|
2018-12-29 19:07:58 +00:00
|
|
|
f"{target.mention} for {duration_text} | {safe_name}\n"\
|
2018-12-27 21:36:18 +00:00
|
|
|
f"🏷 __User ID__: {target.id}\n"
|
|
|
|
if reason:
|
|
|
|
chan_message += f"✏️ __Reason__: \"{reason}\""
|
|
|
|
else:
|
|
|
|
chan_message += "Please add an explanation below. In the future"\
|
|
|
|
", it is recommended to use `.ban <user> [reason]`"\
|
|
|
|
" as the reason is automatically sent to the user."
|
|
|
|
|
|
|
|
add_job("unban", target.id, {"guild": ctx.guild.id}, expiry_timestamp)
|
|
|
|
|
|
|
|
log_channel = self.bot.get_channel(config.log_channel)
|
|
|
|
await log_channel.send(chan_message)
|
2018-12-29 19:07:58 +00:00
|
|
|
await ctx.send(f"{safe_name} is now b&. "
|
|
|
|
f"It will expire {duration_text}. 👍")
|
2018-12-27 21:36:18 +00:00
|
|
|
|
|
|
|
@commands.guild_only()
|
|
|
|
@commands.check(check_if_staff)
|
|
|
|
@commands.command()
|
|
|
|
async def timemute(self, ctx, target: discord.Member,
|
2018-12-29 19:07:58 +00:00
|
|
|
duration: str, *, reason: str = ""):
|
|
|
|
"""Mutes a user for a specified amount of time, staff only."""
|
2018-12-27 21:36:18 +00:00
|
|
|
# Hedge-proofing the code
|
|
|
|
if target == ctx.author:
|
|
|
|
return await ctx.send("You can't do mod actions on yourself.")
|
|
|
|
elif self.check_if_target_is_staff(target):
|
|
|
|
return await ctx.send("I can't mute this user as "
|
|
|
|
"they're a member of staff.")
|
|
|
|
|
2018-12-29 19:07:58 +00:00
|
|
|
expiry_timestamp = self.bot.parse_time(duration)
|
|
|
|
expiry_datetime = datetime.utcfromtimestamp(expiry_timestamp)
|
|
|
|
duration_text = self.bot.get_relative_timestamp(time_to=expiry_datetime,
|
|
|
|
include_to=True,
|
|
|
|
humanized=True)
|
|
|
|
|
|
|
|
userlog(target.id, ctx.author, f"{reason} (Timed, until "
|
|
|
|
f"{duration_text})",
|
2018-12-27 21:36:18 +00:00
|
|
|
"mutes", target.name)
|
|
|
|
|
2019-01-07 08:49:19 +00:00
|
|
|
safe_name = await commands.clean_content().convert(ctx, str(target))
|
2018-12-27 21:36:18 +00:00
|
|
|
|
|
|
|
dm_message = f"You were muted!"
|
|
|
|
if reason:
|
|
|
|
dm_message += f" The given reason is: \"{reason}\"."
|
2018-12-29 19:07:58 +00:00
|
|
|
dm_message += f"\n\nThis mute will expire {duration_text}."
|
2018-12-27 21:36:18 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
await target.send(dm_message)
|
|
|
|
except discord.errors.Forbidden:
|
|
|
|
# Prevents kick issues in cases where user blocked bot
|
|
|
|
# or has DMs disabled
|
|
|
|
pass
|
|
|
|
|
|
|
|
mute_role = ctx.guild.get_role(config.mute_role)
|
|
|
|
|
|
|
|
await target.add_roles(mute_role, reason=str(ctx.author))
|
|
|
|
|
|
|
|
chan_message = f"🔇 **Timed Mute**: {ctx.author.mention} muted "\
|
2018-12-29 19:07:58 +00:00
|
|
|
f"{target.mention} for {duration_text} | {safe_name}\n"\
|
2018-12-27 21:36:18 +00:00
|
|
|
f"🏷 __User ID__: {target.id}\n"
|
|
|
|
if reason:
|
|
|
|
chan_message += f"✏️ __Reason__: \"{reason}\""
|
|
|
|
else:
|
|
|
|
chan_message += "Please add an explanation below. In the future, "\
|
|
|
|
"it is recommended to use `.mute <user> [reason]`"\
|
|
|
|
" as the reason is automatically sent to the user."
|
|
|
|
|
|
|
|
add_job("unmute", target.id, {"guild": ctx.guild.id}, expiry_timestamp)
|
|
|
|
|
|
|
|
log_channel = self.bot.get_channel(config.log_channel)
|
|
|
|
await log_channel.send(chan_message)
|
2018-12-29 19:07:58 +00:00
|
|
|
await ctx.send(f"{target.mention} can no longer speak. "
|
|
|
|
f"It will expire {duration_text}.")
|
2018-12-27 21:36:18 +00:00
|
|
|
add_restriction(target.id, config.mute_role)
|
|
|
|
|
|
|
|
|
|
|
|
def setup(bot):
|
|
|
|
bot.add_cog(ModTimed(bot))
|