/
BotFuncs.py
157 lines (126 loc) · 3.78 KB
/
BotFuncs.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
# -*- coding: utf-8 -*-
"""
Created on Wed Jan 29 16:56:05 2020
@author: Dafydd
"""
from datetime import datetime as dt
from mcstatus import JavaServer
from mcrcon import MCRcon
import dateparser as dp
import time,discord,aiocsv,aiofiles,asyncio
def find_jim(guild):
for m in guild.members:
if m.name == "Jimbob" and m.discriminator == "9284":
jim = m
break
return jim
def getMembers(guild):
membs = {}
membList = []
for m in guild.members:
discordID = str(m.name)+"#"+str(m.discriminator)
#print(discordID)
membs[discordID] = m.id
membList.append(discordID)
return membs, membList
def getVoiceChannels(guild):
chans = {}
for vc in guild.voice_channels:
chans[vc.name] = vc.id
return chans
def get_user_vc(guild, user):
chan = False
membs, membList = getMembers(guild)
target = guild.get_member(membs[user])
for vc in guild.voice_channels:
if target in vc.members:
chan = vc
break
return chan
def getRoles(guild):
roles = {}
for r in guild.roles:
roles[r.name] = r
return roles
def user2name(user):
name = user.name
disc = user.discriminator
discordid = str(name)+"#"+str(disc)
return discordid
def make_time(timestring):
format = 0
if 'h' in timestring:
format += 1
if 'm' in timestring:
format += 10
if 's' in timestring:
format +=100
return
def get_tickrate(ip,rconpassword):
try:
with MCRcon(ip,rconpassword) as mcr:
resp = mcr.command("/tps")
#print(resp)
mcr.disconnect()
tps = resp
"""
dummy = resp.split("TPS")
tps = [dummy[0].strip()]
for i in dummy:
loc = i.find(")")+1
if (loc):
tps.append(i[loc:])
tps = tps[:-1]
"""
except:
tps = ["Unable to access tps information"]
return tps
def get_mc_status(ip):
#check for valid ip
if len(ip.split(":")) == 2:
ip = str(ip)
elif len(ip.split(":")) == 1:
ip = str(ip) + ":25565"
else:
return {"status":-1}
server = JavaServer.lookup(ip)
try:
dets = server.status()
out = {"status":1,
"colour":discord.Colour.green(),
"title":"Minecraft Server Status",
"description":"Server {0} at {1} is Online\nPlayers Online : {2}/{3}".format(dets.description,ip,dets.players.online,dets.players.max)}
try:
players = server.query().players.names
out["players"] = players
except:
print("Server Query is disabled on {0}".format(ip))
except:
out = {"status":0,
"colour":discord.Colour.red(),
"title":"Minecraft Server Status",
"description":"Server at {0} is currently offline!".format(ip)}
return out
def get_unix_time(intime):
"""
Takes a date and time and converts to unix time
"""
intime = "le "+intime
dtime = dp.parse(intime)
utime = int(time.mktime(dtime.timetuple()))
return utime
async def backup_user_nicknames(guild):
"""
Backs up the nicknames of the guild and stores the file as {guild}.csv
"""
backup_success = False
async with aiofiles.open("{0}.csv".format(guild), mode="w", encoding="utf-8",newline="") as afp:
writer = aiocsv.AsyncWriter(afp, dialect="unix")
for m in guild.members:
await writer.writerow([str(m.id),"{0}#{1}".format(m.name,m.discriminator),m.nick])
backup_success = True
return backup_success
def get_current_time():
now = dt.now()
current_time = now.strftime("%H:%M:%S")
return current_time