Skip to content

Commit

Permalink
Added slash command 'post-copy'
Browse files Browse the repository at this point in the history
- bot posts a copy of the referenced message content into target channel
- accepts either message ID or message URI as last argument
- handles common errors in fetching the message
  • Loading branch information
paranox committed Oct 6, 2024
1 parent 10bac45 commit 743f757
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,9 @@ async def message_count_other(ctx, käyttäjä: discord.Option(discord.SlashComm
await mybot.message_count_other_command(ctx, käyttäjä)
pass

@bot.slash_command(name="post-copy", description="Tekee toisen käyttäjän viestistä kopion, jonka julkaisee kohdekanavalla")
async def post_copy(ctx, kanava: discord.Option(discord.SlashCommandOptionType.channel), viesti_viittaus): # "kanava" and "viesti_viittaus" are in Finnish because they're visible in command context help
await mybot.post_copy(ctx, channel, message)
pass

bot.run(cfg["token"])
35 changes: 35 additions & 0 deletions mybot.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,41 @@ async def list_threads_command(self, ctx):
await ctx.respond("**Threads** ({} total)".format(n_threads))
for i in range(0, len(lines), 50):
await ctx.send_followup("\n".join(lines[i:i+50]))

# can raise NotFound, Forbidden or HTTPException
# message_reference can be either message ID or URL (which ends with the ID)
async def post_copy(self, ctx, channel, message_reference):
message_id = ""

# parse ID from URI or use it directly if not an URI
slash_index = message_reference.rfind('/')
if slash_index >= 0:
slash_index += 1 # Exclude the slash itself
message_id = message_reference[slash_index:]
else:
message_id = message_reference

id = -1

# parse ID into an integer and handle error if thrown
try:
id = int(message_id)
except ValueError:
await ctx.send_response(
content="Virheellinen viittaus kopioitavaan viestiin {message_reference}->{message_id}.",
ephemeral=True)
return

try:
message = await ctx.fetch_message(id)
except Exception as e:
await ctx.send_response(
content="Viestin {message_id} hakeminen epäonnistui, virhe: '{e}'",
ephemeral=True)
return

text = message.content
await message.channel.send(text)

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

0 comments on commit 743f757

Please sign in to comment.