Skip to content

Allow snekbox to run in both 3.10 and 3.11#2218

Merged
ChrisLovering merged 7 commits into
mainfrom
3.11-snekbox
Jul 16, 2022
Merged

Allow snekbox to run in both 3.10 and 3.11#2218
ChrisLovering merged 7 commits into
mainfrom
3.11-snekbox

Conversation

@ChrisLovering
Copy link
Copy Markdown
Member

@ChrisLovering ChrisLovering commented Jul 13, 2022

This PR updates the snekbox cog to run using Python 3.11 by default, and send a button with the code output response:
image

Clicking this button allows the use to re-run their same code, but under 3.10.
image

Clicking this button deletes the previous response and sends a new response, also with the button. Clicking the button again allows the user to change back to 3.11.

This also works with editing your evaled code, where the button ill always run the most recent edition of the user's code.

To do this the cog needed to track, for each user, the active eval's code, python version and response message.
Since a button press can now also trigger a job to continue, if we did not track these then editing your own code and then re-evaluating it would trigger the wait_fors in continue_job for each time you pressed the button to change languages.

Comment thread docker-compose.yml
Comment thread bot/exts/utils/snekbox.py Outdated
Comment thread bot/exts/utils/snekbox.py Outdated
Comment thread bot/exts/utils/snekbox.py Outdated
Comment thread bot/exts/utils/snekbox.py Outdated
Comment thread bot/exts/utils/snekbox.py Outdated
Comment on lines +483 to +488
self.jobs[ctx.author.id][ctx.message.id] = {
"code": code,
"python_version": python_version,
"start_date": datetime.datetime.now(),
"bot_response_message_id": response.id
}
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.

A namedtuple would be nicer, though I have doubts that you even need to store most of this.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

You're correct, it only needs to be a invokation:response mapping 0817398

Comment thread bot/exts/utils/snekbox.py Outdated
Comment thread bot/exts/utils/snekbox.py Outdated
Comment thread bot/exts/utils/snekbox.py Outdated
@wookie184
Copy link
Copy Markdown
Contributor

Being able to run in 3.11 sounds good.

I'm not sure that the button to change versions is worth the added clutter given that in the vast majority of cases of eval commands the version wouldn't make a difference.

Was having a separate command like !eval3.10, or an optional argument like !eval 3.10 considered?

@ChrisLovering
Copy link
Copy Markdown
Member Author

Being able to run in 3.11 sounds good.

I'm not sure that the button to change versions is worth the added clutter given that in the vast majority of cases of eval commands the version wouldn't make a difference.

Was having a separate command like !eval3.10, or an optional argument like !eval 3.10 considered?

This PR does add optional argument to the commands too.

The thoughts behind the button is if the user runs into a 3.11 problem, they can easily switch to 3.10 and see if it works on that.

@ChrisLovering
Copy link
Copy Markdown
Member Author

Something that could reduce the clutter would be making the delete action a button too, so it's inn line, rather an emoji below.

Comment thread bot/utils/Interactions.py Outdated
Comment thread bot/exts/utils/snekbox.py Outdated
Comment thread bot/exts/utils/snekbox.py Outdated
Comment thread bot/exts/utils/snekbox.py Outdated
Comment thread bot/exts/utils/snekbox.py Outdated
Comment thread bot/exts/utils/snekbox.py Outdated
Comment thread bot/exts/utils/snekbox.py Outdated
Comment thread bot/exts/utils/snekbox.py Outdated
Comment thread bot/exts/utils/snekbox.py Outdated
Comment thread bot/utils/Interactions.py Outdated
Comment thread docker-compose.yml
@MarkKoz
Copy link
Copy Markdown
Contributor

MarkKoz commented Jul 16, 2022

I get the following error when trying to use the delete button

2022-07-16 13:56:31 | discord.ui.view | ERROR | Ignoring exception in view <ViewWithUserAndRoleCheck timeout=180.0 children=2> for item <DeleteMessageButton style=<ButtonStyle.secondary: 2> url=None disabled=False label='Delete' emoji=None row=None>
Traceback (most recent call last):
  File "/home/mark/repos/python/bot-pydis/.venv/lib/python3.9/site-packages/discord/ui/view.py", line 412, in _scheduled_task
    await item.callback(interaction)
  File "/home/mark/repos/python/bot-pydis/.venv/lib/python3.9/site-packages/botcore/utils/interactions.py", line 86, in callback
    await interaction.delete_original_message()
  File "/home/mark/repos/python/bot-pydis/.venv/lib/python3.9/site-packages/discord/interactions.py", line 486, in delete_original_message
    await adapter.delete_original_interaction_response(
  File "/home/mark/repos/python/bot-pydis/.venv/lib/python3.9/site-packages/discord/webhook/async_.py", line 217, in request
    raise NotFound(response, data)
discord.errors.NotFound: 404 Not Found (error code: 10015): Unknown Webhook

@ChrisLovering
Copy link
Copy Markdown
Member Author

I get the following error when trying to use the delete button

2022-07-16 13:56:31 | discord.ui.view | ERROR | Ignoring exception in view <ViewWithUserAndRoleCheck timeout=180.0 children=2> for item <DeleteMessageButton style=<ButtonStyle.secondary: 2> url=None disabled=False label='Delete' emoji=None row=None>
Traceback (most recent call last):
  File "/home/mark/repos/python/bot-pydis/.venv/lib/python3.9/site-packages/discord/ui/view.py", line 412, in _scheduled_task
    await item.callback(interaction)
  File "/home/mark/repos/python/bot-pydis/.venv/lib/python3.9/site-packages/botcore/utils/interactions.py", line 86, in callback
    await interaction.delete_original_message()
  File "/home/mark/repos/python/bot-pydis/.venv/lib/python3.9/site-packages/discord/interactions.py", line 486, in delete_original_message
    await adapter.delete_original_interaction_response(
  File "/home/mark/repos/python/bot-pydis/.venv/lib/python3.9/site-packages/discord/webhook/async_.py", line 217, in request
    raise NotFound(response, data)
discord.errors.NotFound: 404 Not Found (error code: 10015): Unknown Webhook

I keep using the wrong delete method for these interactions 😮‍💨
python-discord/bot-core#104 for the fix.

@MarkKoz
Copy link
Copy Markdown
Contributor

MarkKoz commented Jul 16, 2022

Error

2022-07-16 14:17:41 | botcore.utils.scheduling | ERROR | Error in task Task-674 139899212692800!
Traceback (most recent call last):
  File "/home/mark/repos/python/bot-pydis/bot/exts/utils/snekbox.py", line 466, in run_job
    response = await self.send_job(ctx, python_version, code, args=args, job_name=job_name)
  File "/home/mark/repos/python/bot-pydis/bot/utils/lock.py", line 112, in wrapper
    raise LockedResourceError(str(namespace), id_)
bot.errors.LockedResourceError: Cannot operate on snekbox.run_job `137073073168973824`; it is currently locked and in use by another operation.

Reproduction:

  1. Run an eval
  2. Run another eval
  3. Quickly press "run in 3.10/11" on the first eval before the second finishes.

@ChrisLovering
Copy link
Copy Markdown
Member Author

ChrisLovering commented Jul 16, 2022

Hmm, it seems the cog_command_error isn't hit when the function is being called from the interaction.

It's an easy fix, we just need to move the error handling from there to instead be a try/except around send_job inside run_job.

@ChrisLovering
Copy link
Copy Markdown
Member Author

Will squash some of these commits before merging.

@ChrisLovering ChrisLovering added a: utility Related to utility commands: (bot, eval, extensions, jams, reminders, snekbox, utils) t: enhancement Changes or improvements to existing features review: do not merge The PR can be reviewed but cannot be merged now labels Jul 16, 2022
Comment thread bot/exts/utils/snekbox.py
Comment thread bot/exts/utils/snekbox.py Outdated

return output, paste_link

@lock_arg("snekbox.run_job", "ctx", attrgetter("author.id"), raise_error=True)
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.

Not really important but...

Suggested change
@lock_arg("snekbox.run_job", "ctx", attrgetter("author.id"), raise_error=True)
@lock_arg("snekbox.send_job", "ctx", attrgetter("author.id"), raise_error=True)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good point, fixed this!

ChrisLovering and others added 3 commits July 16, 2022 23:45
To do this we need to track, for each user, the active eval's code, python version and response message. This is because a button press can now also trigger a job to continue. If we did not track these, then editing your own code and then re-evaluating it would trigger the wait_fors in continue_job for each time you pressed the button to change languages.

Co-authored-by: Mark <1515135+MarkKoz@users.noreply.github.com>
Rather than passing around superfluous variables.
The cog_command_error isn't hit when the run_job function is called from the button interaction, this means if the lock error is raiseed, it doees not get handled.
This is so that we do not need to spawn the run_job call in a seperate task.

This also wraps interaction.message.delete() in a NotFound suppress to cover the case where a user re-runs code and very quickly clicks the button. The log arg on send_job will stop the actual job from running in this case.
@ChrisLovering ChrisLovering removed the review: do not merge The PR can be reviewed but cannot be merged now label Jul 16, 2022
@ChrisLovering ChrisLovering merged commit f568618 into main Jul 16, 2022
@ChrisLovering ChrisLovering deleted the 3.11-snekbox branch July 16, 2022 22:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: utility Related to utility commands: (bot, eval, extensions, jams, reminders, snekbox, utils) t: enhancement Changes or improvements to existing features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants