6

So I'm making a Discord Bot that posts when a person goes live on Twitch.tv. At the moment I have a Python program that runs the bot and a program that runs a mini server to receive the data from the Twitch server(webhook). I am unsure on how to pass on the data I receive from my server to the discord bot. Both programs have to be running at the same time.

DiscordBot

import discord


client = discord.Client()




async def goes_live(data):
    print(data)
    print('Going Live')
    msg = '--- has gone live'
    await client.send_message(discord.Object(id='---'), msg)


@client.event
async def on_message(message):
    if message.author == client.user:
        return

    message.content = message.content.casefold()


@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')


client.run('---')

Web server

import web

urls = ('/.*', 'hooks')

app = web.application(urls, globals())


class hooks:

    def POST(self):
        data = web.data()
        print("")
        print('DATA RECEIVED:')
        print(data)
        print("")

        return 'OK'

    def GET(self):
        try:
            data = web.input()
            data = data['hub.challenge']
            print("Hub challenge: ", data)
            return data
        except KeyError:
            return web.BadRequest



if __name__ == '__main__':
    app.run()
2
  • The relevant part of the documentation: Interprocess Communication and Networking. Note that discord.py already uses asyncio, so that's probably a good place to start looking. Commented Sep 7, 2018 at 15:13
  • You have 2 options: 1. Make your webhook server post discord message. 2. (I do not recommend using this method because it makes the application more hard to maintain) pub sub with redis/rabbitmq/etc.. Do not use process based approach because you will be limited to a single machine. Commented Dec 30, 2020 at 11:08

3 Answers 3

3

Since both your programs are in python, and if they are related to each other enough that they always run together, you can simply use multiprocessing module: have each one of the programs run as a multiprocessing.Process instance, and give each one of them one end of a multiprocessing.Pipe, that way you can exchange informations between processes.

The architecture would look something like that main.py:

# main.py
from multiprocessing import Process, Pipe
import program_1
import program_2

program_1_pipe_end, program_2_pipe_end = Pipe()

process_1 = Process(
    target = program_1.main,
    args = (program_1_pipe_end,)
    )

process_2 = Process(
    target = program_2.main,
    args = (program_2_pipe_end,)
    )

process_1.start()
process_2.start()
#Now they are running
process_1.join()
process_2.join()
# program_1.py and program_2.py follow this model

# [...]

# instead of if __name__ == '__main__' , do:
def main(pipe_end):
    # and use that pipe end to discuss with the other program
    pass

You can find the Pipe documentation here, (inside multiprocessing documentation).

Sign up to request clarification or add additional context in comments.

Comments

0

Are the bot and the mini server running on the same machine? In that case you just make the server writing a file to a location where the bot can access and check periodically.

1 Comment

Maybe expand your answer a bit to show how that could be done.
0

How about using a Flask server that handles the communication between the 2 programs. You could define custom endpoints that would be able to grab the data as well as send it to the discord script.

@app.route('/ep1', methods = ['GET','POST'])
def ep1():
    if request.method == 'POST':
        #do something for a POST request. 
    else:
        #do something for a GET request.

You can use this structure to construct something that listens for changes and then publishes those to the discord bot. Also hosting this server on heroku is something that you might want to consider

1 Comment

Sending the data to the discord bot is what the question's about, and you sort of gloss over it here. How exactly are you proposing to do that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.