Skip to content

Instantly share code, notes, and snippets.

@idimiter
Last active March 30, 2023 23:54
Show Gist options
  • Save idimiter/11fa77c624782aa69226bf93bd2dcec5 to your computer and use it in GitHub Desktop.
Save idimiter/11fa77c624782aa69226bf93bd2dcec5 to your computer and use it in GitHub Desktop.
#
# Fetches ark server info every minute and sends the info to a specific discord channel and message id
# Uses:
# discord(https://discordpy.readthedocs.io/) - pip install -U discord
# steam(https://github.com/ValvePython/steam) - pip install -U steam
#
import config # check config_sample.py
import time
import discord
from discord.ext import tasks, commands
from steam import game_servers as gs
import asyncio
bot = commands.Bot(command_prefix='!', intents=discord.Intents.default())
@tasks.loop(minutes = config.UPDATE_INTERVAL)
async def update_discord_info():
print("Updating discord...")
if (bot):
channel = bot.get_channel(config.DISCORD_INFO_CHANNEL_ID)
if channel:
ark_info = ""
message = await channel.fetch_message(config.DISCORD_INFO_MSG_ID)
for server in config.SERVERS:
ark_info += get_ark_info(server[0], server[1])
if server != config.SERVERS[-1]:
ark_info += "\n\n------------------------\n\n"
ark_info += f"\n\n*Last update: {time.strftime('%A %d %B %Y %H:%M:%S', time.localtime())}*"
await message.edit(content=ark_info)
def get_ark_info(server_ip, server_port):
qry = gs.query_master(f"\\appid\\{config.GAME_ID}\\gameaddr\\{server_ip}:{server_port}")
server_addr = next(qry,None)
if not server_addr:
return "**Status:** offline :stop_sign:"
server_info = gs.a2s_info(server_addr, force_goldsrc=False)
server_rules = gs.a2s_rules(server_addr)
players_online = []
res = ""
for p in gs.a2s_players(server_addr, timeout=1):
if p['name']:
plr = {}
plr['name'] = p['name']
plr['time'] = time.strftime("%H:%M:%S", time.gmtime(p['duration']))
players_online.append(plr)
res += "**Status:** online :white_check_mark:" + "\n"
res += "**Server Name:** " + server_info['name'] + "\n"
res += "**Map:** " + server_info['map'] + "\n"
res += "**Ingame day:** " + str(server_rules['DayTime_s']) + "\n"
res += "**Players:** " + str(len(players_online)) + "/" + str(server_info['max_players']) + "\n"
if len(players_online) > 0:
res += "\n```arm\n"
for player in players_online:
res += player['name'] + "\t" + player['time'] + "\n"
res += "```"
return res
@bot.event
async def on_message(msg):
if (msg.author.id == bot.user.id):
return
if (msg.content.lower().startswith("hi") or msg.content.lower().startswith("hello")):
await msg.reply("Hey, " + msg.author.display_name)
ark_info = ""
if msg.content.lower().startswith("status"):
for server in config.SERVERS:
ark_info += get_ark_info(server[0], server[1])
await msg.channel.send(ark_info)
await bot.process_commands(msg)
@bot.event
async def on_ready():
print(f"Bot has logged in as {bot.user}")
update_discord_info.start()
@bot.command(pass_context=True)
async def ping(ctx):
print(f" --- MESSSAGE --- ");
await ctx.send("pong")
bot.run(config.DISCORD_TOKEN)
DISCORD_TOKEN = "INSERT_DISCORD_BOT_TOKEN_HERE"
DISCORD_INFO_CHANNEL_ID = INSERT_DISCORD_CHANNEL_ID
DISCORD_INFO_MSG_ID = INSERT_DISCORD_POST_ID
SERVERS = [
[
"INSERT_SERVER_IP","INSERT_SERVER_PORT"
],
[
"INSERT_SERVER_IP", "INSERT_SERVER_PORT"
]
]
UPDATE_INTERVAL = 1.0
GAME_ID = 346110 #ARK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment