Skip to content

Preserve Invocation Context#3377

Merged
jb3 merged 23 commits intopython-discord:mainfrom
DragonSenseiGuy:preserve-invocation-context
Sep 23, 2025
Merged

Preserve Invocation Context#3377
jb3 merged 23 commits intopython-discord:mainfrom
DragonSenseiGuy:preserve-invocation-context

Conversation

@DragonSenseiGuy
Copy link
Copy Markdown
Contributor

@DragonSenseiGuy DragonSenseiGuy commented Aug 31, 2025

Added:

  • A link to the pastebin for commands executed outside of #bot-commands, to preserve invocation context

Closes #3376

@jb3 jb3 enabled auto-merge (squash) September 1, 2025 18:28
@jb3 jb3 disabled auto-merge September 1, 2025 18:29
@jb3 jb3 enabled auto-merge (squash) September 1, 2025 18:29
@jb3 jb3 disabled auto-merge September 1, 2025 18:29
@jb3 jb3 enabled auto-merge (squash) September 1, 2025 18:30
Comment thread bot/decorators.py
Comment thread bot/decorators.py
Comment thread bot/decorators.py Outdated
@swfarnsworth
Copy link
Copy Markdown
Contributor

@jb3, am I correct in my understanding that this copies messages from the server to our pastebin, possibly unexpectedly?

Comment thread bot/decorators.py
Comment thread bot/decorators.py Outdated
@jb3
Copy link
Copy Markdown
Member

jb3 commented Sep 2, 2025

@jb3, am I correct in my understanding that this copies messages from the server to our pastebin, possibly unexpectedly?

Yes, but with it being a bot command I'm less worried about that. Previously we've worried about that when things such as automatically uploading unformatted code have been mentioned, or when we are talking about uploading messages which are not direct interactions with the bot.

In this case, it's reasonable to temporarily upload after a user has directly used our features (bear in mind we automatically upload the results if they are too long).

@swfarnsworth
Copy link
Copy Markdown
Contributor

Perhaps we can send the user the link to delete the paste? You can largely copy the code here:

# Send the user a DM with the delete link for the paste.
# The angle brackets around the remove link are required to stop Discord from visiting the URL to produce a
# preview, thereby deleting the paste
await message.author.send(content=f"[Click here](<{paste_response.removal}>) to delete your recent paste.")

auto-merge was automatically disabled September 2, 2025 19:44

Head branch was pushed to by a user without write access

@DragonSenseiGuy
Copy link
Copy Markdown
Contributor Author

Perhaps we can send the user the link to delete the paste? You can largely copy the code here:

# Send the user a DM with the delete link for the paste.
# The angle brackets around the remove link are required to stop Discord from visiting the URL to produce a
# preview, thereby deleting the paste
await message.author.send(content=f"[Click here](<{paste_response.removal}>) to delete your recent paste.")

Added.

Comment thread bot/decorators.py Outdated
Comment on lines +183 to +184
content=
f"Your command output was redirected to <#{Channels.bot_commands}>. [Click here](<{paste_response.removal}>) to delete the automatically uploaded copy of your original command."
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a string is too long, the preferred format is like this

content = (
    "Lorem ipsum dolor sit amet, consectetur "
    f"adipiscing {elit}, sed do eiusmod tempor "
    "incididunt ut labore et dolore magna aliqua."
)

This leverages implicit string concatenation. Note that segments using f-string need to start with an f. The trailing spaces at the end of each line are literal single spaces that are not line breaks.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, Just saw the lint errors, pushed a commit.

Copy link
Copy Markdown
Member

@jb3 jb3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting and wording.

Comment thread bot/decorators.py Outdated
Comment thread bot/decorators.py Outdated
Comment thread bot/decorators.py Outdated
Comment thread bot/decorators.py Outdated
Comment thread bot/decorators.py Outdated
@jb3 jb3 requested review from MarkKoz and swfarnsworth September 5, 2025 01:09
Copy link
Copy Markdown
Contributor

@wookie184 wookie184 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly looks good, thanks for the PR.

Comment thread bot/decorators.py Outdated
Copy link
Copy Markdown
Contributor

@MarkKoz MarkKoz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Looks good besides the comment about exception handling.

Maybe we should only be doing this when RedirectOutput.delete_invocation is true. But in practice this is a global option and the default is true, so it's probably not worth our time. I'm not sure why we even have a config option for that.

@jb3
Copy link
Copy Markdown
Member

jb3 commented Sep 16, 2025

Thanks. Looks good besides the comment about exception handling.

Maybe we should only be doing this when RedirectOutput.delete_invocation is true. But in practice this is a global option and the default is true, so it's probably not worth our time. I'm not sure why we even have a config option for that.

If we have the option we may as well implement the functionality. I think just wrapping the network request section in an if statement should cover it? Once the definition for paste_response is added back then as long as that remains set to None then the bot will just form the generic no-link message and send that.

@DragonSenseiGuy
Copy link
Copy Markdown
Contributor Author

Thanks. Looks good besides the comment about exception handling.
Maybe we should only be doing this when RedirectOutput.delete_invocation is true. But in practice this is a global option and the default is true, so it's probably not worth our time. I'm not sure why we even have a config option for that.

If we have the option we may as well implement the functionality. I think just wrapping the network request section in an if statement should cover it? Once the definition for paste_response is added back then as long as that remains set to None then the bot will just form the generic no-link message and send that.

So an if statement over the entire aysnc with ClientSession() as session?

@jb3
Copy link
Copy Markdown
Member

jb3 commented Sep 17, 2025

[...]

So an if statement over the entire aysnc with ClientSession() as session?

Yes, as well as the variable declaration.

@DragonSenseiGuy DragonSenseiGuy force-pushed the preserve-invocation-context branch from ea39656 to 0ea257b Compare September 19, 2025 23:55
Comment thread bot/decorators.py Outdated
# Send a DM to the user about the redirect and paste removal
await ctx.author.send(
f"Your command output was redirected to <#{Channels.bot_commands}>."
f" [Click here](<{paste_response.removal}>) to delete the pasted"
Copy link
Copy Markdown
Contributor

@decorator-factory decorator-factory Sep 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will break if paste_response is None

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the logs stop it from executing?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They don't, as mentioned in wookie's previous review (logs explicitly do not stop execution, nor do we want them to).

This will need wrapping with an if paste_response:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then no else or is there a fallback option?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need an else because if there isn't a paste_response then we didn't upload their paste.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@jb3 jb3 merged commit a0e6853 into python-discord:main Sep 23, 2025
5 checks passed
@DragonSenseiGuy
Copy link
Copy Markdown
Contributor Author

Thanks for the merge @jb3 !

Comment thread bot/decorators.py
await ctx.send(f"Here's the output of your command, {ctx.author.mention}")
paste_response = None
if RedirectOutput.delete_invocation:
async with ClientSession() as session:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should use ctx.bot.http_session rather than creating a whole new session.

@DragonSenseiGuy DragonSenseiGuy deleted the preserve-invocation-context branch September 27, 2025 01:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Preserve invocation context when redirecting output of command

7 participants