-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
73 lines (64 loc) · 2.55 KB
/
bot.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
# bot.py
import os
import requests
import json
import csv
import discord
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
client = discord.Client()
def find_mentionstr(gmrname):
with open("users.csv", "r") as infile:
reader = csv.reader(infile)
next(reader)
for line in reader:
if [gmrname] == line[:1]:
return line[1:][0]
@client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.casefold() == '!GMR'.casefold():
site_request = requests.get(os.getenv('GMR_URL'))
jsonResponse = site_request.json()
currentturn = jsonResponse["Games"][0]["CurrentTurn"]["UserId"]
expires = jsonResponse["Games"][0]["CurrentTurn"]["Expires"]
playerdata = jsonResponse["Players"]
for player in playerdata:
playerid = player["SteamID"]
if playerid == currentturn:
name = player["PersonaName"]
break
mentionstr = find_mentionstr(str(name))
if mentionstr == None:
response = "**Current Turn:** " + str(name) + "\n**Expires:** " + expires
await message.channel.send(response)
else:
response = mentionstr + "\n**Current Turn:** " + str(name) + "\n**Expires:** " + expires
await message.channel.send(response)
if message.content.casefold().startswith('!GMR@ME'.casefold()):
mentionstr = str(message.author.mention)
gmrname = message.content[8:]
site_request = requests.get(os.getenv('GMR_URL'))
jsonResponse = site_request.json()
playerdata = jsonResponse["Players"]
userexists = "false"
for player in playerdata:
if player["PersonaName"] == gmrname:
userexists = "true"
try:
if userexists == "false":
raise Exception()
with open('users.csv', mode='a') as usercsv:
usercsv_writer = csv.writer(usercsv, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
usercsv_writer.writerow([gmrname, mentionstr])
response = "I will mention " + mentionstr + " when it is " + gmrname + "'s turn"
await message.channel.send(response)
except:
response = "Something went wrong, try again\n**Proper use:**\n!GMR@ME [Steam Name Here]"
await message.channel.send(response)
client.run(TOKEN)