-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
265 lines (231 loc) · 10.7 KB
/
main.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
#preventing creation of the __pycache__ folder (HAS TO BE BEFORE THE MODULES IMPORT)
import sys
sys.dont_write_bytecode = True
#imports
from nextcord import Intents, Interaction, Attachment, Embed, TextInputStyle
from nextcord.ext import commands
from nextcord.ui import Modal, TextInput
from nextcord.errors import HTTPException, LoginFailure
from os import getenv
from dotenv import load_dotenv
from time import sleep
from datetime import datetime
from asyncio import sleep as asyncsleep
#modules
import modules.functions as functions
import modules.informative as informative
import modules.internal as internal
#------------------------------- SETUP ---------------------------------------------------
shards_count = 5
#loading api key from .env
load_dotenv()
TOKEN = getenv("API_TOKEN")
#prefix
intents = Intents.default()
#intents.message_content = True
bot = commands.AutoShardedBot(shard_count=shards_count, intents=intents)
bot.remove_command('help')
#-------------------------------- CLIENT ------------------------------------------------
@bot.event
async def on_ready():
internal.tclear()
print(f'Logged in as: {bot.user.name}(#{bot.user.discriminator})')
print(f'Servers Joined: {len(bot.guilds)}')
total_members = 0
for g in bot.guilds:
if g.member_count:
total_members += int(g.member_count)
print(f'Total Users: {total_members}')
try:
await bot.sync_all_application_commands()
except Exception as e:
print("Error while syncing application commands:",e)
print("----------------------------- LOGS ---------------------------------")
#update database every 5 minutes
bot.loop.create_task(internal.updatedb("db_update.py"))
@bot.event
async def on_connect():
now = datetime.now()
dt_string = now.strftime("[%d/%m - %H:%M:%S] ")
print(dt_string + "{} connected".format(bot.user.name))
@bot.event
async def on_disconnect():
now = datetime.now()
dt_string = now.strftime("[%d/%m - %H:%M:%S] ")
print(dt_string + "{} disconnected".format(bot.user.name))
all_shards_offline = True
for shard_id, shard_info in bot.shards.items():
if shard_info.is_closed() != False:
all_shards_offline = False
break
if all_shards_offline:
print(dt_string + "All shards are offline. Restarting the bot...")
await bot.close()
await asyncsleep(10)
await bot.start(TOKEN, reconnect=True)
print(dt_string + "Restarted. Sleeping for 10 minute...")
await asyncsleep(600)
@bot.event
async def on_resumed():
now = datetime.now()
dt_string = now.strftime("[%d/%m - %H:%M:%S] ")
print(dt_string + "{} resumed".format(bot.user.name))
#------------------------- SHARDS ---------------------------------------------------------
@bot.event
async def on_shard_ready(shard_id):
now = datetime.now()
dt_string = now.strftime("[%d/%m - %H:%M:%S] ")
print(dt_string + "{0} Shard[{1}] ready".format(bot.user.name, shard_id))
@bot.event
async def on_shard_connect(shard_id):
now = datetime.now()
dt_string = now.strftime("[%d/%m - %H:%M:%S] ")
print(dt_string + "{0} Shard[{1}] connected".format(bot.user.name, shard_id))
@bot.event
async def on_shard_disconnect(shard_id):
now = datetime.now()
dt_string = now.strftime("[%d/%m - %H:%M:%S] ")
print(dt_string + "{0} Shard[{1}] disconnected".format(bot.user.name, shard_id))
try:
print(dt_string + "{0} Shard[{1}] reconnecting".format(bot.user.name, shard_id))
shard = bot.get_shard(shard_id)
if shard.is_closed() and not shard.is_ws_ratelimited():
print(dt_string + f"Shard[{shard_id}] connection is closed but not rate limited. Trying to connect()")
await shard.reconnect()
now2 = datetime.now()
dt_string2 = now2.strftime("[%d/%m - %H:%M:%S] ")
print(dt_string2 + "Reconnected. Sleeping for 10 minutes...")
await asyncsleep(600)
elif not shard.is_ws_ratelimited():
print(dt_string + f"Shard[{shard_id}] connection is not closed nor rate limited. Trying to reconnect()")
await shard.disconnect()
await shard.reconnect()
now2 = datetime.now()
dt_string2 = now2.strftime("[%d/%m - %H:%M:%S] ")
print(dt_string2 + "Reconnected. Sleeping for 10 minutes...")
await asyncsleep(600)
except Exception as e:
print(dt_string + f"Error while reconnecting Shard[{shard_id}]:",e)
#finally:
# await asyncio.sleep(600)
@bot.event
async def on_shard_resumed(shard_id):
now = datetime.now()
dt_string = now.strftime("[%d/%m - %H:%M:%S] ")
print(dt_string + "{0} Shard[{1}] resumed".format(bot.user.name, shard_id))
#--------------------------------- OTHER ----------------------------------------------------------
@bot.event
async def on_http_ratelimit(limit, remaining, reset_after, bucket, scope):
now = datetime.now()
dt_string = now.strftime("[%d/%m - %H:%M:%S] ")
print(dt_string + "{0} RATE LIMITED. Retry in {1}".format(bot.user.name, reset_after))
@bot.event
async def on_global_http_ratelimit(retry_after):
now = datetime.now()
dt_string = now.strftime("[%d/%m - %H:%M:%S] ")
print(dt_string + "{0} GLOBAL RATE LIMITED. Retry in {1}".format(bot.user.name, retry_after))
#---------------------------------- FUNCTIONS ------------------------------------------------------------------
@bot.slash_command(name="ammo", description="Get round info such as damage, speed, weight, modifiers etc...")
async def ammo(interaction:Interaction, name: str):
try:
await functions.ammo(interaction, name)
except Exception as e:
print("[-] ammo(main.py) error:",e)
print("Caused by:", interaction.user.name,"User ID:",interaction.user.id," with input:",name)
@ammo.on_autocomplete("name")
async def ammo_autocomplete(interaction:Interaction, name: str):
try:
await functions.ammo_autocomplete(interaction, name)
except Exception as e:
print("[-] ammo_autocomplete(main.py) error:",e)
print("Caused by:", interaction.user.name,"User ID:",interaction.user.id," with input:",name)
@bot.slash_command(name="item", description="Get item informations such as tier,price,etc...")
async def item(interaction:Interaction, name: str):
try:
await functions.item(interaction, name)
except Exception as e:
print("[-] item(main.py) error:",e)
print("Caused by:", interaction.user.name,"User ID:",interaction.user.id," with input:",name)
@item.on_autocomplete("name")
async def item_autocomplete(interaction:Interaction, name: str):
try:
await functions.item_autocomplete(interaction, name)
except Exception as e:
print("[-] item_autocomplete(main.py) error:",e)
print("Caused by:", interaction.user.name,"User ID:",interaction.user.id," with input:",name)
@bot.slash_command(name="auto", description="Automatically scans items from an image and returns their price.")
async def auto(interaction:Interaction, image:Attachment):
try:
await functions.auto(interaction, image)
except Exception as e:
print("[-] auto(main.py) error:",e)
print("Caused by:", interaction.user.name,"User ID:",interaction.user.id," with input image:",image.url)
#informative
@bot.slash_command(name="boss", description="Get boss and guards information.")
async def boss(interaction:Interaction, name: str):
try:
await informative.boss(interaction, name)
except Exception as e:
print("[-] boss(main.py) error:",e)
print("Caused by:", interaction.user.name,"User ID:",interaction.user.id," with input:",name)
@boss.on_autocomplete("name")
async def boss_autocomplete(interaction:Interaction, name: str):
try:
await informative.boss_autocomplete(interaction, name)
except Exception as e:
print("[-] boss_autocomplete(main.py) error:",e)
print("Caused by:", interaction.user.name,"User ID:",interaction.user.id," with input:",name)
@bot.slash_command(name="help", description="Get all the commands description.")
async def help(interaction:Interaction):
await informative.help(interaction)
@bot.slash_command(name="tiers", description="Informations on the tiers system.")
async def tiers(interaction:Interaction):
await informative.tiers(interaction)
@bot.slash_command(name="patchnotes", description="Get the latest patchnotes.")
async def patchnotes(interaction:Interaction):
await informative.patchnotes(interaction)
@bot.slash_command(name="serverstatus", description="Check if the servers are having some issues.")
async def serverstatus(interaction:Interaction):
await informative.serverstatus(interaction)
@bot.slash_command(name="bug", description="Report a bug.") #if not showing add guild_ids = [1059571834451931188]
async def bug(interaction:Interaction):
await interaction.response.send_modal(BugModal())
#Bug Report Class
class BugModal (Modal):
def __init__(self):
super().__init__(
"Report a Bug",
)
self.emTitle = TextInput(label = "Title", min_length = 2, max_length = 124, required = True, placeholder = "Enter the title here.")
self.add_item(self.emTitle)
self.emDesc = TextInput(label = "Description", min_length = 5, max_length = 4000, required = True, placeholder = "Explain what is the bug and how we can replicate it.", style = TextInputStyle.paragraph)
self.add_item(self.emDesc)
async def callback(self, interaction:Interaction) -> None:
title = self.emTitle.value
desc = self.emDesc.value
username = f"{interaction.user.name}#{interaction.user.discriminator}"
embed = Embed(title = title, description = desc)
embed.set_footer(text = "Reported by: "+username)
admin_user = await bot.fetch_user(483759864527454238)
await admin_user.send(embed = embed)
embed1 = Embed(title = "Bug report successful!🪳", description = "Thanks you! This bug will be verified and if it comes out as real you will get the Bug Hunter role in the official server!")
await interaction.response.send_message(embed=embed1, ephemeral=True)
def runBot():
try:
internal.tclear()
bot.run(TOKEN)
except LoginFailure:
print("[-] Invalid token provided, check that you have inserted a valid token in the .env.")
sys.exit(0)
except HTTPException as e:
retry_after = e.response.headers.get('Retry-After')
if retry_after:
print(f"ERROR: 429 - BLOCKED BY RATE LIMITS - Retry in {retry_after/60} minutes")
sleep(retry_after)
else:
retry_after = 300
print(f"ERROR: 429 - BLOCKED BY RATE LIMITS - Retry in {retry_after/60} minutes")
sleep(retry_after)
finally:
exit()
runBot()