-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
79 lines (64 loc) · 3.26 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#TODO - OUTLINE
# we need to provide ids in here, have them hard coded in a dict like 'ffa': '124124' etc.
# need to support multiple bots soon, turn creds.file into json for each thing, like 'ffa'?
#server to test: Black Ops 2 FFA (Plutonium) 203.123.120.184:4976
import discord
from discord.enums import ActivityType, Status
from discord.ext import commands, tasks
from discord.ext.tasks import loop
import asyncio
import requests
import os
try:
with open(os.path.dirname(os.path.realpath(__file__)) + r"\creds.file") as creds:
BOT_TOKEN = creds.read()
except OSError:
print("Add a creds.file with your Discord Bot's token")
os._exit()
class IW4MDiscordClient(commands.Bot):
lastMapName = ""
serverInfo = { 'map': '', #the maps file name, eg. nuketown_2020, for thumbnails
'mapname': '', #the Alias, eg. Nuketown 2025
'players': '',
'maxplayers': '' }
async def on_ready(self):
print('Logged in as {0}'.format(self.user))
self.loop.create_task(infoTimer(self))
#self.getInfo()
#await self.updateInfo(self.serverInfo['mapname'], self.serverInfo['players'], self.serverInfo['maxplayers'])
async def updateInfo(self, map, mapName, playerCount, maxPlayerCount):
infoString = "{} {}/{}".format(mapName, playerCount, maxPlayerCount)
print('Updating info: \'{}\''.format(infoString))
#set the presence
#if no players, change status to idle
await self.change_presence(activity=discord.Activity(name=infoString, type=ActivityType.playing), status=(Status.idle if playerCount == 0 else Status.online))
#check if we have to update profile picture
if (map != self.lastMapName):
print('Map changed, changing profile picture to current map')
self.lastMapName = self.serverInfo['map']
try:
mapImage = open(os.path.dirname(os.path.realpath(__file__)) + r"\assets\map_thumb\{}.png".format(map), 'rb')
except OSError as e:
print("Failed to open map thumbnail \'{}.png\', using default".format(map))
#lets hope we dont fail this
mapImage = open(os.path.dirname(os.path.realpath(__file__)) + r"\assets\map_thumb\default.png", 'rb')
await client.user.edit(avatar=mapImage.read())
async def getInfo(self):
serverStatus = requests.get('http://localhost:1624/api/status')
server = next((server for server in serverStatus.json() if server['id'] == 1270014976), None)
if (server is not None):
self.serverInfo['map'] = server['map']['name']
self.serverInfo['mapname'] = server['map']['alias']
self.serverInfo['players'] = server['currentPlayers']
self.serverInfo['maxplayers'] = server['maxPlayers']
return True
return False
client = IW4MDiscordClient(command_prefix="$")
async def infoTimer(self):
await client.wait_until_ready()
print("Updating bot info")
while not client.is_closed():
await self.getInfo()
await self.updateInfo(self.serverInfo['map'], self.serverInfo['mapname'], self.serverInfo['players'], self.serverInfo['maxplayers'])
await asyncio.sleep(30)
client.run(BOT_TOKEN)