-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDiscordMCStatsBot.py
More file actions
136 lines (102 loc) · 3.53 KB
/
DiscordMCStatsBot.py
File metadata and controls
136 lines (102 loc) · 3.53 KB
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
import os, sys
try:
import minestat
import discord
from discord.ext import commands, tasks
except ImportError:
if sys.platform.startswith("win"):
os.system("python -m pip install discord.py minestat")
else:
os.system("python3 -m pip install discord.py minestat")
try:
import minestat
import discord
from discord.ext import commands, tasks
except ImportError:
print("Can't install stuff")
exit()
"""
minecraft = minestat.MineStat(address=add, port=p)
menu = f\"""
Port: {minecraft.port}
Host: {minecraft.address}
MOTD: {minecraft.motd}
Online: {minecraft.online}
Players: {minecraft.current_players}
Max Players: {minecraft.max_players}
Gamemode: {minecraft.gamemode}
Latency: {minecraft.latency}
Tool version: {minecraft.VERSION}
Server version: {minecraft.version}
SPL protocol: {minecraft.slp_protocol}
Stripped MOTD: {minecraft.stripped_motd}
\"""
"""
bot = commands.Bot(command_prefix="!!!", help=None, intents=discord.Intents.all())
channel_id = 1159216815801630743
message_stats = None
# (address:port) : name
mc_servers = dict()
mc_servers["127.0.0.1:25565"] = "MyServer"
display_players_server = "MyServer"
my_guild_id = 0
def getMCStats(address: str, port: int) -> minestat.MineStat:
return minestat.MineStat(address=address, port=port)
async def sendStats(embed: discord.Embed):
channel = bot.get_channel(channel_id)
if channel is None:
return
global message_stats
if message_stats is None:
s = await channel.send(embed=embed)
message_stats = s
else:
await message_stats.edit(embed=embed)
def formatMCStats(minecraft: minestat.MineStat, server_name: str) -> discord.Embed:
data = dict()
if minecraft is None:
return discord.Embed(colour=discord.Colour.red())
emb = discord.Embed()
# MOTD : {minecraft.stripped_motd}
menu = f"""
Host : {minecraft.address}
Server Stats : { "🟢" if minecraft.online else "🔴" }
Port : {minecraft.port}
MOTD : {minecraft.stripped_motd}
Online : {minecraft.current_players} / {minecraft.max_players}
Server Version : 1.16 - 1.20
"""
# f"```{menu}```"
# "🟢" if minecraft.online else "🔴"
emb.add_field(name=server_name, value=f"```{menu}```", inline=False)
return emb
@tasks.loop(seconds=3)
async def update():
server_embeds = []
players = 0
for host in mc_servers.keys():
name = mc_servers[host]
stats = None
if ":" in host:
spl = host.split(":")
port = int(spl[1])
address = spl[0]
stats = getMCStats(address, port)
server_embeds.append(formatMCStats(stats, name))
else:
stats = getMCStats(host, 25565)
server_embeds.append(formatMCStats(stats, name))
if name == display_players_server:
players = stats.current_players
totalEmbed = discord.Embed(colour=discord.Colour.blue(), title="Servers Status")
for server in server_embeds:
for f in server.fields:
totalEmbed.add_field(name=f.name, value=f.value, inline=f.inline)
await sendStats(totalEmbed)
await bot.change_presence(activity=discord.Game(name=f"{display_players_server}: {players}"))
@bot.event
async def on_ready():
update.start()
print(bot.user.name + " is online")
if __name__ == "__main__":
bot.run("TOKEN HERE")