diff --git a/examples/broadcast_exec/__init__.py b/examples/broadcast_exec/__init__.py new file mode 100644 index 0000000..9efc13e --- /dev/null +++ b/examples/broadcast_exec/__init__.py @@ -0,0 +1,21 @@ +# MIT License +# +# Copyright (c) 2021 TrigonDev +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. diff --git a/examples/broadcast_exec/__main__.py b/examples/broadcast_exec/__main__.py new file mode 100644 index 0000000..e0d5411 --- /dev/null +++ b/examples/broadcast_exec/__main__.py @@ -0,0 +1,38 @@ +# MIT License +# +# Copyright (c) 2021 TrigonDev +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +import sys + +from .brain import run as runbrain +from .server import run as runserver + +if __name__ == "__main__": + try: + t = sys.argv[1] + except IndexError: + print('Please specify either "brain" or "server":') + print("\tpython -m examples.basic brain/server") + exit() + if t == "brain": + runbrain() + elif t == "server": + runserver() diff --git a/examples/broadcast_exec/brain.py b/examples/broadcast_exec/brain.py new file mode 100644 index 0000000..39f041e --- /dev/null +++ b/examples/broadcast_exec/brain.py @@ -0,0 +1,34 @@ +# MIT License +# +# Copyright (c) 2021 TrigonDev +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +from hikari_clusters import Brain + + +def run() -> None: + Brain( + host="localhost", + port=8765, + token="ipc token", + total_servers=1, + clusters_per_server=2, + shards_per_cluster=3, + ).run() diff --git a/examples/broadcast_exec/server.py b/examples/broadcast_exec/server.py new file mode 100644 index 0000000..a5182f5 --- /dev/null +++ b/examples/broadcast_exec/server.py @@ -0,0 +1,76 @@ +# MIT License +# +# Copyright (c) 2021 TrigonDev +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +# When you send `!exec `, the code will be sent to all clusters. Try +# running `print(1)` with multiple servers. + +from hikari import GatewayBot, GuildMessageCreateEvent + +from hikari_clusters import Cluster, ClusterLauncher, Server, payload +from hikari_clusters.commands import CommandGroup + + +class MyBot(GatewayBot): + _cluster: Cluster + + def __init__(self) -> None: + super().__init__(token="discord token") + + self.listen(GuildMessageCreateEvent)(self.on_message) + + async def on_message(self, event: GuildMessageCreateEvent) -> None: + if not event.content: + return + if event.content.startswith("!exec"): + await self.cluster.ipc.send_command( + self.cluster.ipc.cluster_uids, + "exec_code", + {"code": event.content[6:]}, + ) + + @property + def cluster(self) -> Cluster: + return self._cluster + + @cluster.setter + def cluster(self, value: Cluster) -> None: + # when the cluster is set, we can include our commands. + self._cluster = value + self._cluster.ipc.commands.include(COMMANDS) + + +COMMANDS = CommandGroup() + + +@COMMANDS.add("exec_code") +async def exec_code(pl: payload.COMMAND) -> None: + assert pl.data.data is not None + exec(pl.data.data["code"]) + + +def run() -> None: + Server( + host="localhost", + port=8765, + token="ipc token", + cluster_launcher=ClusterLauncher(MyBot), + ).run()