|
1 | 1 | import time
|
2 | 2 |
|
| 3 | +import arrow |
3 | 4 | import discord
|
4 | 5 | from discord import app_commands
|
5 | 6 | from discord.ext import commands
|
6 |
| -from loguru import logger as log |
7 | 7 |
|
8 |
| -from chiya import database |
9 | 8 | from chiya.config import config
|
| 9 | +from chiya.database import ModLog |
10 | 10 | from chiya.utils import embeds
|
11 | 11 | from chiya.utils.helpers import can_action_member, log_embed_to_channel
|
12 | 12 |
|
13 | 13 |
|
14 |
| -class BansCommands(commands.Cog): |
| 14 | +class BanCog(commands.Cog): |
15 | 15 | def __init__(self, bot: commands.Bot) -> None:
|
16 | 16 | self.bot = bot
|
17 | 17 |
|
@@ -93,18 +93,13 @@ async def ban(
|
93 | 93 | ),
|
94 | 94 | )
|
95 | 95 |
|
96 |
| - db = database.Database().get() |
97 |
| - db["mod_logs"].insert( |
98 |
| - dict( |
99 |
| - user_id=user.id, |
100 |
| - mod_id=ctx.user.id, |
101 |
| - timestamp=int(time.time()), |
102 |
| - reason=reason, |
103 |
| - type="ban", |
104 |
| - ) |
105 |
| - ) |
106 |
| - db.commit() |
107 |
| - db.close() |
| 96 | + ModLog( |
| 97 | + user_id=user.id, |
| 98 | + mod_id=ctx.user.id, |
| 99 | + timestamp=arrow.utcnow().timestamp(), |
| 100 | + reason=reason, |
| 101 | + type="ban", |
| 102 | + ).save() |
108 | 103 |
|
109 | 104 | await ctx.guild.ban(user=user, reason=reason, delete_message_days=daystodelete or 0)
|
110 | 105 | await ctx.followup.send(embed=mod_embed)
|
@@ -140,18 +135,35 @@ async def unban(self, ctx: discord.Interaction, user: discord.Member | discord.U
|
140 | 135 | color=discord.Color.green(),
|
141 | 136 | )
|
142 | 137 |
|
143 |
| - db = database.Database().get() |
144 |
| - db["mod_logs"].insert( |
145 |
| - dict(user_id=user.id, mod_id=ctx.user.id, timestamp=int(time.time()), reason=reason, type="unban") |
146 |
| - ) |
147 |
| - db.commit() |
148 |
| - db.close() |
| 138 | + ModLog( |
| 139 | + user_id=user.id, |
| 140 | + mod_id=ctx.user.id, |
| 141 | + timestamp=arrow.utcnow().timestamp(), |
| 142 | + reason=reason, |
| 143 | + type="unban", |
| 144 | + ).save() |
149 | 145 |
|
150 | 146 | await ctx.guild.unban(user, reason=reason)
|
151 | 147 | await ctx.followup.send(embed=mod_embed)
|
152 | 148 | await log_embed_to_channel(ctx=ctx, embed=mod_embed)
|
153 | 149 |
|
| 150 | + @commands.Cog.listener() |
| 151 | + async def on_member_ban(self, guild: discord.Guild, user: discord.Member | discord.User) -> None: |
| 152 | + """ |
| 153 | + Add the user's ban entry to the database if they were banned manually. |
| 154 | + """ |
| 155 | + # TODO: Emit an embed in #moderation when this happens |
| 156 | + ban_entry = await guild.fetch_ban(user) |
| 157 | + logs = [log async for log in guild.audit_logs(limit=1, action=discord.AuditLogAction.ban)] |
| 158 | + if logs[0].user != self.bot.user: |
| 159 | + ModLog( |
| 160 | + user_id=user.id, |
| 161 | + mod_log=logs[0].user.id, |
| 162 | + timestamp=int(time.time()), |
| 163 | + reason=ban_entry.reason, |
| 164 | + type="ban", |
| 165 | + ) |
| 166 | + |
154 | 167 |
|
155 | 168 | async def setup(bot: commands.Bot) -> None:
|
156 |
| - await bot.add_cog(BansCommands(bot)) |
157 |
| - log.info("Commands loaded: ban") |
| 169 | + await bot.add_cog(BanCog(bot)) |
0 commit comments