-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
144 lines (121 loc) · 4.54 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
import discord
import os
from dotenv import load_dotenv
from discord import app_commands
from user import Hunter
from scoreboard_calculator import calculate_scoreboard
from scoreboard_calculator import calculate_scoreboard_around_hunter
import threading
import pickle
from table2ascii import table2ascii as t2a, PresetStyle
from command_scheduler import scheduler
from command_type import command_type as ct
from command import command
load_dotenv()
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client)
TOKEN = str(os.getenv("BOT_TOKEN"))
SERVERID = int(os.getenv("SERVER_ID"))
error_counter = 0
directory = os.fsencode("Hunters")
cmd_sched = scheduler()
thread = threading.Thread(target=cmd_sched.run)
thread.start()
print("Starting Bot")
@tree.command(
name="register",
description="Registers a new Achievement Hunter. It may take a few minutes.",
guild = discord.Object(id=SERVERID)
)
async def register(interaction, steam_id : str):
message : str
try:
if(steam_id.lower()+".hunt" in os.listdir("Hunters")):
message="SteamID already Registered. Use /score to see the recorded score."
else:
cmd_sched.queue_command(command(ct.REGISTER, steam_id))
message="Achievement Hunter is being registered. This may take a while."
except Exception as e:
message="Registration failed. Please Verify the SteamID is written correctly. Alternatively, send us a ticket using /error."
e.with_traceback()
await interaction.response.send_message(message)
@tree.command(
name="update",
description="Update Single Hunter",
guild=discord.Object(id=SERVERID)
)
async def update(interaction, steam_id : str):
try:
if(steam_id+".hunt" not in os.listdir("Hunters")):
message = "User not Registered. Use /register to register a new Achievement Hunter"
else:
print("Scheduling Update")
cmd_sched.queue_command(command(ct.UPDATE, steam_id))
message="Achievement Hunter is being updated. This may take a while."
except Exception as e:
message="Registration failed. Please Verify the SteamID is written correctly. Alternatively, send us a ticket using /error."
e.with_traceback()
await interaction.response.send_message(message)
@tree.command(
name="scoreboard",
description="Shows the Scoreboard",
guild = discord.Object(id=SERVERID)
)
async def scoreboard(interaction):
output = t2a(
header=["Rank","Hunter", "Score"],
body=calculate_scoreboard(),
style=PresetStyle.thin_compact
)
await interaction.response.send_message(f"```\n{output}\n```")
@tree.command(
name="scoreboard_hunter",
description="Shows the Scoreboard around an individual",
guild = discord.Object(id=SERVERID)
)
async def scoreboard_hunter(interaction, steam_id : str):
output = t2a(
header=["Rank","Hunter", "Score"],
body=calculate_scoreboard_around_hunter(steam_id),
style=PresetStyle.thin_compact
)
await interaction.response.send_message(f"```\n{output}\n```")
@tree.command(
name = "score",
description="Shows the score of a given SteamID",
guild = discord.Object(id=SERVERID)
)
async def score(interaction, steam_id : str):
print("Scoring")
message : str
if(steam_id.lower()+".hunt" not in os.listdir("Hunters")):
print("Hunter not Registered")
message = "User not Registered. Use /register to register a new Achievement Hunter"
else:
with open("Hunters/"+steam_id.lower()+".hunt", "rb") as file:
hunter : Hunter = pickle.load(file)
message = str(hunter.score)
await interaction.response.send_message(message)
@tree.command(
name = "error",
description = "Send us information about a error you encountered",
guild = discord.Object(id=SERVERID)
)
async def error(interaction, error_message : str, error_counter : int = error_counter):
with open("Errors/error-" + str(error_counter), "w") as file:
file.write(error_message)
error_counter += 1
await interaction.response.send_message("Error has been reported")
@client.event
async def on_ready():
await tree.sync(guild=discord.Object(id=SERVERID))
print('We have logged in as {0.user}'.format(client))
#@client.event
#async def on_message(message):
# if message.author == client.user:
# return
# if message.content.startswith('$hello'):
# await message.channel.send('Hello!')
client.run(TOKEN)