-
Notifications
You must be signed in to change notification settings - Fork 0
/
v1.2.py
452 lines (286 loc) · 13.8 KB
/
v1.2.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import requests
from bs4 import BeautifulSoup
import discord
from discord.ext import commands
from discord import Intents
import json
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
prefix = "bg-"
bot = commands.Bot(command_prefix=prefix, case_insensitive=True, intents=Intents.all())
bot.remove_command('help')
# ----- COMMANDS -----
# --- ping ---
@bot.command()
async def ping(ctx):
return await ctx.send(f'Ping is {round(bot.latency * 1000)}ms')
# --- help ---
@bot.command()
async def help(ctx):
embed = discord.Embed(title="Help Command", colour=ctx.bot.user.colour)
embed = embed.add_field(name=f"{prefix}ping", value="to check bot ping", inline=False)
embed = embed.add_field(name=f"{prefix}invite", value="to get bot invite link", inline=False)
embed = embed.add_field(name=f"{prefix}servers", value="to search online or offline servers", inline=False)
embed = embed.add_field(name=f"{prefix}players", value="to check online players of a server", inline=False)
embed = embed.add_field(name=f"{prefix}imginfo", value="to get info banner of a server", inline=False)
embed = embed.add_field(name=f"{prefix}info", value="to get info of a server", inline=False)
return await ctx.send(embed=embed)
# --- !invite ---
@bot.command()
async def invite(ctx):
return await ctx.send("Click:- https://discord.com/api/oauth2/authorize?client_id=910434347365064714&permissions=1511828880448&scope=bot")
# --- !servers ---
@bot.command()
async def servers(ctx, *,server:str):
msg = await ctx.send("`Searching...`")
url = f'https://www.game-state.com/index.php?search={server}&spp=50&game=samp'
result = requests.get(url, headers=headers)
soup = BeautifulSoup(result.text, 'html.parser')
total_server = 0
embed = discord.Embed(title="Online Server", description=f"Server found with name {server}, Bot can only send first 25 matching servers.", colour=ctx.bot.user.colour)
for a in soup.find_all('a', href=True):
if str(server.lower()) in str((a.get_text()).lower()):
total_server = total_server + 1
server_ip_info = a['href']
# server_raw = server_ip_info[1:-1].split(':')
server_ip = server_ip_info[1:-1]
embed = embed.add_field(name=f"{str(a.get_text())}", value=f"IP = {server_ip}", inline=False)
if total_server <= 0:
await msg.delete()
return await ctx.send(f"There is no server online with name `{server}`")
else:
await msg.delete()
embed = embed.add_field(name="Total Servers", value=total_server, inline=True)
embed = embed.set_footer(text="Big Smoke", icon_url=ctx.bot.user.avatar_url)
return await ctx.send(embed=embed)
# --- !players ---
@bot.command()
async def players(ctx, *,server:str):
msg = await ctx.send("`Searching...`")
url = f'https://www.game-state.com/index.php?search={server}&spp=50&game=samp'
result = requests.get(url, headers=headers)
soup = BeautifulSoup(result.text, 'html.parser')
if total_online_servers(server) >= 2:
await msg.delete()
return await ctx.send(f"There are two or too many servers online with a name `{server}`, Use `!servers` to check right server name.")
elif total_online_servers(server) == False:
await msg.delete()
return await ctx.send(f"There are no server found with name `{server}`")
player_list = ""
for a in soup.find_all('a', href=True):
if str(server.lower()) in str((a.get_text()).lower()):
su = requests.get(f"https://www.game-state.com{a['href']}", headers=headers)
soupurl = BeautifulSoup(su.text, 'html.parser')
playerslist = soupurl.find(id='playerslist')
tp = 0
for player in playerslist.find_all('td'):
if tp%2 == 0:
tp = tp + 1
player_list = player_list + f"{str(player.get_text())}"
elif tp%2 == 1:
tp = tp + 1
player_list = player_list + f" - {str(player.get_text())}\n"
if tp <= 2:
await msg.delete()
return await ctx.send("There are no player online or unable to retrive player data due to server having 100+ members.")
await msg.delete()
embed = discord.Embed(title=f"{str(a.get_text())}", description=player_list, colour=ctx.bot.user.colour)
embed.add_field(name="Total Players", value=str((soupurl.find(id='players')).get_text()), inline=True)
embed = embed.set_footer(text="Big Smoke", icon_url=ctx.bot.user.avatar_url)
return await ctx.send(embed=embed)
else:
await msg.delete()
return await ctx.send(f"Server not found with name {server}.")
# --- !imginfo ---
@bot.command()
async def imginfo(ctx, *,server:str):
msg = await ctx.send("`Searching...`")
url = f'https://www.game-state.com/index.php?search={server}&game=samp'
result = requests.get(url, headers=headers)
soup = BeautifulSoup(result.text, 'html.parser')
if total_online_servers(server) >= 2:
await msg.delete()
return await ctx.send(f"There are two or too many servers online with name `{server}`")
elif total_online_servers(server) == False:
await msg.delete()
return await ctx.send(f"There are no server found with name `{server}`")
for a in soup.find_all('a', href=True):
if str(server.lower()) in str((a.get_text()).lower()):
await msg.delete()
return await ctx.send(f"http://www.game-state.com/{a['href']}/560x95_FFFFFF_FF9900_000000_000000.png")
# --- info ---
@bot.command()
async def info(ctx, *, server:str):
msg = await ctx.send(f"`Searching...`")
url = f'https://www.game-state.com/index.php?search={server}&spp=50&game=samp'
result = requests.get(url, headers=headers)
soup = BeautifulSoup(result.text, 'html.parser')
if total_online_servers(server) >= 2:
await msg.delete()
return await ctx.send(f"There are two or too many servers online with name `{server}`")
elif total_online_servers(server) == False:
await msg.delete()
return await ctx.send(f"There are no server found with name `{server}`")
for a in soup.find_all('a', href=True):
if str(server.lower()) in str((a.get_text()).lower()):
su = requests.get(f"https://www.game-state.com{a['href']}", headers=headers)
soupurl = BeautifulSoup(su.text, 'html.parser')
embed = discord.Embed(title="Server Info", colour=ctx.bot.user.colour)
embed = embed.add_field(name="Server Name", value=f"{str((soupurl.find(id='hostname')).get_text())}", inline=True)
embed = embed.add_field(name="State", value=f"{str((soupurl.find(id='state')).get_text())}", inline=True)
embed = embed.add_field(name="Total Players", value=f"{str((soupurl.find(id='players')).get_text())}", inline=True)
embed = embed.add_field(name="IP", value=f"{str(a['href'][1:-1])}", inline=True)
embed = embed.add_field(name="Game Mode", value=f"{str((soupurl.find(id='gamemode')).get_text())}", inline=True)
embed = embed.add_field(name="Map Name", value=f"{str((soupurl.find(id='mapname')).get_text())}", inline=True)
servervar = soupurl.find(id='var')
varcout = 0
var1 = ""
var2 = ""
for link in servervar.find_all('td'):
varcout = varcout + 1
if varcout%2 == 1:
var1 = str(link.get_text())
elif varcout%2 == 0:
var2 = str(link.get_text())
embed = embed.add_field(name=var1, value=var2, inline=True)
# if not found td or loop won't add new fields :)
await msg.delete()
embed = embed.set_footer(text="Big Smoke", icon_url=ctx.bot.user.avatar_url)
return await ctx.send(embed=embed)
# ----- EVENTS -----
@bot.event
async def on_ready():
# --- Bot Rich Presence ---
game = discord.Game("Created by DeViL#7091")
await bot.change_presence(status=discord.Status.online, activity=game)
print(f'Bot is online.\n Bot Name: {bot.user}!')
# ----- COMMAND ERRORS -----
@players.error
async def players_error(ctx, error):
if isinstance(error, commands.MissingRequiredArgument) or isinstance(error, commands.BadArgument):
await ctx.send(f"{ctx.author.mention} Correct Usage: `{prefix}players [Server Name]`")
@players.error
async def servers_error(ctx, error):
if isinstance(error, commands.MissingRequiredArgument) or isinstance(error, commands.BadArgument):
await ctx.send(f"{ctx.author.mention} Correct Usage: `{prefix}servers [Server Name]`")
@imginfo.error
async def imginfo_error(ctx, error):
if isinstance(error, commands.MissingRequiredArgument) or isinstance(error, commands.BadArgument):
await ctx.send(f"{ctx.author.mention} Correct Usage: `{prefix}imginfo [Server Name]`")
@info.error
async def info_error(ctx, error):
if isinstance(error, commands.MissingRequiredArgument) or isinstance(error, commands.BadArgument):
await ctx.send(f"{ctx.author.mention} Correct Usage: `{prefix}imginfo [Server Name]`")
# ----- FUNCTIONS -----
# --- Total Online Servers ---
def total_online_servers(server:str):
url = f'https://www.game-state.com/index.php?search={server}&spp=50&game=samp'
result = requests.get(url, headers=headers)
soup = BeautifulSoup(result.text, 'html.parser')
total_server = 0
serverlist = soup.find(id='serverlist')
if serverlist == None:
return False
for cs in serverlist.find_all('a', href=True):
if server.lower() in str((cs.get_text().lower())):
print(server.lower())
total_server = total_server + 1
return total_server
with open('./config.json', 'r') as configFile:
configdata = json.load(configFile)
token = configdata['TOKEN']
bot.run(token)
"""
# --- Total Online Servers ---
def total_online_servers(server:str):
url = f'https://www.game-state.com/index.php?search={server}&spp=50&game=samp'
result = requests.get(url, headers=headers)
soup = BeautifulSoup(result.text, 'html.parser')
total_server = 0
serverlist = soup.find(id='serverlist')
for cs in serverlist.find_all('a', href=True):
if str(server.lower()) in str((cs.get_text().lower())):
total_server = total_server + 1
return total_server
# --- Servers ---
def find_online_servers(server:str):
url = f'https://www.game-state.com/index.php?search={server}&spp=50&game=samp'
result = requests.get(url, headers=headers)
soup = BeautifulSoup(result.text, 'html.parser')
total_server = 0
total_server_string = ""
serverlist = soup.find(id='serverlist')
for a in serverlist.find_all('a', href=True):
if str(userserver.lower()) in str((a.get_text().lower())):
total_server = total_server + 1
server_ip_info = a['href']
# server_raw = server_ip_info[1:-1].split(':')
server_ip = server_ip_info[1:-1]
total_server_string = total_server_string + f"Server: {str(a.get_text())} IP: {server_ip}\n"
if total_server <= 0:
return f"There is no server online with name {server}"
else:
return total_server_string
# ----- Server Image -----
def find_online_server_img(server:str):
url = f'https://www.game-state.com/index.php?search={server}&spp=50&game=samp'
result = requests.get(url, headers=headers)
soup = BeautifulSoup(result.text, 'html.parser')
if total_online_servers(server) >= 2:
return f"There are two or too many servers online with name {server}"
for a in soup.find_all('a', href=True):
if str(server.lower()) in str((a.get_text().lower())):
return f"http://www.game-state.com/{a['href']}/560x95_FFFFFF_FF9900_000000_000000.png"
# ----- Online Players -----
def find_online_server_players(server:str):
url = f'https://www.game-state.com/index.php?search={server}&spp=50&game=samp'
result = requests.get(url, headers=headers)
soup = BeautifulSoup(result.text, 'html.parser')
if total_online_servers(server) >= 2:
return f"There are two or too many servers online with name {server}"
player_list = ""
for a in soup.find_all('a', href=True):
if str(userserver.lower()) in str((a.get_text().lower())):
serverurl = f"https://www.game-state.com{a['href']}"
su = requests.get(serverurl, headers=headers)
soupurl = BeautifulSoup(su.text, 'html.parser')
playerslist = soupurl.find(id='playerslist')
tp = 0
for player in playerslist.find_all('td'):
if tp%2 == 0:
player_list = player_list + f"{str(player.get_text())}"
tp = tp + 1
elif tp%2 == 1:
tp = tp + 1
player_list = player_list + f" - {str(player.get_text())}\n"
if (tp-2) <= 2:
return f"There are no players online or not retrive players data due to server having more 100+ player"
return player_list
# ----- Server Info -----
def find_online_server_info(server:str):
url = f'https://www.game-state.com/index.php?search={server}&spp=50&game=samp'
result = requests.get(url, headers=headers)
soup = BeautifulSoup(result.text, 'html.parser')
if total_online_servers(server) >= 2:
return f"There are two or too many servers online with name {server}"
for a in soup.find_all('a', href=True):
if str(server.lower()) in str((a.get_text().lower())):
serverurl = f"https://www.game-state.com{a['href']}"
su = requests.get(serverurl, headers=headers)
soupurl = BeautifulSoup(su.text, 'html.parser')
print(f"Server Name: {(soupurl.find(id='hostname')).get_text()}")
print(f"State: {(soupurl.find(id='state')).get_text()}")
print(f"Total Players: {(soupurl.find(id='players')).get_text()}")
print(f"IP: {a['href'][1:-1]}")
print(f"Game Mode: {(soupurl.find(id='gamemode')).get_text()}")
print(f"Map Name: {(soupurl.find(id='mapname')).get_text()}")
servervar = soupurl.find(id='var')
varcout = 0
var = ""
for link in servervar.find_all('td'):
varcout = varcout + 1
if varcout%2 == 1:
var = var + str(link.get_text())
elif varcout%2 == 0:
var = var + f"{str(link.get_text())}\n"
return var
"""