-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
240 lines (213 loc) · 7.19 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
import asyncio
import logging
import os
import aiohttp
import traceback
import time
import discord
from discord import app_commands
from discord.ext import commands
from extensions.commands.giveaway import giveaway_views
from extensions.commands.ticket_system import (
closed_ticket_views,
panel_views,
ticket_views,
)
from utils import DataManager, cooldown_error
DataManager.setup(
[
(
"data/config.json",
{
"token": None,
"postgres_user": None,
"postgres_password": None,
"postgres_database": None,
"giphy_key": None,
"unsplash_key": None,
"weather_api_key": None,
},
),
(
"data/economy.json",
{
"items": {
"Example Item": {
"name": "Example Item",
"description": "> This is an example item, not meant to be used",
"type": "Example",
"sell price": 0,
"buy price": 0,
"emoji": "Use a discord emoji here",
"emoji_id": 0,
},
},
"hunting items": {
"Example Item": {"chance": 0},
},
"fishing items": {
"Example Item": {"chance": 0},
},
"shop items": {
"Example Item": {"price": 0},
},
},
),
(
"data/mine/mines.json",
{
"example mine": {
"requiredLevel": 0,
"mainOre": "example ore",
"secondaryOre": "example ore 2",
"emoji": "Use a discord emoji here",
"resources": {
"example resource": {"min": 1, "max": 2},
},
},
},
),
(
"data/mine/ores.json",
{
"ores": {
"example ore": {
"name": "Example Ore",
"emoji": "Use a discord emoji here",
"type": "Example type (e.g material, consumable, etc)",
"xp": 0,
},
},
},
),
]
)
class bot(commands.Bot):
def __init__(self):
intents = discord.Intents().all()
super().__init__(
command_prefix="'",
owner_id=705435835306213418,
intents=intents,
)
async def setup_hook(self) -> None:
for root, _, files in os.walk("extensions"):
for file in files:
if file.endswith(".py"):
extension_name = root.replace("\\", ".") + "." + file[:-3]
if extension_name in bot.extensions:
await bot.unload_extension(extension_name)
try:
await bot.load_extension(extension_name)
except discord.ext.commands.errors.ExtensionAlreadyLoaded:
await bot.reload_extension(extension_name)
await bot.tree.sync()
self.add_view(giveaway_views(bot))
self.add_view(panel_views(bot))
tickets = await DataManager.get_all_tickets()
panels = await DataManager.get_all_panels()
try:
for panel in panels:
for ticket in tickets:
self.add_view(
view=closed_ticket_views(
bot,
panel["id"],
ticket["ticket_creator"],
ticket["ticket_id"],
)
)
self.add_view(
view=ticket_views(bot, panel["id"], ticket["ticket_creator"])
)
except Exception as e:
print(e)
bot = bot()
bot.remove_command("help")
handler = logging.FileHandler(
filename="data/discord.log",
encoding="utf-8",
mode="w",
)
if "fonts" not in os.listdir("."):
os.mkdir("fonts")
@bot.event
async def on_ready():
await bot.change_presence(
status=discord.Status.online,
activity=discord.Game(f"/help | https://discord.gg/VsDDf8YKBV"),
)
@bot.tree.error
async def on_app_command_error(
interaction: discord.Interaction, error: app_commands.AppCommandError
):
await interaction.response.defer(ephemeral=True)
if isinstance(error, app_commands.errors.MissingPermissions):
return await interaction.edit_original_response(
embed=discord.Embed(
description="<:white_cross:1096791282023669860> You are missing the required permissions to use this command.",
colour=discord.Colour.red(),
),
)
elif isinstance(error, app_commands.CommandOnCooldown):
await interaction.edit_original_response(
embed=discord.Embed(
description=f"<:white_cross:1096791282023669860> You can use this command again <t:{int(time.time() + error.retry_after)}:R>",
colour=discord.Colour.red(),
),
)
await asyncio.sleep(error.retry_after)
try:
return await interaction.delete_original_response()
except:
pass
elif isinstance(error.original, cooldown_error):
await interaction.edit_original_response(
embed=discord.Embed(
description=f"{error.original.error_message}, try again <t:{int(time.time() + error.original.time_left)}:R>",
colour=discord.Colour.red(),
),
)
await asyncio.sleep(error.original.time_left)
try:
return await interaction.delete_original_response()
except:
pass
else:
logging.error(f"An error occurred: {error}")
@bot.event
async def on_command_error(ctx: commands.Context, error: commands.CommandError):
if isinstance(error, commands.CommandNotFound):
return
if isinstance(error, commands.NotOwner):
return
else:
return await bot.get_user(bot.owner_id).send(
embed=discord.Embed(
description=f"```py\n{traceback.format_exc()}\n```",
colour=discord.Colour.red(),
)
)
async def main():
await DataManager.initialise()
if None in [
DataManager.get("config", key)
for key in [
"token",
"postgres_user",
"postgres_password",
"postgres_database",
"giphy_key",
"unsplash_key",
"weather_api_key",
]
]:
print(f"Please fill out the config.json file before running {__file__}")
else:
discord.utils.setup_logging(handler=handler, level=logging.INFO)
await bot.start(DataManager.get("config", "token"))
if __name__ == "__main__":
try:
asyncio.run(main())
except aiohttp.client_exceptions.ClientConnectorError as e:
logging.warning(f"A network error occurred: {e}")