-
Notifications
You must be signed in to change notification settings - Fork 0
/
spooncraft_cog.py
161 lines (139 loc) · 5.57 KB
/
spooncraft_cog.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
import discord
import requests
from discord import app_commands
from discord.ext import commands
from bot import GwaffBot
from custom_logger import Logger
from database_mc import DatabaseMinecraft
from permissions import require_admin
logger = Logger('gwaff.bot.spooncraft')
def update_data(url, new_data):
headers = {'Content-Type': 'application/json', "User-Agent": "Mozilla/5.0"}
response = requests.post(url, json=new_data, headers=headers)
if response.status_code == 200:
logger.info(f"Data updated successfully: {response.json()}")
return True
else:
logger.info(f"Failed to update data: {response.status_code, response.text}")
return False
class SpooncraftCog(commands.GroupCog, group_name='spooncraft'):
def __init__(self, bot: GwaffBot):
"""
Initialises the SpooncraftCog with the bot instance and schedules tasks.
Args:
bot (GwaffBot): The bot instance.
"""
self.bot: GwaffBot = bot
# Upload the data every day at midnight
self.bot.schedule_task(
self.upload,
trigger="cron",
hour=0,
timezone="Australia/Brisbane"
)
# Update names every month
self.bot.schedule_task(
self.update_names,
trigger="cron",
hour=23,
timezone="Australia/Brisbane",
day='last'
)
async def upload(self) -> bool:
"""
Asynchronously uploads Spooncraft data and logs the process.
"""
dbm = DatabaseMinecraft()
logger.info("Starting upload")
if self.bot.logging_channel:
data = dbm.to_json()
result = update_data("https://gwaff.uqcloud.net/api/spooncraft", data)
if result:
logger.info("Upload completed successfully!")
return True
else:
logger.warning(f"Could not find required channel")
await self.bot.logging_channel.send("SC data upload failed")
logger.warning(f"Upload failed!")
return False
async def update_names(self) -> None:
"""
Asynchronously updates Minecraft names and logs the process.
"""
# TODO: Fix blocking
logger.info("Starting update")
if self.bot.logging_channel:
await self.bot.logging_channel.send("Updating names now!")
else:
logger.warning(f"Could not find required channel")
return
dbm = DatabaseMinecraft()
success, total = await dbm.update_all_mc_names()
if self.bot.logging_channel:
await self.bot.logging_channel.send(
f"Finished updating names with {total - success} fails out of {total}!")
logger.info(f"Finished updating names with {total - success} fails out of {total}!")
@app_commands.command(name="upload",
description="(Admin only) Upload the Spooncraft data")
@require_admin
async def command_upload(self, interaction: discord.Interaction) -> None:
"""
Command to upload Spooncraft data.
Args:
interaction (discord.Interaction): The interaction object.
"""
await interaction.response.defer(ephemeral=True)
# dbm: DatabaseMinecraft = DatabaseMinecraft()
result = await self.upload()
if result:
await interaction.followup.send("Data uploaded successfully!")
else:
await interaction.followup.send("Data upload failed!")
@app_commands.command(name="updateall",
description="(Admin only) Update Spooncraft MC names")
@require_admin
async def command_updateall(self, interaction: discord.Interaction) -> None:
"""
Command to update all Minecraft names.
Args:
interaction (discord.Interaction): The interaction object.
"""
# TODO: Fix blocking
await interaction.response.defer(ephemeral=True)
dbm = DatabaseMinecraft()
msg: discord.WebhookMessage = await interaction.followup.send("Starting the update")
success, total = await dbm.update_all_mc_names()
await msg.edit(
content=f"Finished updating names with {total - success} fails out of {total}!")
@app_commands.command(name="add",
description="(Admin only) Add a Spooncraft player")
@require_admin
async def command_add(self, interaction: discord.Interaction,
member: discord.User,
uuid: str,
name: str = None) -> None:
"""
Command to add a Spooncraft player to the database.
Args:
interaction (discord.Interaction): The interaction object.
member (discord.User): The Discord user to add.
uuid (str): The UUID of the Minecraft player.
name (str, optional): The name of the Minecraft player. Defaults to None.
"""
await interaction.response.defer(ephemeral=True)
dbm = DatabaseMinecraft()
dbm.add_user(member.id, uuid, name)
dbm.commit()
if name:
await interaction.followup.send(
f"Added user {member.mention} with UUID {uuid} and name {name}")
else:
await interaction.followup.send(f"Added user {member.mention} with UUID {uuid}")
async def setup(bot: GwaffBot) -> None:
"""
Sets up the SpooncraftCog and adds it to the bot.
Args:
bot (GwaffBot): The bot instance.
"""
cog = SpooncraftCog(bot)
await bot.add_cog(cog, guilds=bot.guilds)