Skip to content

Instantly share code, notes, and snippets.

@TheNathanSpace
Last active October 8, 2021 18:10
Show Gist options
  • Save TheNathanSpace/6982cba4bdb1da29ea3f632887537e14 to your computer and use it in GitHub Desktop.
Save TheNathanSpace/6982cba4bdb1da29ea3f632887537e14 to your computer and use it in GitHub Desktop.
Hypixel Online Alert Bot

This bot sends a message to a Discord channel when someone connects or disconnects to Hypixel. This bot waits 5 seconds between cycles. If you have more than 10 players in your player_dict, you'll start running into Hypixel API trouble, since you can't make more than 120 requests per minute.

You'll need to install the PyPixel package from here.

You'll need to create a Discord bot. Be sure to put all the relevant secrets in secrets.py. Set the channel in line 29 of api_manager.py.

import secrets
import PyPixel
def get_state(online):
if online:
return "online!"
else:
return "offline."
class APIManager:
def __init__(self, bot):
self.hypixel = PyPixel.Hypixel(api_key = secrets.hypixel_key, clear_cache_after = 0)
self.bot = bot
async def update_names(self):
for name in secrets.player_dict:
try:
uuid = await self.hypixel.get_uuid(name)
except:
print(f"Could not get player UUID for {name}")
else:
print(f"Updated UUID for {name}")
secrets.player_dict[name]["uuid"] = uuid
async def get_online(self):
await self.check_dict(secrets.player_dict, PUT THE DISCORD CHANNEL HERE AS A RAW NUMBER)
async def check_dict(self, player_dict, channel_id):
for player_name in player_dict:
if player_dict[player_name]["uuid"] != "":
player_uuid = player_dict[player_name]["uuid"]
try:
player = await self.hypixel.get_player(player_uuid)
except:
player_dict[player_name]["changed"] = False
print(f"Could not get player info for {player_name}")
else:
old_state = player_dict[player_name]["online"]
online = player.last_logout < player.lastLogin
player_dict[player_name]["online"] = online
if old_state != online:
player_dict[player_name]["changed"] = True
else:
player_dict[player_name]["changed"] = False
for player_name in player_dict:
if player_dict[player_name]["changed"]:
online = player_dict[player_name]["online"]
channel = self.bot.get_channel(channel_id)
await channel.send(f"{player_name} is now {get_state(online)}")
import asyncio
import secrets
import discord
from discord.ext import commands
from api_manager import APIManager
intents = discord.Intents.default()
bot = commands.Bot(command_prefix = '!', description = "no ssn in gchat", intents = intents)
bot.remove_command("help")
api_manager = APIManager(bot)
loop = asyncio.get_event_loop()
forecast = loop.run_until_complete(api_manager.update_names())
async def main_loop():
await bot.wait_until_ready()
while not bot.is_closed():
await api_manager.get_online()
await asyncio.sleep(5)
print("Bot starting!")
bot.loop.create_task(main_loop())
bot.run(secrets.discord_key)
hypixel_key = "HYPIXEL API KEY HERE"
discord_key = "DISCORD BOT TOKEN HERE"
player_dict = {"Player1": {"uuid": "", "online": False},
"OtherPlayer": {"uuid": "", "online": False}
}
@MachineKillin
Copy link

Do you think you might add more to this? It’s a very good start and this could be much more customizable. I really like it!

@TheNathanSpace
Copy link
Author

I didn't have any plans to, because it works fine for my purposes. I just need it to send a message when a friend logs on. If you have any suggestions I'd be happy to look into implementing them, though!

@MachineKillin
Copy link

Oh, alright. My suggestions are:
Adding commands to easily edit which players you want to track; making it easier to change things. Also adding it so you can get notified when a player plays a certain game. have it track a specific game. I would try to implement these things by myself but i’m not familiar with python yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment