An IPC extension allowing for the communication between a discord.py bot and an asynchronous web-framework (i.e. Quart or aiohttp.web)
Project description
An IPC extension allowing for the communication between a discord.py bot and an asynchronous web-framework (i.e. Quart or aiohttp.web)
Installation
Installation can be complete through github only:
python -m pip install -U git+https://github.com/lganwebb/discord-ext-ipc
Usage
One of the most basic programs you can make is a simple guild counter web-page. An example using Quart:
# BOT FILE
import discord
from discord.ext import commands
# Our bot will be the server we make requests to in order to get data from it.
from discord.ext.ipc import Server
class Bot(commands.Bot):
"""Main bot class"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
async def on_ipc_ready(self):
"""Event dispatched upon the IPC being ready"""
print("IPC ready")
async def on_ready(self):
"""Event dispatched upon our discord bot being ready"""
print("Bot ready")
bot = Bot(command_prefix="!", case_insensitive=True)
bot_ipc = Server(bot, "localhost", 8765, "secret_key")
# ipc.server.Server takes four arguments: the bot object, the port to run the IPC on, and a secret key used to authenticate client connections (seen in the web server file).
@bot_ipc.route() # if no name is supplied in ipc.server.Server.route, the function name will become the route name.
async def get_guild_count(data):
"""This route named get_guild_count will return the amount of guilds our bot is in"""
return len(bot.guilds)
if __name__ == "__main__":
bot_ipc.start() # ipc.server.Server.start will begin the IPC
bot.run("TOKEN") # run the bot as usual
# WEB SERVER FILE
from quart import Quart
from client import Client
app = Quart(__name__)
web_ipc = Client(secret_key="my_auth_token")
@app.route("/")
async def show_guilds():
guild_count = await app.ipc_node.request("get_guild_count") # Make a request to get the bot's IPC get_guild_count route.
return str(guild_count) # return the data sent to us.
@app.before_first_request
async def before():
app.ipc_node = await web_ipc.discover() # discover IPC Servers on your network
if __name__ == "__main__":
app.run()
For support join the Ext-Creators Discord Server.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for discord_ext_ipc-1.1.1a0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | deaf2bad7554c34088463fe5b77e59fefbaa5c18daeef46d685482a995545fbb |
|
MD5 | 5032bb394909e220ccfb62e770214e4c |
|
BLAKE2b-256 | bfde7afe22a40cbfc84b4dcabc702640bb79e7476e1d369ba5bcedee54abc2c7 |