-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
74 lines (62 loc) · 2.15 KB
/
app.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
# Kenna 21.07.2018
# Github: https://github.com/kennethrisa/discord-rustserverstatus-python
# Version 0.0.2
import discord
import asyncio
import requests
import configparser
# Config:
config = configparser.ConfigParser()
config.read('config.ini')
BOT = config['BOT']
token = BOT['token']
apiSite = BOT['apiSite']
apiUrl = BOT['apiUrl']
# updateInSec
updateInterval = 180
client = discord.Client()
# Print the starting text
print('---------------')
print('Discord Bot updates activity status every 3 min')
print('---------------')
print('Starting Bot...')
try:
async def updateStatusPresence():
await client.wait_until_ready()
counter = 0
while not client.is_closed():
counter += 1
if apiSite == '1':
url = apiUrl
resp = requests.get(url=url)
if resp.status_code != 200:
raise print("Expected status code 200, but got {}")
else:
data = resp.json()
players = data['players']
maxPlayers = data['players_max']
game = discord.Game(name=players + " / " + maxPlayers)
await client.change_presence(status=discord.Status.idle, activity=game)
if apiSite == '2':
url = apiUrl
resp = requests.get(url=url)
if resp.status_code != 200:
raise print("Expected status code 200, but got {}")
else:
data = resp.json()
players = data['players']
maxPlayers = data['maxplayers']
game = discord.Game(name=players + " / " + maxPlayers)
await client.change_presence(status=discord.Status.idle, activity=game)
await asyncio.sleep(updateInterval)
@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print('------')
# Create task to update status
client.loop.create_task(updateStatusPresence())
except Exception as e:
print(e)
# Start bot
client.run(token)