Skip to content

feat: allow file uploads for cvar, uvar, svar, gvar and alias, servalias, snippet, servsnippet#2207

Merged
codybanman merged 9 commits intoavrae:nightlyfrom
7he4lph4:cvar-file-upload
Oct 28, 2025
Merged

feat: allow file uploads for cvar, uvar, svar, gvar and alias, servalias, snippet, servsnippet#2207
codybanman merged 9 commits intoavrae:nightlyfrom
7he4lph4:cvar-file-upload

Conversation

@7he4lph4
Copy link
Copy Markdown
Contributor

@7he4lph4 7he4lph4 commented Aug 12, 2025

Summary

  • Extend gvar, alias, servalias, snippet, and servsnippet create/edit commands to accept file attachments, following the same pattern as cvar, uvar, and svar.

From PR #2206:

Read file attachments if no value is provided so that longer variables than a discord message can be set. Avrae already provides longer variables as a text file anyways, so this just allows full support in both directions.

Depends on PR #2206 - This PR builds on @LazyDope's cvar/uvar/svar file upload implementation. Please merge #2206 first to avoid conflicts. (both PRs merged into this now)

Changelog Entry

!cvar, !uvar, !svar, !gvar, !alias, !servalias, !snippet, and !servsnippet now support uploading file attachments.

Checklist

PR Type

  • This PR is a code change that implements a feature request.
  • This PR fixes an issue.
  • This PR adds a new feature that is not an open feature request.
  • This PR is not a code change (e.g. documentation, README, ...)

Other

  • This PR is a breaking change (e.g. methods or parameters removed/renamed)
  • If code changes were made then they have been tested.
  • I have updated the documentation to reflect the changes.

@LazyDope
Copy link
Copy Markdown
Contributor

I believe the changes to the read_file_from_message function will break viewing cvars, uvars, and svars.

Copy link
Copy Markdown
Contributor

@LazyDope LazyDope left a comment

Choose a reason for hiding this comment

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

if not ctx.message.attachments:
        raise IndexError("No attachments")

These lines are unnecessary, IndexError is already raised by ctx.message.attachments[0] in this scenario

@7he4lph4
Copy link
Copy Markdown
Contributor Author

if not ctx.message.attachments:
        raise IndexError("No attachments")

These lines are unnecessary, IndexError is already raised by ctx.message.attachments[0] in this scenario

Yea honestly, woulda removed that if I wasn't fixing it in a hurry, other than that, any other suggestions? I'll make this point to your fork?

@Infinidoge
Copy link
Copy Markdown
Contributor

I'd personally keep the check. Raising the index error ourselves allows it to return a better error message than "Index out of bounds for attachments".

@7he4lph4
Copy link
Copy Markdown
Contributor Author

I'd personally keep the check. Raising the index error ourselves allows it to return a better error message than "Index out of bounds for attachments".

The IndexError happens naturally at ctx.message.attachments[0]like lazydope said and is being properly handled in _get_value_or_file()

@LazyDope
Copy link
Copy Markdown
Contributor

LazyDope commented Aug 13, 2025

I'd personally keep the check. Raising the index error ourselves allows it to return a better error message than "Index out of bounds for attachments".

We're not actually returning this error either way, it needs to be converted into an InvalidArgument error before being raised any further to avoid the error appearing as a bot error rather than an argument error.

I suppose explicitly raising the error could make it clearer to any other readers/users of the function that the IndexError is intetional and meant to caught.

@7he4lph4
Copy link
Copy Markdown
Contributor Author

I suppose explicitly raising the error could make it clearer to any other readers/users of the function that the IndexError is intetional and meant to caught.

@LazyDope Good point. Also, wrt catching all Exceptions in cvar/svar/uvar. Won't it be better to only catch IndexError there as well? And let other errors go through, coz say, the Discord CDN fails during a file upload, we won't be sending the "Error in Discord API. Try again" msg Avrae usually shows right? And all other sys errs go through as well.

@LazyDope
Copy link
Copy Markdown
Contributor

I suppose explicitly raising the error could make it clearer to any other readers/users of the function that the IndexError is intetional and meant to caught.

@LazyDope Good point. Also, wrt catching all Exceptions in cvar/svar/uvar. Won't it be better to only catch IndexError there as well? And let other errors go through, coz say, the Discord CDN fails during a file upload, we won't be sending the "Error in Discord API. Try again" msg Avrae usually shows right? And all other sys errs go through as well.

My initial thought was if fetching the attachment failed then to just continue as if it didn't exist, however you're probably right that we should only catch IndexErrors

@LazyDope
Copy link
Copy Markdown
Contributor

Also, what are your thoughts on merging the PRs into this one?

@7he4lph4
Copy link
Copy Markdown
Contributor Author

Also, what are your thoughts on merging the PRs into this one?

Sure yea, that works too

@7he4lph4
Copy link
Copy Markdown
Contributor Author

It's already technically merged tho lol, this PR contains all your commits too

@LazyDope
Copy link
Copy Markdown
Contributor

It's already technically merged tho lol, this PR contains all your commits too

Just update the title and description to clarify that it covers both features.

- style: also added thousands separators to size limit err msgs
@7he4lph4 7he4lph4 changed the title feat: file upload support for gvar create, edit feat: allow file uploads for cvar, uvar, svar, and gvar Aug 15, 2025
except InvalidArgument as e:
raise e
except Exception:
value = await _get_value_or_file(ctx, None, CVAR_FILE_SIZE_LIMIT, allow_empty=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.

With the addition of this helper function, we could probably just move this outside of the if value is None check, since that check is a bit redundant

Copy link
Copy Markdown
Contributor Author

@7he4lph4 7he4lph4 Aug 18, 2025

Choose a reason for hiding this comment

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

Right, but the code readability is crystal clear here with that flow, making it self documenting. Removing that check would make it slightly obfuscated.

I'm open to removing tho if that tradeoff is fine.

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.

I agree with @LazyDope. The name _get_value_or_file() is self documenting and cleans a level of nesting in the different places it's called

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.

Sure, done. This was just me being overtly KISS at the cost of a little DRYness.

Copy link
Copy Markdown
Contributor

@codybanman codybanman left a comment

Choose a reason for hiding this comment

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

This looks great and works well for the various cases I tested. As I mentioned in the thread, I agree with @LazyDope’s suggestion on the use of use _get_value_or_file(). Once that change is in, we can test it out on nightly.

except InvalidArgument as e:
raise e
except Exception:
value = await _get_value_or_file(ctx, None, CVAR_FILE_SIZE_LIMIT, allow_empty=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.

I agree with @LazyDope. The name _get_value_or_file() is self documenting and cleans a level of nesting in the different places it's called

@7he4lph4 7he4lph4 changed the title feat: allow file uploads for cvar, uvar, svar, and gvar feat: allow file uploads for cvar, uvar, svar, and gvar and alias, servalias, snippet, servsnippet Oct 16, 2025
@7he4lph4 7he4lph4 changed the title feat: allow file uploads for cvar, uvar, svar, and gvar and alias, servalias, snippet, servsnippet feat: allow file uploads for cvar, uvar, svar, gvar and alias, servalias, snippet, servsnippet Oct 17, 2025
@codybanman codybanman merged commit b58381c into avrae:nightly Oct 28, 2025
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.

4 participants