COSMO
AND THE
UNIVERSE

Software

LoL death messages bot

Recently I discovered that every time you play league of legends there is a local API endpoint that stores a bunch of useful data on the game you are currently playing. With this data I've made a little discord bot that sends customisable death messages to the chat everytime someone on your team dies. The set up is pretty easy and is as follows.

  1. Create a discord webhook in the Integrations part of the server menu, copy the webhook URL
  2. Then pip install the relevant libraries (you can see these in the code)
  3. Add your custom messages to the messages array
  4. Run the script before your next league game!

Here is the code (it's a bit sketchy)


import requests
import random
import time

# will need to pip install these to get it working
import urllib3
from dhooks import Webhook

# set up a discord bot and paste the webhook between the ''
hook = Webhook('Your webhook here')

# had to do this to get around some riot api weirdness
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
BASE_URL = "https://127.0.0.1:2999/liveclientdata"

# these are just some example messages, 
# you have access to the player who got killed with {victim} 
# and the player who killed them {killer}, make up some funny ones
messages = [
    "{victim} got creative with it",
    "{victim} fed the enemy team",
    "{victim}'s time came",
    "{victim} was nuked by {killer}"
]

def safe_get(endpoint):
    try:
        r = requests.get(f"{BASE_URL}/{endpoint}", 
                        verify=False, timeout=1)
        if r.status_code == 200:
            return r.json()
    except:
        pass
    return None

def game_watcher():
    seen_events = set()
    print("Waiting for game to start...")

    while True:
        data = safe_get("allgamedata")
        if data:
            break
        time.sleep(2)

    # get the players on your team for later use
    my_name = data["activePlayer"]["summonerName"]
    my_team = None
    for p in data["allPlayers"]:
        if p["summonerName"] == my_name:
            my_team = p["team"]
            break

    allies = set()
    for p in data["allPlayers"]:
        if p["team"] == my_team:
            allies.add(p["riotIdGameName"])

    # debugging stuff
    #print(f"Game detected! Team: {my_team}")
    #print(f"Allies: {allies}")
    #print("Watching for events...")

    while True:
        events = safe_get("eventdata")
        if not events:
            print("Game ended.")
            return

        for e in events.get("Events", []):
            if e["EventID"] in seen_events:
                continue
            seen_events.add(e["EventID"])

            if e["EventName"] == "GameEnd":
                print(f"Game ended: {e.get('Result', 'Unknown')}")
                return

            if e["EventName"] == "ChampionKill":
                killer = e.get("KillerName", "Unknown")
                victim = e.get("VictimName", "Unknown")

                #print(f"{killer} killed {victim}")

                # only go off if someone from your team dies
                if victim not in allies:
                    continue

                timestamp = e.get("EventTime", 0)
                minutes = int(timestamp // 60)
                seconds = int(timestamp % 60)

                msg = random.choice(messages).format(killer=killer, 
                                                    victim=victim)
                hook.send(f"{msg} at {minutes}:{seconds:02d}")

        time.sleep(2)

game_watcher()