-
Notifications
You must be signed in to change notification settings - Fork 3
/
Discord.py
165 lines (137 loc) · 6.45 KB
/
Discord.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import discord
import Topic
import math
import pymysql
import yaml
from ipaddress import IPv4Address
from urllib.parse import parse_qs
with open("config/config.yml", "r") as ymlfile:
cfg = yaml.load(ymlfile)
client = discord.Client()
"""SQL Connection stuff"""
async def queryMySql(query):
db = pymysql.connect(cfg["mysql"]["host"], cfg["mysql"]["user"], cfg["mysql"]["passwd"], cfg["mysql"]["db"])
try:
with db.cursor() as cursor:
cursor.execute(query)
result = cursor.fetchall()
finally:
del(cursor)
db.close()
return result
async def staffServerCheck(id):
if id == cfg['discord']['staffserverID']:
return True
else:
return False
@client.event
async def on_message(message):
if message.author == client.user:
return
eye = "\U0001F441"
if message.content.startswith(eye) == False:
return
explode = message.content.split(' ')
if explode[0] == "\U0001F441status":
await client.send_typing(message.channel)
try:
topic = Topic.Topic()
response = parse_qs(topic.send_topic("status"))
outmsg = "**Gamemode:** " + response["mode"][0] + "\n"
states = ["Starting Up", "Lobby", "Setting Up", "In Progress", "Finished"]
outmsg += "**State:** " + states[int(response["gamestate"][0])] + "\n"
outmsg += "**Admins:** " + response["admins"][0] + "\n"
outmsg += "**Players:** " + response["active_players"][0] + "\n"
roundseconds = int(response["round_duration"][0])
roundhours = 12 + int(math.floor(roundseconds / 3600))
roundseconds %= 3600
roundminutes = int(math.floor(roundseconds / 60))
roundseconds %= 60
outmsg += "**Time:** {h:02d}:{m:02d}:{s:02d}".format(h=roundhours, m=roundminutes, s=roundseconds)
except ConnectionError as e:
outmsg = "Server appears to be down!"
await client.send_message(message.channel, outmsg)
if explode[0] == "\U0001F441bwoink" and message.server.id == cfg['discord']['staffserverID']:
await client.send_typing(message.channel)
try:
topic = Topic.Topic()
outmsg = topic.send_topic("adminmsg=" + explode[1] + "&msg=" + explode[2] + "&sender=" + message.author.name)
except ConnectionError as e:
outmsg = "Server appears to be down!"
await client.send_message(message.channel, outmsg)
if explode[0] == "\U0001F441notes" and await staffServerCheck(message.server.id): # Check it's in the correct server
output = ""
ckey = ""
if len(explode) == 2:
ckey = explode[1]
elif len(explode) > 2:
ckey = " ".join(explode[1:])
else:
output = "No argument specified."
await client.send_message(message.channel, output)
return
query = "SELECT `text`, `timestamp`, `adminckey`, `lasteditor` FROM messages WHERE targetckey LIKE \'{0}\'".format(ckey)
result = await queryMySql(query)
if result:
output = "Notes for player " + ckey + "\n\n"
for line in result:
newnote = "```" + line[0] + "\n"
newnote += "added at " + str(line[1]) + " by " + line[2] + "\n\n"
newnote += "```"
if len(output + newnote) > 2000:
"""If the message would be over 2000 characters then output the message and then reset"""
await client.send_message(message.channel, output)
output = newnote
else:
output = output + newnote
else:
output = "No results found for " + ckey
await client.send_message(message.channel, output)
if explode[0] == "\U0001F441info" and await staffServerCheck(message.server.id):
output = ""
ckey = ""
if len(explode) == 2:
ckey = explode[1]
elif len(explode) > 2:
ckey = " ".join(explode[1:])
else:
output = "No argument specified."
await client.send_message(message.channel, output)
return
query = "SELECT `accountjoindate`, `firstseen`, `lastseen`, `computerid`, `ip` FROM player WHERE ckey LIKE \'{0}\';".format(ckey)
result = await queryMySql(query)
if result:
output = "Player information for **{0}**:\n\n".format(ckey)
for line in result:
ip = IPv4Address(int(line[4]))
output += "```Join Date: {0}\nFirst Seen: {1}\nLast Seen: {2}\nComputer ID: {3}\nIP: {4} ({5})```".format(line[0], line[1], line[2], line[3], ip, line[4])
return await client.send_message(message.channel, output)
else:
return await client.send_message(message.channel, "No result found for {0}".format(ckey))
if explode[0] == "\U0001F441searchban" and await staffServerCheck(message.server.id):
output = ""
ckey = ""
if len(explode) == 2:
ckey = explode[1]
elif len(explode) > 2:
ckey = " ".join(explode[1:])
else:
output = "No argument specified."
return await client.send_message(message.channel, output)
query = "SELECT `bantype`, `bantime`, `expiration_time`, `computerid`, `ip`, `adminwho`,`unbanned`,`unbanned_datetime`, `duration`, `reason` FROM ban WHERE ckey LIKE \'{0}\';".format(ckey)
result = await queryMySql(query)
if result:
output = "Ban information for **{0}**:\n\n".format(ckey)
for line in result:
if len(output) > 1500:
await client.send_message(message.channel, output)
output = ""
output += "```Ban Type: {0} duration {1} minutes placed by {2} on {3}.\n".format(line[0], line[8], line[5], line[1])
output += "Expiry Time: {0}\n".format(line[2])
output += "Reason: {0}\n".format(line[9])
output += "Unbanned: {0} {1}\n```".format("YES at" if str(line[6]) == "1" else "NO", line[7] if str(line[6]) == "1" else "")
return await client.send_message(message.channel, output)
@client.event
async def on_ready():
print('SS13 BOT ONLINE')
client.run(cfg["botclient"]["token"])