misc.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # mautrix-instagram - A Matrix-Instagram puppeting bridge.
  2. # Copyright (C) 2022 Tulir Asokan
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. from mautrix.bridge.commands import HelpSection, command_handler
  17. from .. import puppet as pu
  18. from .typehint import CommandEvent
  19. SECTION_MISC = HelpSection("Miscellaneous", 40, "")
  20. @command_handler(
  21. needs_auth=True,
  22. management_only=False,
  23. help_section=SECTION_MISC,
  24. help_text="Search for Instagram users",
  25. help_args="<_query_>",
  26. )
  27. async def search(evt: CommandEvent) -> None:
  28. if len(evt.args) < 1:
  29. await evt.reply("**Usage:** `$cmdprefix+sp search <query>`")
  30. return
  31. resp = await evt.sender.client.search_users(" ".join(evt.args))
  32. if not resp.users:
  33. await evt.reply("No results :(")
  34. return
  35. response_list = []
  36. for user in resp.users[:10]:
  37. puppet = await pu.Puppet.get_by_pk(user.pk, create=True)
  38. await puppet.update_info(user, evt.sender)
  39. response_list.append(
  40. f"* [{puppet.name}](https://matrix.to/#/{puppet.mxid})"
  41. f" ([@{user.username}](https://instagram.com/{user.username}))"
  42. )
  43. await evt.reply("\n".join(response_list))