4. Example Usages#

Here are some ways to use nextcord-ext-ipc in your own bot! For GitHub based examples, see the examples directory.

4.1. Basic IPC Implementation#

Your main bot file

 1import nextcord
 2from nextcord.ext import commands, ipc
 3
 4
 5class MyBot(commands.Bot):
 6    def __init__(self, *args, **kwargs):
 7        super().__init__(*args, **kwargs)
 8
 9        self.ipc = ipc.Server(self, secret_key="my_secret_key")  # create our IPC Server
10
11    async def on_ready(self):
12        """Called upon the READY event"""
13        print("Bot is ready.")
14
15    async def on_ipc_ready(self):
16        """Called upon the IPC Server being ready"""
17        print("IPC is ready.")
18
19    async def on_ipc_error(self, endpoint, error):
20        """Called upon an error being raised within an IPC route"""
21        print(endpoint, "raised", error)
22
23
24my_bot = MyBot(command_prefix="!", intents=nextcord.Intents.all())
25
26
27@my_bot.ipc.route()
28async def get_member_count(data):
29    guild = my_bot.get_guild(
30        data.guild_id
31    )  # get the guild object using parsed guild_id
32
33    return guild.member_count  # return the member count to the client
34
35
36if __name__ == "__main__":
37    my_bot.ipc.start()  # start the IPC Server
38    my_bot.run("TOKEN")

Your bot’s webserver file

 1from quart import Quart
 2from nextcord.ext import ipc
 3
 4
 5app = Quart(__name__)
 6ipc_client = ipc.Client(
 7    secret_key="my_secret_key"
 8)  # secret_key must be the same as your server
 9
10
11@app.route("/")
12async def index():
13    # get the member count of the guild with the id 12345678
14    member_count = await ipc_client.request("get_member_count", guild_id=12345678)
15
16    # display member count
17    return str(member_count)
18
19
20
21if __name__ == "__main__":
22    app.run()

4.2. Cog based IPC implementation#

The bot file:

 1import nextcord
 2from nextcord.ext import commands, ipc
 3
 4
 5class MyBot(commands.Bot):
 6    def __init__(self, *args, **kwargs):
 7        super().__init__(*args, **kwargs)
 8
 9        self.ipc = ipc.Server(self, secret_key="my_secret_key")  # create our IPC Server
10
11        self.load_extension("cogs.ipc")  # load the IPC Route cog
12
13    async def on_ready(self):
14        """Called upon the READY event"""
15        print("Bot is ready.")
16
17    async def on_ipc_ready(self):
18        """Called upon the IPC Server being ready"""
19        print("IPC is ready.")
20
21    async def on_ipc_error(self, endpoint, error):
22        """Called upon an error being raised within an IPC route"""
23        print(endpoint, "raised", error)
24
25
26my_bot = MyBot(command_prefix="!", intents=nextcord.Intents.all())
27
28
29if __name__ == "__main__":
30    my_bot.ipc.start()  # start the IPC Server
31    my_bot.run("TOKEN")

The cog file:

 1from nextcord.ext import commands, ipc
 2
 3
 4class IpcRoutes(commands.Cog):
 5    def __init__(self, bot):
 6        self.bot = bot
 7
 8    @ipc.server.route()
 9    async def get_member_count(self, data):
10        guild = self.bot.get_guild(
11            data.guild_id
12        )  # get the guild object using parsed guild_id
13
14        return guild.member_count  # return the member count to the client
15
16
17def setup(bot):
18    bot.add_cog(IpcRoutes(bot))

The webserver file:

 1from quart import Quart
 2from nextcord.ext import ipc
 3
 4
 5app = Quart(__name__)
 6ipc_client = ipc.Client(
 7    secret_key="my_secret_key"
 8)  # secret_key must be the same as your server
 9
10
11@app.route("/")
12async def index():
13    member_count = await ipc_client.request(
14        "get_member_count", guild_id=12345678
15    )  # get the member count of server with ID 12345678
16
17    return str(member_count)  # display member count
18
19
20if __name__ == "__main__":
21    app.run()