-
Notifications
You must be signed in to change notification settings - Fork 2
/
bot.py
256 lines (219 loc) · 8.66 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import asyncio
import json
import os
import sys
import nextcord
import aiohttp
import aiofiles
from rich import print, console
from dotenv import load_dotenv
from nextcord.ext import commands, tasks
intents = nextcord.Intents.default()
intents.message_content = True
bot = commands.Bot(
command_prefix="!", intents=intents,
status=nextcord.Status.do_not_disturb,
activity=nextcord.Game(name="https://github.com/Ixogamer"),
)
console = console.Console()
load_dotenv()
class CloseButton(nextcord.ui.View):
def __init__(self, message: nextcord.Message):
super().__init__()
self.message = message
@nextcord.ui.button(label="🗑️", style=nextcord.ButtonStyle.red)
async def close_button(
self, button: nextcord.Button, interaction: nextcord.Interaction
):
await self.message.delete()
async def readConfig():
async with aiofiles.open("config.json", "r", encoding="utf8") as jsonfile:
config = json.loads(await jsonfile.read())
console.log("Config loaded [green]successfully[/green].")
return config
# Run this the first time the bot is started
async def first_startup():
config = await readConfig()
console.log("Starting...")
print("Please, send here the Channel ID for the updating message.")
channel_id = input()
# user_ids = {"userids": []}
# Set the Channel Id to the config file
config["channel_id"] = channel_id
# config["user_ids"] = user_ids
async with aiofiles.open("config.json", "w", encoding="utf8") as jsonfile:
await jsonfile.write(json.dumps(config))
# On ready, do this.
@bot.event
async def on_ready():
config = await readConfig()
if not config["channel_id"]:
await first_startup()
config = await readConfig()
try:
channel_id = int(config["channel_id"])
console.log(f"Channel ID Set to: {channel_id}.")
except Exception as e:
console.log(f"[red]Error[/red]: Couldn't read channel id: {e}")
sys.exit(1)
print(f"Logged in as {bot.user}.")
channel = bot.get_channel(channel_id)
console.log(f"Channel #{channel.name} ({channel.id}) [green]found[/green].")
# You have to make the message a variable so the ctx.edit() can use it. Without it, it doesn't have the correct context.
message = await channel.send(
embed=nextcord.Embed(
title="The bot has been enabled.", description="Welcome!", color=0x008B02
)
)
await asyncio.sleep(5)
embed = nextcord.Embed(
title="Command List:", description="!prioq \n !start", color=0x5300EB
).set_footer(
text="Remember that you can always invoke the command list using !help"
)
await message.edit(embed=embed, delete_after=5)
@bot.event
async def on_command_error(ctx, error):
console.log(f"[red]Error[/red]: {str(error)}")
@bot.command()
async def queue(ctx):
async with aiohttp.ClientSession() as session:
async with session.get("https://2bqueue.info/*") as response:
stats = await response.json()
async with session.get("https://api.2b2t.dev/prioq") as response:
estimatedtime = await response.json()
estimatedtime = estimatedtime[2]
clean_embed = nextcord.Embed(
title="Scale of unplayability (0 - 100)",
description=f'Normal:{stats["regular"]}\nPrio: {stats["prio"]}',
color=0xFF00BF,
).set_footer(text=f"Estimated queue time: {estimatedtime}")
message_to_send = await ctx.send(embed=clean_embed)
await message_to_send.edit(view=CloseButton(message_to_send))
@bot.command()
async def start(ctx):
repeat_command.start(ctx)
@bot.command()
async def coords(ctx, coordx: float, coordz: float):
overworld_coords_x = int(coordx * 8)
overworld_coords_z = int(coordz * 8)
nether_coords_x = int(coordx // 8)
nether_coords_z = int(coordz // 8)
embed = nextcord.Embed(title="Coordinates conversion result:")
embed.set_author(name="Sponsored by Ccorp", url="https://discord.gg/ccorp")
embed.add_field(
name="Nether to Overworld:",
value=f"**X:** {overworld_coords_x} | **Z:** {overworld_coords_z}",
inline=False,
)
embed.add_field(
name="Overworld to Nether:",
value=f"**X:** {nether_coords_x} | **Z:** {nether_coords_z}",
inline=False,
)
embed.set_footer(text="Made by GUMI#1337")
message_to_send = await ctx.send(embed=embed)
await message_to_send.edit(view=CloseButton(message_to_send))
@bot.command()
async def eta(ctx, blocks: int = 0, bps: float = 18.0):
if blocks == 0:
message_to_send = await ctx.send(
"Usage: !eta [Blocks to travel] [Blocks per second] \n Example: `!eta 5000 18`. \n If the blocks per second isnt set, it will use the value 18."
)
await message_to_send.edit(view=CloseButton(message_to_send))
return
seconds = blocks / bps
seconds = seconds % (24 * 3600)
hour = seconds // 3600
seconds %= 3600
minutes = seconds // 60
seconds %= 60
result = f"%d:%02d:%02d" % (hour, minutes, seconds)
embed = nextcord.Embed(title="Estimated time of arrival:")
embed.set_author(name="Sponsored by Ccorp", url="https://discord.gg/ccorp")
embed.add_field(
name=f"Result at {bps} blocks per second:",
value=f"{result} \n\n*(Hours:Minutes:Seconds)*",
inline=False,
)
embed.set_footer(text="Made by GUMI#1337")
message_to_send = await ctx.send(embed=embed)
await message_to_send.edit(view=CloseButton(message_to_send))
@bot.command()
async def user(ctx, username):
async with aiohttp.ClientSession() as session:
async with session.get(
f"https://api.mojang.com/users/profiles/minecraft/{username}"
) as response:
data = await response.json()
uuid = data["id"]
async with aiohttp.ClientSession() as session:
async with session.get(
f"http://api.cokesniffer.org:8080/mutes?username={username}"
) as response:
# I have to do this because the API is scuffed and returns plain text.
# FIXME: Fix this if the API is fixed.
code = response.status
data = await response.read()
if data == b"Username Not Found":
is_muted = False
mute_type = None
mute_rules = None
else:
loaded_data = json.loads(data)
is_muted = True
mute_type = loaded_data["type"]
mute_rules = loaded_data["rules"]
async with aiohttp.ClientSession() as session:
async with session.get(
f"http://api.cokesniffer.org:8080/bans?username={username}"
) as response:
code = response.status
data = await response.read()
if data == b"Username Not Found":
is_banned = False
ban_rules = None
else:
loaded_data = json.loads(data)
is_banned = True
ban_rules = loaded_data["rules"]
# TODO: MAKE THIS BETTER
clean_embed = nextcord.Embed(
title=f"Results for lookup of player {username}: ", color=0x008B02
)
clean_embed.set_thumbnail(url=f"https://crafatar.com/avatars/{uuid}?size=64.png")
clean_embed.add_field(
name="Banned:",
value=f"**{is_banned}**, for rule(s) **{ban_rules}**",
inline=True,
)
clean_embed.add_field(
name="Muted:",
value=f"**{is_muted}**, of type **{mute_type}**, for rules **{mute_rules}**",
inline=True,
)
clean_embed.add_field(name="UUID:", value=f"**{uuid}**", inline=False)
clean_embed.set_image(url=f"https://crafatar.com/renders/body/{uuid}?scale=4")
clean_embed.set_footer(text="Made by GUMI#1337")
message_to_send = await ctx.send(embed=clean_embed)
await message_to_send.edit(view=CloseButton(message_to_send))
@tasks.loop(minutes=10)
async def repeat_command(ctx):
global stats
old_stats = stats
async with aiohttp.ClientSession() as session:
async with session.get("https://2bqueue.info/*") as response:
stats = await response.json()
async with session.get("https://api.2b2t.dev/prioq") as response:
estimatedtime = await response.json()
estimatedtime = estimatedtime[2]
if stats == old_stats:
console.log("[yellow]No change in stats[/yellow], skipping.")
return
clean_embed = nextcord.Embed(
title="Scale of unplayability (0 - 100)",
description=f'Normal:{stats["regular"]}\nPrio: {stats["prio"]}',
color=0xFF00BF,
).set_footer(text=f"Estimated queue time: {estimatedtime}")
await ctx.send(embed=clean_embed)
bot.run(os.getenv("TOKEN"))