Browse Category

Python

Simple Discord Messages in Python

This is a quick and easy way to send a bot message on Discord using Python, without the discord.py library, by simply doing a POST request with the requests library.

First you’ll need your bot’s token, which you can find from: https://discord.com/developers/applications

Next you’ll need the ID of the text channel where you want to send the messages to be sent. Simply right click the channel name and click “Copy ID”.

With those 2 values copied, paste them into the respective variables in the code below.

import requests

BOT_TOKEN = 'xxxxx'
CHANNEL_ID = '#####'

message = 'hello world!'

requests.post(
    url='https://discord.com/api/v8/channels/{}/messages'.format(CHANNEL_ID),
    json={'content': message},
    headers={
        'authorization': 'Bot {}'.format(BOT_TOKEN),
        'content-type': 'application/json',
    }
)

And that’s it! Execute the python script and check the Discord channel for the message!

Integrating Counter-Strike: Global Offensive with Philips Hue

csgo-c4-hue

Recently, Valve had released their Counter-Strike: Global Offensive game state integration for developers to use. This allows the game client to send all game data, such as the player’s health, guns, ammo, frags, etc. to a HTTP POST endpoint server.

After seeing the PGL Arena Effects in the DreamHack Cluj-Napoca CS:GO tournament a couple of months ago, I decided to make full use of my Philips Hue lights in my room by recreating the C4 bomb lighting effects.

Coding

Since I’ve been coding in Python a lot recently, I stuck with this language for this project. First, I had to create the HTTP server to receive the POST game state data, so I used the Flask microframework. I launched the Flask server and joined a game in CS:GO, and immediately I was seeing tons of JSON being received in the terminal window. All I really needed was the C4 bomb information, which I was able to find in data[’round’][‘bomb’]. By checking that key, I noticed there were 3 values if the key existed: planted, exploded, and defused. If the bomb isn’t planted, there won’t be a bomb key in the round object at all. So after getting the bomb status, I made the server write the result to a file conveniently named bomb_status.

Now I had to do the fun part — making my Philips Hue lights turn on and change colors. I went step by step with the Philips Hue API Getting Started guide, and it was very easy to follow and understand the RESTful interface. Once I understood how to control the Hue lights, I wrote a 2nd Python script for reading the bomb_status file. In this 2nd script, I have a while loop that runs indefinitely, with a 250ms wait time in between each loop. In each loop iteration, it gets the bomb status and goes through a series of if-statements to determine what to do. Here’s the run down of it:

  • if bomb is planted: make each light blink red 1-by-1.
  • if bomb is planted and plant time has been >= 30 seconds: make all lights blink red every second.
  • if bomb is exploded: make all lights turn orange.
  • if bomb is defused: make all lights turn blue.
  • if bomb has no status: make all lights turn white.

And that’s basically it.

Final Result

My Python code is now on GitHub:
https://github.com/doobix/csgo-c4-hue

I have created a Node.js version of the same code, also on GitHub:
https://github.com/doobix/csgo-c4-hue-node

A video of the script in action is uploaded to YouTube:
https://www.youtube.com/watch?v=QBdI54MHB-k

Reddit thread:
https://redd.it/3wjpgg

csgo-c4-hue