Skip to content

Commit fbd50a8

Browse files
committed
fix bugs with and add more admin only features
1 parent acf6dcc commit fbd50a8

1 file changed

Lines changed: 52 additions & 3 deletions

File tree

bot.py

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,15 +205,34 @@ async def cronjob():
205205
count = count[0]
206206

207207
async with db.execute("SELECT user FROM notification") as cursor:
208-
mention_users = await cursor.fetchall()
209-
mentions = " ".join([f"<@{user[0]}>" for user in mention_users]) # Assuming 'user' is a Discord ID
208+
notify_users = await cursor.fetchall()
210209

211210
embed = discord.Embed(title=t("embed.dresscode_title"), color=discord.Color.purple())
212211
embed.add_field(name=t("embed.next_theme"), value=result, inline=False)
213212
embed.add_field(name=t("embed.submitted_by"), value=user, inline=False)
214213
embed.add_field(name=t("embed.themes_count"), value=count, inline=False)
215214
await bot.change_presence(activity=discord.Game(name=t("presence.theme", theme=result)))
216-
await channel.send(f"{mentions}", embed=embed)
215+
if channel is not None:
216+
await channel.send(embed=embed)
217+
218+
for notification_user in notify_users:
219+
raw_user_id = notification_user[0]
220+
try:
221+
notify_user_id = int(raw_user_id)
222+
except (TypeError, ValueError):
223+
logging.warning("Invalid notification user id: %r", raw_user_id)
224+
continue
225+
226+
try:
227+
target_user = bot.get_user(notify_user_id) or await bot.fetch_user(notify_user_id)
228+
if target_user is None:
229+
logging.warning("Notification user not found: %s", notify_user_id)
230+
continue
231+
await target_user.send(embed=embed)
232+
except discord.Forbidden:
233+
logging.warning("Cannot DM user (DMs disabled): %s", notify_user_id)
234+
except Exception as dm_error:
235+
logging.error("Failed to DM user %s: %s", notify_user_id, dm_error)
217236
else:
218237
embed = discord.Embed(title=t("embed.dresscode_title"), color=discord.Color.purple())
219238
embed.add_field(name=t("embed.next_theme"), value=t("embed.no_themes_available"), inline=False)
@@ -413,6 +432,9 @@ async def tmall(interaction: discord.Interaction):
413432
name='tmon',
414433
description=app_commands.locale_str("Enable scheduled output posting.", key="cmd.tmon.description")
415434
)
435+
@app_commands.guild_only()
436+
@app_commands.default_permissions(administrator=True)
437+
@app_commands.checks.has_permissions(administrator=True)
416438
async def tmon(interaction: discord.Interaction):
417439
try:
418440
async with aiosqlite.connect(db_path) as db:
@@ -423,11 +445,26 @@ async def tmon(interaction: discord.Interaction):
423445
await send_interaction_message(interaction, content=ti(interaction, "msg.tmon_error", error=e), ephemeral=True)
424446

425447

448+
@tmon.error
449+
async def tmon_error(interaction: discord.Interaction, error: app_commands.AppCommandError):
450+
if isinstance(error, app_commands.MissingPermissions):
451+
await send_interaction_message(interaction, content=ti(interaction, "msg.tmdelete_admin_only"), ephemeral=True)
452+
return
453+
if isinstance(error, app_commands.NoPrivateMessage):
454+
await send_interaction_message(interaction, content=ti(interaction, "msg.tmdelete_no_dm"), ephemeral=True)
455+
return
456+
457+
await send_interaction_message(interaction, content=ti(interaction, "msg.tmon_error", error=error), ephemeral=True)
458+
459+
426460
#Turn output off
427461
@bot.tree.command(
428462
name='tmoff',
429463
description=app_commands.locale_str("Disable scheduled output posting.", key="cmd.tmoff.description")
430464
)
465+
@app_commands.guild_only()
466+
@app_commands.default_permissions(administrator=True)
467+
@app_commands.checks.has_permissions(administrator=True)
431468
async def tmoff(interaction: discord.Interaction):
432469
try:
433470
async with aiosqlite.connect(db_path) as db:
@@ -438,6 +475,18 @@ async def tmoff(interaction: discord.Interaction):
438475
await send_interaction_message(interaction, content=ti(interaction, "msg.tmoff_error", error=e), ephemeral=True)
439476

440477

478+
@tmoff.error
479+
async def tmoff_error(interaction: discord.Interaction, error: app_commands.AppCommandError):
480+
if isinstance(error, app_commands.MissingPermissions):
481+
await send_interaction_message(interaction, content=ti(interaction, "msg.tmdelete_admin_only"), ephemeral=True)
482+
return
483+
if isinstance(error, app_commands.NoPrivateMessage):
484+
await send_interaction_message(interaction, content=ti(interaction, "msg.tmdelete_no_dm"), ephemeral=True)
485+
return
486+
487+
await send_interaction_message(interaction, content=ti(interaction, "msg.tmoff_error", error=error), ephemeral=True)
488+
489+
441490
#Add user to notification
442491
@bot.tree.command(
443492
name='tmnotify',

0 commit comments

Comments
 (0)