Skip to content

Commit 4b96d91

Browse files
committed
move scampurge to helper function, change scamkick and scamprobate
1 parent b4404a7 commit 4b96d91

File tree

3 files changed

+79
-42
lines changed

3 files changed

+79
-42
lines changed

cogs/kickban.py

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from utils.utils import send_dm_message
1414
from utils import Restriction
1515
from utils.database import FilterKind
16+
from utils.utils import scam_purge
1617

1718
if TYPE_CHECKING:
1819
from kurisu import Kurisu
@@ -80,26 +81,12 @@ async def scamkick(self, ctx: GuildContext, member: discord.Member):
8081
msg += "\n\nYou are able to rejoin the server, but please secure your account and consider adding two-factor authentication."
8182
await send_dm_message(member, msg, ctx)
8283

83-
after = discord.utils.utcnow() - datetime.timedelta(days=1)
84-
deleted_count = 0
85-
failures: list[str] = []
86-
87-
for channel in ctx.guild.text_channels:
88-
try:
89-
deleted = await channel.purge(
90-
limit=30,
91-
after=after,
92-
oldest_first=False,
93-
check=lambda m: m.author.id == member.id,
94-
reason=f"scamkick: {reason}",
95-
bulk=True,
96-
)
97-
deleted_count += len(deleted)
98-
99-
except (discord.Forbidden, discord.HTTPException) as e:
100-
status = getattr(e, "status", None)
101-
code = getattr(e, "code", None)
102-
failures.append(f"#{channel.name}: {type(e).__name__} (status={status}, code={code})")
84+
deleted_count, failures = await scam_purge(
85+
guild=ctx.guild,
86+
target_id=member.id,
87+
reason=f"scamkick: {reason}",
88+
limit=30,
89+
)
10390

10491
try:
10592
await member.kick(reason=reason)

cogs/mod.py

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from utils.converters import DateOrTimeToSecondsConverter, TimeTransformer
1515
from utils.checks import is_staff, check_staff, check_bot_or_staff, is_staff_app
1616
from utils.utils import paginate_message, send_dm_message, parse_time, text_to_discord_file, gen_color, \
17-
create_error_embed, create_userinfo_embed
17+
create_error_embed, create_userinfo_embed, scam_purge
1818

1919
if TYPE_CHECKING:
2020
from kurisu import Kurisu
@@ -676,27 +676,12 @@ async def scamprobate(self, ctx: GuildContext, member: discord.Member | discord.
676676
msg += "\n\nPlease secure your account (change password, enable 2FA) and contact staff if you think this was a mistake."
677677
await send_dm_message(member, msg, ctx)
678678

679-
after = discord.utils.utcnow() - timedelta(days=1)
680-
deleted_count = 0
681-
failures: list[str] = []
682-
target_id = member.id
683-
684-
for channel in ctx.guild.text_channels:
685-
try:
686-
deleted = await channel.purge(
687-
limit=30,
688-
after=after,
689-
oldest_first=False,
690-
check=lambda m, tid=target_id: m.author.id == tid,
691-
reason=f"scamprobate: {reason}",
692-
bulk=True,
693-
)
694-
deleted_count += len(deleted)
695-
696-
except (discord.Forbidden, discord.HTTPException) as e:
697-
status = getattr(e, "status", None)
698-
code = getattr(e, "code", None)
699-
failures.append(f"#{channel.name}: {type(e).__name__} (status={status}, code={code})")
679+
deleted_count, failures = await scam_purge(
680+
guild=ctx.guild,
681+
target_id=member.id,
682+
reason=f"scamprobate: {reason}",
683+
limit=30,
684+
)
700685

701686
if failures:
702687
text = "⚠️ purge issues:\n" + "\n".join(failures)

utils/utils.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,29 @@ def wii(cls):
3434
def legacy(cls):
3535
return cls(0x707070)
3636

37+
PUBLIC_CHAN_IDS = [
38+
196635695958196224, # 3ds 1
39+
247557068490276864, # 3ds 2
40+
279783073497874442, # wiiu
41+
439933093118476289, # switch 1
42+
655681905907466250, # switch 2
43+
1159223824676565114, # wii-vwii
44+
196635745551646720, # modding gen
45+
770582653132734464, # tt
46+
314582689829355521, # legacy
47+
196635781798952960, # dev
48+
233002779717795850, # hardware
49+
270890866820775946, # appeals
50+
872309776233684992, # meta
51+
918315061003579482, # kurisu-dev
52+
1159936589997281390, # guide meta
53+
1022667983081967636, # wiki discussion
54+
1364102432790941776, # hw wiki discussion
55+
314856589716750346, # off topic
56+
300388576451887104, # nintendo discussion
57+
485138525885431808, # elsewhere (just in case)
58+
]
59+
3760

3861
async def send_dm_message(member: discord.Member, message: str, ctx: Optional[commands.Context] = None, **kwargs) -> bool:
3962
"""A helper method for sending a message to a member's DMs.
@@ -195,3 +218,45 @@ async def simple_embed(ctx: commands.Context, text: str, *, title: str = "", col
195218
embed = discord.Embed(title=title, color=color)
196219
embed.description = cleandoc(text)
197220
await ctx.send(embed=embed, reference=ctx.message.reference, mention_author=mention_author)
221+
222+
async def scam_purge(
223+
guild: discord.Guild,
224+
target_id: int,
225+
*,
226+
reason: str,
227+
limit: int = 50,
228+
) -> tuple[int, list[str]]:
229+
"""
230+
Purge messages by a user from PUBLIC_CHAN_IDS in the last 24 hours.
231+
"""
232+
after = discord.utils.utcnow() - datetime.timedelta(days=1)
233+
234+
deleted_count = 0
235+
failures: list[str] = []
236+
237+
for channel_id in PUBLIC_CHAN_IDS:
238+
channel = guild.get_channel(channel_id)
239+
240+
if channel is None or not isinstance(channel, discord.TextChannel):
241+
failures.append(f"{channel_id}: not found or not text")
242+
continue
243+
244+
try:
245+
deleted = await channel.purge(
246+
limit=limit,
247+
after=after,
248+
oldest_first=False,
249+
check=lambda m, tid=target_id: m.author.id == tid,
250+
reason=reason,
251+
bulk=True,
252+
)
253+
deleted_count += len(deleted)
254+
255+
except (discord.Forbidden, discord.HTTPException) as e:
256+
status = getattr(e, "status", None)
257+
code = getattr(e, "code", None)
258+
failures.append(
259+
f"#{channel.name}: {type(e).__name__} (status={status}, code={code})"
260+
)
261+
262+
return deleted_count, failures

0 commit comments

Comments
 (0)