Skip to content

Commit

Permalink
even more wipppppp zzzzzzzz
Browse files Browse the repository at this point in the history
  • Loading branch information
Snaacky committed Dec 24, 2024
1 parent d133d93 commit 7a4a47e
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 119 deletions.
11 changes: 5 additions & 6 deletions chiya/cogs/ban.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import time

import arrow
import discord
from discord import app_commands
from discord.ext import commands
Expand Down Expand Up @@ -49,13 +48,13 @@ async def ban(
await ctx.response.defer(thinking=True, ephemeral=True)

if not can_action_member(ctx=ctx, member=user):
return await embeds.error_message(ctx=ctx, description=f"You cannot action {user.mention}.")
return await embeds.send_error(ctx=ctx, description=f"You cannot action {user.mention}.")

if await self.is_user_banned(ctx=ctx, user=user):
return await embeds.error_message(ctx=ctx, description=f"{user.mention} is already banned.")
return await embeds.send_error(ctx=ctx, description=f"{user.mention} is already banned.")

if len(reason) > 1024:
return await embeds.error_message(ctx=ctx, description="Reason must be less than 1024 characters.")
return await embeds.send_error(ctx=ctx, description="Reason must be less than 1024 characters.")

mod_embed = embeds.make_embed(
ctx=ctx,
Expand Down Expand Up @@ -121,10 +120,10 @@ async def unban(self, ctx: discord.Interaction, user: discord.Member | discord.U
await ctx.response.defer(thinking=True, ephemeral=True)

if not await self.is_user_banned(ctx=ctx, user=user):
return await embeds.error_message(ctx=ctx, description=f"{user.mention} is not banned.")
return await embeds.send_error(ctx=ctx, description=f"{user.mention} is not banned.")

if len(reason) > 1024:
return await embeds.error_message(ctx=ctx, description="Reason must be less than 1024 characters.")
return await embeds.send_error(ctx=ctx, description="Reason must be less than 1024 characters.")

mod_embed = embeds.make_embed(
ctx=ctx,
Expand Down
4 changes: 2 additions & 2 deletions chiya/cogs/developer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def _cleanup_code(self, content: str) -> str:
await ctx.response.defer(thinking=True, ephemeral=True)

if not await self.bot.is_owner(ctx.user):
return await embeds.error_message(ctx=ctx, description="You do not own this bot.")
return await embeds.send_error(ctx=ctx, description="You do not own this bot.")

# Required environment variables.
env = {
Expand Down Expand Up @@ -135,7 +135,7 @@ async def ping(self, ctx: discord.Interaction):
async def console(self, ctx: discord.Interaction, lines: int):
await ctx.response.defer(thinking=True, ephemeral=True)
if lines >= 500:
return await embeds.error_message(ctx=ctx, description="Please specify <= 500 lines max.")
return await embeds.send_error(ctx=ctx, description="Please specify <= 500 lines max.")
with open(os.path.join("logs", "bot.log")) as f:
lines = f.readlines()[-lines:]
with io.StringIO() as file:
Expand Down
6 changes: 3 additions & 3 deletions chiya/cogs/highlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ async def list_highlights(self, ctx: discord.Interaction) -> None:
await ctx.response.defer(thinking=True, ephemeral=True)

if not (results := HighlightUser.query.filter_by(user_id=ctx.user.id)):
return await embeds.error_message(ctx=ctx, description="You are not tracking any terms.")
return await embeds.send_error(ctx=ctx, description="You are not tracking any terms.")

embed = embeds.make_embed(
ctx=ctx,
Expand All @@ -157,7 +157,7 @@ async def remove_highlight(

result = Highlight.query.filter(Highlight.users.ilike(f"%{ctx.user.id}%")).all()
if not result:
return await embeds.error_message(ctx=ctx, description="You are not tracking that term.")
return await embeds.send_error(ctx=ctx, description="You are not tracking that term.")

users = orjson.loads(result.users)
users.remove(ctx.user.id)
Expand Down Expand Up @@ -185,7 +185,7 @@ async def clear_highlights(self, ctx: discord.Interaction) -> None:
results = Highlight.query.filter(Highlight.users.ilike(f"%{ctx.user.id}%")).all()

if not results:
return await embeds.error_message(ctx=ctx, description="You are not tracking any terms.")
return await embeds.send_error(ctx=ctx, description="You are not tracking any terms.")

for result in results:
users = orjson.loads(result.users)
Expand Down
4 changes: 2 additions & 2 deletions chiya/cogs/move_question.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ async def move_question(self, ctx: discord.Interaction, message: discord.Message

staff = [x for x in ctx.user.roles if x.id == config.roles.staff or x.id == config.roles.trial]
if not staff:
return await embeds.error_message(ctx=ctx, description="You do not have permissions to use this command.")
return await embeds.send_error(ctx=ctx, description="You do not have permissions to use this command.")

if ctx.channel.category_id in [
config.categories.moderation,
config.categories.development,
config.categories.logs,
config.categories.tickets,
]:
return await embeds.error_message(
return await embeds.send_error(
ctx=ctx,
description="You do not have permissions to use this command in this category.",
)
Expand Down
76 changes: 39 additions & 37 deletions chiya/cogs/mute.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import time

import arrow
import discord
from discord import app_commands
from discord.ext import commands
from loguru import logger
from parsedatetime import Calendar

from chiya.config import config
from chiya.database import ModLog
from chiya.utils import embeds
from chiya.utils.helpers import can_action_member, get_duration, log_embed_to_channel
from chiya.utils.helpers import can_action_member, log_embed_to_channel


class MuteCog(commands.Cog):
Expand Down Expand Up @@ -40,49 +40,49 @@ async def mute(
await ctx.response.defer(thinking=True, ephemeral=True)

if not isinstance(member, discord.Member):
return await embeds.error_message(ctx=ctx, description="That user is not in the server.")
return await embeds.send_error(ctx=ctx, description="That user is not in the server.")

if not can_action_member(ctx=ctx, member=member):
return await embeds.error_message(ctx=ctx, description=f"You cannot action {member.mention}.")
return await embeds.send_error(ctx=ctx, description=f"You cannot action {member.mention}.")

if member.is_timed_out():
return await embeds.error_message(ctx=ctx, description=f"{member.mention} is already muted.")
return await embeds.send_error(ctx=ctx, description=f"{member.mention} is already muted.")

if len(reason) > 1024:
return await embeds.error_message(ctx=ctx, description="Reason must be less than 1024 characters.")

duration_string, mute_end_time = get_duration(duration=duration)
if not duration_string:
return await embeds.error_message(
ctx=ctx,
description=(
"Duration syntax: `y#mo#w#d#h#m#s` (year, month, week, day, hour, min, sec)\n"
"You can specify up to all seven but you only need one."
),
)
return await embeds.send_error(ctx=ctx, description="Reason must be less than 1024 characters.")

struct, status = Calendar().parse(duration)
if not status:
return await embeds.send_error(ctx=ctx, description="Unable to decipher duration, please try again")

time_delta = mute_end_time.delta(arrow.utcnow()).total_seconds()
# TODO: Apparently arrow expects struct to be UTC but it's based on the local time instead which throws it off
muted_until = arrow.get(*struct[:6], tzinfo=arrow.now().tzinfo.zone)
logger.info(f"Before: {muted_until}")
logger.info(f"After: {muted_until.to('utc')}")

if time_delta >= 2419200:
return await embeds.error_message(ctx=ctx, description="Timeout duration cannot exceed 28 days.")
if muted_until >= arrow.utcnow().shift(days=+28):
return await embeds.send_error(ctx=ctx, description="Timeout duration cannot exceed 28 days.")

mod_embed = embeds.make_embed(
ctx=ctx,
title=f"Muting member: {member}",
description=f"{member.mention} was muted by {ctx.user.mention} for: {reason}",
thumbnail_url="https://i.imgur.com/rHtYWIt.png",
thumbnail_url="https://files.catbox.moe/6rs4fn.png",
color=discord.Color.red(),
fields=[{"name": "Duration:", "value": duration_string, "inline": False}],
fields=[
{"name": "Duration:", "value": muted_until.humanize(), "inline": True},
{"name": "Expires:", "value": f"<t:{int(muted_until.timestamp())}:R>", "inline": True},
],
)

user_embed = embeds.make_embed(
title="Uh-oh, you've been muted!",
description="If you believe this was a mistake, contact staff.",
image_url="https://i.imgur.com/840Q48l.gif",
image_url="https://files.catbox.moe/b05gg3.gif",
color=discord.Color.blurple(),
fields=[
{"name": "Server:", "value": f"[{ctx.guild.name}]({await ctx.guild.vanity_invite()})", "inline": True},
{"name": "Duration:", "value": duration_string, "inline": True},
{"name": "Server:", "value": f"{ctx.guild.name}", "inline": True},
{"name": "Duration:", "value": f"<t:{int(muted_until.timestamp())}:R>", "inline": True},
{"name": "Reason:", "value": reason, "inline": False},
],
)
Expand All @@ -97,18 +97,19 @@ async def mute(
"This can be caused by the user not being in the server, "
"having DMs disabled, or having the bot blocked."
),
inline=False,
)

ModLog(
user_id=member.id,
mod_id=ctx.user.id,
timestamp=arrow.utcnow().timestamp(),
timestamp=arrow.utcnow().int_timestamp,
reason=reason,
duration=duration_string,
duration=duration,
type="mute",
).save()

await member.timeout(arrow.get(mute_end_time).datetime, reason=reason)
await member.timeout(muted_until.datetime, reason=reason)
await ctx.followup.send(embed=mod_embed)
await log_embed_to_channel(ctx=ctx, embed=mod_embed)

Expand All @@ -134,30 +135,30 @@ async def unmute(
await ctx.response.defer(thinking=True, ephemeral=True)

if not isinstance(member, discord.Member):
return await embeds.error_message(ctx=ctx, description="That user is not in the server.")
return await embeds.send_error(ctx=ctx, description="That user is not in the server.")

if not can_action_member(ctx=ctx, member=member):
return await embeds.error_message(ctx=ctx, description=f"You cannot action {member.mention}.")
return await embeds.send_error(ctx=ctx, description=f"You cannot action {member.mention}.")

if not member.is_timed_out():
return await embeds.error_message(ctx=ctx, description=f"{member.mention} is not muted.")
return await embeds.send_error(ctx=ctx, description=f"{member.mention} is not muted.")

if len(reason) > 1024:
return await embeds.error_message(ctx=ctx, description="Reason must be less than 1024 characters.")
return await embeds.send_error(ctx=ctx, description="Reason must be less than 1024 characters.")

mod_embed = embeds.make_embed(
ctx=ctx,
title=f"Unmuting member: {member.name}",
description=f"{member.mention} was unmuted by {ctx.user.mention} for: {reason}",
color=discord.Color.green(),
thumbnail_url="https://i.imgur.com/W7DpUHC.png",
thumbnail_url="https://files.catbox.moe/izm83m.png",
)

user_embed = embeds.make_embed(
author=False,
title="Yay, you've been unmuted!",
description="Review our server rules to avoid being actioned again in the future.",
image_url="https://i.imgur.com/U5Fvr2Y.gif",
image_url="https://files.catbox.moe/razmf6.gif",
color=discord.Color.blurple(),
fields=[
{"name": "Server:", "value": f"[{ctx.guild.name}]({await ctx.guild.vanity_invite()})", "inline": True},
Expand All @@ -174,12 +175,13 @@ async def unmute(
"This can be caused by the user not being in the server, "
"having DMs disabled, or having the bot blocked."
),
inline=False,
)

ModLog(
user_id=member.id,
mod_id=ctx.user.id,
timestamp=arrow.utcnow().timestamp(),
timestamp=arrow.now().int_timestamp,
reason=reason,
type="unmute",
).save()
Expand All @@ -199,7 +201,7 @@ async def on_member_update(self, before: discord.Member, after: discord.Member)
ModLog(
user_id=after.id,
mod_id=logs[0].user.id,
timestamp=int(time.time()),
timestamp=arrow.now().int_timestamp,
reason=logs[0].reason,
type="mute",
).save()
Expand All @@ -210,7 +212,7 @@ async def on_member_update(self, before: discord.Member, after: discord.Member)
ModLog(
user_id=after.id,
mod_id=logs[0].user.id,
timestamp=int(time.time()),
timestamp=arrow.now().int_timestamp,
reason=logs[0].reason,
type="unmute",
)
Expand Down
39 changes: 16 additions & 23 deletions chiya/cogs/note.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import time
from datetime import datetime
from typing import Literal

import discord
from discord import app_commands
from discord.ext import commands

from chiya import database
from chiya.config import config
from chiya.database import ModLog
from chiya.utils import embeds
from chiya.utils.helpers import log_embed_to_channel
from chiya.utils.pagination import MyMenuPages, MySource
Expand All @@ -26,26 +25,21 @@ async def add_note(self, ctx: discord.Interaction, user: discord.Member | discor
"""Adds a note to the specified user queryable via /search."""
await ctx.response.defer(thinking=True, ephemeral=True)

db = database.Database().get()
note_id = db["mod_logs"].insert(
dict(
user_id=user.id,
mod_id=ctx.user.id,
timestamp=int(time.time()),
reason=note,
type="note",
)
)
db.commit()
db.close()
note = ModLog(
user_id=user.id,
mod_id=ctx.user.id,
timestamp=datetime.now().timestamp(),
reason=note,
type="note",
).save()

embed = embeds.make_embed(
title=f"Noting user: {user.name}",
description=f"{user.mention} was noted by {ctx.user.mention}",
thumbnail_url="https://i.imgur.com/A4c19BJ.png",
color=discord.Color.blurple(),
fields=[
{"name": "ID:", "value": note_id, "inline": False},
{"name": "ID:", "value": note.id, "inline": False},
{"name": "Note:", "value": note, "inline": False},
],
)
Expand All @@ -71,12 +65,12 @@ async def search_mod_actions(
"""
await ctx.response.defer(thinking=True, ephemeral=True)

db = database.Database().get()
results = ModLog.query.filter_by(user_id=user.id, type=action).order_by(ModLog.id.asc()).all()
# TODO: can't this be merged into one call because action will return None either way?
if action:
results = db["mod_logs"].find(user_id=user.id, type=action, order_by="-id")
else:
results = db["mod_logs"].find(user_id=user.id, order_by="-id")
# if action:
# results = db["mod_logs"].find(user_id=user.id, type=action, order_by="-id")
# else:
# results = db["mod_logs"].find(user_id=user.id, order_by="-id")

actions = []
for action in results:
Expand All @@ -100,9 +94,8 @@ async def search_mod_actions(

actions.append(action_string)

db.close()
if not actions:
return await embeds.error_message(ctx=ctx, description="No mod actions found for that user!")
return await embeds.send_error(ctx=ctx, description="No mod actions found for that user!")

embed = embeds.make_embed(title="Mod Actions")
embed.set_author(name=user, icon_url=user.display_avatar)
Expand Down Expand Up @@ -133,7 +126,7 @@ async def edit_log(self, ctx: discord.Interaction, id: int, note: str) -> None:
db = database.Database().get()
log = db["mod_logs"].find_one(id=id)
if not log:
return await embeds.error_message(ctx=ctx, description="Could not find a log with that ID!")
return await embeds.send_error(ctx=ctx, description="Could not find a log with that ID!")

user = await self.bot.fetch_user(log["user_id"])
embed = embeds.make_embed(
Expand Down
4 changes: 2 additions & 2 deletions chiya/cogs/purge.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ async def purge(self, ctx: discord.Interaction, amount: int, reason: str) -> Non
await ctx.response.defer(thinking=True)

if not self.can_purge_messages(ctx):
return await embeds.error_message(ctx=ctx, description="You cannot use that command in this category.")
return await embeds.send_error(ctx=ctx, description="You cannot use that command in this category.")

if len(reason) > 4096:
return await embeds.error_message(ctx=ctx, description="Reason must be less than 4096 characters.")
return await embeds.send_error(ctx=ctx, description="Reason must be less than 4096 characters.")

amount = 100 if amount > 100 else amount

Expand Down
Loading

0 comments on commit 7a4a47e

Please sign in to comment.