Git Product home page Git Product logo

discord-ext-ipc's Introduction

discord-ext-ipc

An IPC extension allowing for the communication between a discord.py bot and an asynchronous web-framework (i.e. Quart or aiohttp.web)

Installing

As with other extensions, instillation is through git

python -m pip install -U git+https://github.com/lganwebb/discord-ext-ipc

Basic Usage / Getting started

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 discord.ext.ipc import Client

app = Quart(__name__)
web_ipc = Client("localhost", 8765, "secret_key")

@app.route("/")
async def show_guilds():
    guild_count = await web_ipc.request("get_guild_count") # Make a request to get the bot's IPC get_guild_count route.

    return guild_count # return the data sent to us.

if __name__ == "__main__":
    app.run()

discord-ext-ipc's People

Contributors

lgaan avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.