2018-12-29 19:07:58 +00:00
|
|
|
from datetime import datetime
|
2023-04-02 11:56:49 +00:00
|
|
|
from typing import Optional
|
2023-03-09 22:01:10 +00:00
|
|
|
|
|
|
|
import discord
|
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
|
2023-03-09 22:01:10 +00:00
|
|
|
|
|
|
|
from robocop_ng.helpers.checks import check_if_staff
|
|
|
|
from robocop_ng.helpers.restrictions import add_restriction
|
|
|
|
from robocop_ng.helpers.robocronp import add_job
|
|
|
|
from robocop_ng.helpers.userlogs import userlog
|
2018-12-27 21:36:18 +00:00
|
|
|
|
|
|
|
|
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):
|
2023-04-05 10:10:18 +00:00
|
|
|
return any(r.id in self.bot.config.staff_role_ids for r in target.roles)
|
2018-12-27 21:36:18 +00:00
|
|
|
|
|
|
|
@commands.guild_only()
|
|
|
|
@commands.bot_has_permissions(ban_members=True)
|
|
|
|
@commands.check(check_if_staff)
|
|
|
|
@commands.command()
|
2020-04-20 22:05:32 +00:00
|
|
|
async def timeban(
|
2023-04-02 11:56:49 +00:00
|
|
|
self, ctx, target: Optional[discord.Member], duration: str, *, reason: str = ""
|
2020-04-20 22:05:32 +00:00
|
|
|
):
|
2018-12-29 19:07:58 +00:00
|
|
|
"""Bans a user for a specified amount of time, staff only."""
|
2023-04-02 11:56:49 +00:00
|
|
|
if target is None and ctx.message.reference is None:
|
|
|
|
return await ctx.send(
|
|
|
|
f"I'm sorry {ctx.author.mention}, I'm afraid I can't do that."
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
if target is None:
|
2023-04-05 16:57:46 +00:00
|
|
|
target = (
|
|
|
|
await ctx.channel.fetch_message(ctx.message.reference.message_id)
|
2023-04-02 11:56:49 +00:00
|
|
|
).author
|
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):
|
2020-05-17 20:44:15 +00:00
|
|
|
return await ctx.send("I can't ban this user as they're a member of staff.")
|
2018-12-27 21:36:18 +00:00
|
|
|
|
2018-12-29 19:07:58 +00:00
|
|
|
expiry_timestamp = self.bot.parse_time(duration)
|
|
|
|
expiry_datetime = datetime.utcfromtimestamp(expiry_timestamp)
|
2020-04-20 22:05:32 +00:00
|
|
|
duration_text = self.bot.get_relative_timestamp(
|
|
|
|
time_to=expiry_datetime, include_to=True, humanized=True
|
|
|
|
)
|
|
|
|
|
|
|
|
userlog(
|
2023-04-05 10:10:18 +00:00
|
|
|
self.bot,
|
2020-04-20 22:05:32 +00:00
|
|
|
target.id,
|
|
|
|
ctx.author,
|
|
|
|
f"{reason} (Timed, until " f"{duration_text})",
|
|
|
|
"bans",
|
|
|
|
target.name,
|
|
|
|
)
|
2018-12-27 21:36:18 +00:00
|
|
|
|
2020-10-02 00:03:41 +00:00
|
|
|
safe_name = await commands.clean_content(escape_markdown=True).convert(
|
|
|
|
ctx, str(target)
|
|
|
|
)
|
2018-12-27 21:36:18 +00:00
|
|
|
|
|
|
|
dm_message = f"You were banned from {ctx.guild.name}."
|
|
|
|
if reason:
|
2020-04-20 22:05:32 +00:00
|
|
|
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
|
|
|
|
|
2020-04-20 22:05:32 +00:00
|
|
|
await target.ban(
|
|
|
|
reason=f"{ctx.author}, reason: {reason}", delete_message_days=0
|
|
|
|
)
|
|
|
|
chan_message = (
|
|
|
|
f"⛔ **Timed Ban**: {ctx.author.mention} banned "
|
|
|
|
f"{target.mention} for {duration_text} | {safe_name}\n"
|
|
|
|
f"🏷 __User ID__: {target.id}\n"
|
|
|
|
)
|
2018-12-27 21:36:18 +00:00
|
|
|
if reason:
|
2020-04-20 22:05:32 +00:00
|
|
|
chan_message += f'✏️ __Reason__: "{reason}"'
|
2018-12-27 21:36:18 +00:00
|
|
|
else:
|
2020-04-20 22:05:32 +00:00
|
|
|
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."
|
|
|
|
)
|
2018-12-27 21:36:18 +00:00
|
|
|
|
2023-04-05 10:10:18 +00:00
|
|
|
add_job(self.bot, "unban", target.id, {"guild": ctx.guild.id}, expiry_timestamp)
|
2018-12-27 21:36:18 +00:00
|
|
|
|
2023-04-05 10:10:18 +00:00
|
|
|
log_channel = self.bot.get_channel(self.bot.config.log_channel)
|
2018-12-27 21:36:18 +00:00
|
|
|
await log_channel.send(chan_message)
|
2020-04-20 22:05:32 +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()
|
2020-04-20 22:05:32 +00:00
|
|
|
async def timemute(
|
2023-04-02 11:56:49 +00:00
|
|
|
self, ctx, target: Optional[discord.Member], duration: str, *, reason: str = ""
|
2020-04-20 22:05:32 +00:00
|
|
|
):
|
2018-12-29 19:07:58 +00:00
|
|
|
"""Mutes a user for a specified amount of time, staff only."""
|
2023-04-02 11:56:49 +00:00
|
|
|
if target is None and ctx.message.reference is None:
|
|
|
|
return await ctx.send(
|
|
|
|
f"I'm sorry {ctx.author.mention}, I'm afraid I can't do that."
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
if target is None:
|
2023-04-05 16:57:46 +00:00
|
|
|
target = (
|
|
|
|
await ctx.channel.fetch_message(ctx.message.reference.message_id)
|
2023-04-02 11:56:49 +00:00
|
|
|
).author
|
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):
|
2020-04-20 22:05:32 +00:00
|
|
|
return await ctx.send(
|
2020-05-17 20:44:15 +00:00
|
|
|
"I can't mute this user as they're a member of staff."
|
2020-04-20 22:05:32 +00:00
|
|
|
)
|
2018-12-27 21:36:18 +00:00
|
|
|
|
2018-12-29 19:07:58 +00:00
|
|
|
expiry_timestamp = self.bot.parse_time(duration)
|
|
|
|
expiry_datetime = datetime.utcfromtimestamp(expiry_timestamp)
|
2020-04-20 22:05:32 +00:00
|
|
|
duration_text = self.bot.get_relative_timestamp(
|
|
|
|
time_to=expiry_datetime, include_to=True, humanized=True
|
|
|
|
)
|
|
|
|
|
|
|
|
userlog(
|
2023-04-05 10:10:18 +00:00
|
|
|
self.bot,
|
2020-04-20 22:05:32 +00:00
|
|
|
target.id,
|
|
|
|
ctx.author,
|
|
|
|
f"{reason} (Timed, until " f"{duration_text})",
|
|
|
|
"mutes",
|
|
|
|
target.name,
|
|
|
|
)
|
2018-12-27 21:36:18 +00:00
|
|
|
|
2020-10-02 00:03:41 +00:00
|
|
|
safe_name = await commands.clean_content(escape_markdown=True).convert(
|
|
|
|
ctx, str(target)
|
|
|
|
)
|
2018-12-27 21:36:18 +00:00
|
|
|
|
|
|
|
dm_message = f"You were muted!"
|
|
|
|
if reason:
|
2020-04-20 22:05:32 +00:00
|
|
|
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
|
|
|
|
|
2023-04-05 10:10:18 +00:00
|
|
|
mute_role = ctx.guild.get_role(self.bot.config.mute_role)
|
2018-12-27 21:36:18 +00:00
|
|
|
|
|
|
|
await target.add_roles(mute_role, reason=str(ctx.author))
|
|
|
|
|
2020-04-20 22:05:32 +00:00
|
|
|
chan_message = (
|
|
|
|
f"🔇 **Timed Mute**: {ctx.author.mention} muted "
|
|
|
|
f"{target.mention} for {duration_text} | {safe_name}\n"
|
|
|
|
f"🏷 __User ID__: {target.id}\n"
|
|
|
|
)
|
2018-12-27 21:36:18 +00:00
|
|
|
if reason:
|
2020-04-20 22:05:32 +00:00
|
|
|
chan_message += f'✏️ __Reason__: "{reason}"'
|
2018-12-27 21:36:18 +00:00
|
|
|
else:
|
2020-04-20 22:05:32 +00:00
|
|
|
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."
|
|
|
|
)
|
2018-12-27 21:36:18 +00:00
|
|
|
|
2023-04-05 10:10:18 +00:00
|
|
|
add_job(
|
|
|
|
self.bot, "unmute", target.id, {"guild": ctx.guild.id}, expiry_timestamp
|
|
|
|
)
|
2018-12-27 21:36:18 +00:00
|
|
|
|
2023-04-05 10:10:18 +00:00
|
|
|
log_channel = self.bot.get_channel(self.bot.config.log_channel)
|
2018-12-27 21:36:18 +00:00
|
|
|
await log_channel.send(chan_message)
|
2020-04-20 22:05:32 +00:00
|
|
|
await ctx.send(
|
|
|
|
f"{target.mention} can no longer speak. " f"It will expire {duration_text}."
|
|
|
|
)
|
2023-04-05 10:10:18 +00:00
|
|
|
add_restriction(self.bot, target.id, self.bot.config.mute_role)
|
2018-12-27 21:36:18 +00:00
|
|
|
|
|
|
|
|
2022-05-24 18:35:42 +00:00
|
|
|
async def setup(bot):
|
|
|
|
await bot.add_cog(ModTimed(bot))
|