Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add mixin of commands on ctx #15

Merged
merged 4 commits into from
May 21, 2024

Conversation

carafelix
Copy link
Member

@carafelix carafelix commented May 2, 2024

Summary

  • fix: localization command names being empty #14 is built into this pr (please check that out first)
  • changed the setMyCommands function to be able to merge SetMyCommandsParams from two or more different sources, based on the fact that toSingleScopeArgs group them by language. Merge function its basically a reducer that concat lang.commands arrays of the same language and returns an array of setMyCommandsParams. It does not break scoping since it's performed after the toSingleScopeArgs function. If an empty array is passed, it does not throw, if only one Commands instance is passed it treats it ok.
    • the name of the result variable might be better to change it to acc_Array or something in those lines
  • add info jsdoc info about lang's iso codes (an enum would be ideal 👍 )

PD: I closed the other PR since it was a little messy on the branch managment end, this one is ok.
PD2: I can refactor this into a MixedCommands class that extends the Commands Class if that suits more the style.
Another way would be to add this as a method to the Commands Class, but I don't know good of an idea would be to mix them before toSingleScopeArgs in a method like commands.mergeWith(otherCommands) because it would effectively duplicate commands registration if someone use it on let say commands A and B, and register both into bot.use, registering all at once on A (B was merged into A), and then re-register B commands. Even in that case they would simply overwrite themselves I think so, no?

eg:

pr webm

snippet from the gif (localization in the admin command... 🥴)

// localized start command //
userCommands.command("end", "end", async (c) => {
  await c.setMyCommands(userCommands)
}).localize("es", "fin", "finaliza")
  .addToScope(
    { type: "all_private_chats" },
    async (ctx) => {
      ctx.reply(`finalizado`)
      await ctx.setMyCommands(userCommands)
    },
  );
adminCommands.command("admin", "i am the admin", async (c) => {
  await c.setMyCommands(userCommands, adminCommands);
}).localize("es", "start", "el admin soy yo");

Copy link

codecov bot commented May 2, 2024

Codecov Report

Attention: Patch coverage is 71.42857% with 12 lines in your changes are missing coverage. Please review.

Project coverage is 79.88%. Comparing base (63bc0fa) to head (2c2934f).
Report is 2 commits behind head on main.

Current head 2c2934f differs from pull request most recent head 74a2f5a

Please upload reports for the commit 74a2f5a to get more accurate results.

Files Patch % Lines
src/context.ts 65.71% 12 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main      #15       +/-   ##
===========================================
+ Coverage   68.13%   79.88%   +11.74%     
===========================================
  Files           7        7               
  Lines         408      502       +94     
  Branches       66       78       +12     
===========================================
+ Hits          278      401      +123     
+ Misses        128      100       -28     
+ Partials        2        1        -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@carafelix
Copy link
Member Author

If the merge commands function is not to clear on what it's doing, just let me know, I can refactor it and can add it as a commands method

@carafelix
Copy link
Member Author

Removed pollution from commit history

@carafelix carafelix force-pushed the addMixinOfCommands branch 2 times, most recently from 41a7eaf to dc3586c Compare May 17, 2024 19:16
@carafelix
Copy link
Member Author

Test added for merging

roziscoding
roziscoding previously approved these changes May 18, 2024
Copy link
Contributor

@roziscoding roziscoding left a comment

Choose a reason for hiding this comment

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

Awesome work. Thank you so much!

roziscoding
roziscoding previously approved these changes May 18, 2024
@carafelix
Copy link
Member Author

carafelix commented May 19, 2024

Is something missing for this to be merge? I can work on #16 if you think its appropriated

@roziscoding
Copy link
Contributor

image
GitHub won't let me merge for some reason and I haven't had the time to do a local merge.
Could you try do rebase this branch on the main branch of the official repo and do a push --force-with-lease, please?

@carafelix
Copy link
Member Author

carafelix commented May 20, 2024

Now it should be good. Merging main into this branch resulted in duplicate commits, since I merged the fixLocalization branch into this one before. Got a little messy along the way, but it's clean and okay now. (I re-checked that it's working on the telegram client)

@carafelix carafelix reopened this May 20, 2024
@roziscoding roziscoding merged commit 7ff2db2 into grammyjs:main May 21, 2024
6 checks passed
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.

None yet

2 participants