Skip to content

Commit

Permalink
Command to look up message counts. Need to disallow this for non-mode…
Browse files Browse the repository at this point in the history
…rators from server settings.
  • Loading branch information
jnalanko committed Feb 28, 2024
1 parent c061feb commit 4db3ee3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
5 changes: 5 additions & 0 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,9 @@ async def message_count(ctx):
await mybot.message_count_command(ctx)
pass

@bot.slash_command(name="toisen-viestilaskuri", description="Toisen käyttäjän viimeisen 3kk:n viestimäärä DM:llä (vain moderaattoreille).")
async def message_count_other(ctx, käyttäjä: discord.Option(discord.SlashCommandOptionType.user)): # "käyttäjä" is in Finnish because it shows in the slash command help"
await mybot.message_count_other_command(ctx, käyttäjä)
pass

bot.run(cfg["token"])
25 changes: 24 additions & 1 deletion mybot.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,30 @@ def check_midnight_winner(self, message) -> str:
conn.commit()
return prize
return None


async def message_count_other_command(self, ctx, user):
with self.connection_pool.get_connection() as conn:
cursor = conn.cursor()
cursor.execute("SELECT SUM(count) FROM message_counts WHERE user_id = %s", [user.id])
rows = cursor.fetchall()
if len(rows) != 1 or len(rows[0]) != 1:
await ctx.send_response(
content="Virhe! Sori, en osannut.",
ephemeral=True,
)
else:
count = int(rows[0][0])
try:
await ctx.send_response(
content="Käyttäjä {} on lähettänyt tarkastelujakson aikana {} viestiä.".format(user.name, count),
ephemeral=True,
)
except:
await ctx.send_response(
content="Yritin lähettää viestien määrän, mutta se ei onnistunut.",
ephemeral=True,
)

async def message_count_command(self, ctx):
with self.connection_pool.get_connection() as conn:
cursor = conn.cursor()
Expand Down

0 comments on commit 4db3ee3

Please sign in to comment.