Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Discord - Send messages from a channel to my website in real time

I currently have a python/django platform and a discord community. On my discord server there is a channel "announcements". I would just like that when a message is published in this channel, it goes up to my website in real time. This is in order to convert it into a notification.

Currently I managed to upload the messages from the channel to my site in a simple way but not in real time:

def discord_get_last_message_id():
message_id = 0
try:
    message_id = Notification.objects.latest('id').discord_message_id
except:
    pass
return message_id

def get_channel_messages():

#load last id discord message in DB
last_message_id = discord_get_last_message_id()
#Base route
route = "/channels/"+ DISCORD_CHANNEL_ANNONCES_ID +"/messages"

#if first time to DB, load just one item
if last_message_id == 0:
    add = "?limit=1"
else:
    add = "?after="+last_message_id
route = route + add

data,error_message = request_discord('GET',route)

print(data)

def request_discord(method,url_access,body={}):
data =''
#Call token
error_message = ''
access_token = discord_get_token()

#Call request
headers = {'Authorization': access_token}

body = body
if method=="GET":
    result = requests.get(DISCORD_BASE_URI + url_access, headers=headers)
else:
    result = requests.post(DISCORD_BASE_URI + url_access, headers=headers,data=body)

#Check result
if result.status_code != 200 and result.status_code != 201:
    error_message = "Impossible de d'obtenir un resultat erreur: " + str(result.status_code)
else:
    data = result.json()

return data,error_message

def discord_get_token():
  return DISCORD_ANNONCES_CHANNEL_TOKEN 

I'm trying to understand how discord websockets work but I have the impression that it's made to communicate with a bot only.

My question is, which way should I go to get the messages from my discord channel to my website in real time? Do I have to go through a bot?

NOTE: the goal is to get his messages to make notifications on my platform.

Thanks for your answers !

like image 336
Camille Colvray Avatar asked Oct 26 '22 13:10

Camille Colvray


1 Answers

To answer your question:

My question is, which way should I go to get the messages from my discord channel to my website in real time? Do I have to go through a bot?

The best way would be to use a bot. This is the simplest, yet best way to do accomplish what you want. You could use a on_message event to get messages when they are sent. Then you could use that message and update your website. An example of how to do this is:

@bot.event
async def on_message(message):
     message_content = message.content
     return

You can do whatever you want with message_content. For your purpose you might want to store it in a database.

For the website side, you could use JavaScript to get the messages from the DB and update the HTML accordingly.

like image 84
mcdonalds291 Avatar answered Nov 02 '22 09:11

mcdonalds291