-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
243 lines (174 loc) · 7.33 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
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
import time
import discord
from discord.ext import commands
import random
from auth import *
from random import randint
import json
bot = commands.Bot(command_prefix="!", intents=discord.Intents.all())
@bot.event
async def on_ready():
global guild
guild = bot.get_guild(727588974658322442)
loggers_emoji = "<:loggers:860219204828528671>"
def neededXpToLvlUp(lvl, rebirths):
return round((0.5 * (lvl ** 2) + lvl * 2) * 25 + (rebirths ** 1.1))
def neededLvlsForRebirth(rebirths):
return round(rebirths ** 0.8) + 5
def rebirthBonus(rebirths):
return round(rebirths ** 1.2)
def xpAdded(rebirths):
return round((randint(5, 20) + rebirthBonus(rebirths)))
@bot.command()
async def level(ctx):
userID = str(ctx.message.author.id)
with open("data.json", "r") as file:
data = json.load(file)
lvl = data[userID]["lvl"]
xp = data[userID]["xp"]
rebirths = data[userID]["rebirths"]
await ctx.send(
f"""{ctx.message.author.mention}, dina stats lyder som följande:
LvL: {lvl}
XP: {xp}/{neededXpToLvlUp(lvl, rebirths)}
Rebirths: {rebirths}
Rebirth-bonus: +{rebirthBonus(rebirths)} XP/msg"""
)
@bot.command()
async def rebirth(ctx):
userID = str(ctx.message.author.id)
with open("data.json", "r+") as file:
data = json.load(file)
lvl = data[str(userID)]["lvl"]
rebirths = data[str(userID)]["rebirths"]
if lvl >= neededLvlsForRebirth(rebirths):
data[userID]["lvl"] = 1
data[userID]["xp"] = 0
data[userID]["rebirths"] += 1
# Resets level and XP but adds one rebirth
rebirths = data[userID]["rebirths"]
# Defines the amount of rebirths as a varible, to send and
# print it later.
file.seek(0)
# Sometimes, `json.dump` dumps at the end of the file.
# `file.seek(0)` assures that `json.dump()` overwrites the
# file, starting at the beginning.
json.dump(data, file, indent=4)
file.truncate()
# Removes the rest of the file, even if there isn't any.
# This is needed if `json.dump` doesn't overwrite the file,
# and instead jsut writes new content before it. If so, the
# old content gets removed.
rebirths = data[userID]["rebirths"]
await ctx.channel.send(
f"Oh lärkar, {ctx.message.author.mention}! Du rebirthar. Fine."
+ " Det är helt okej, så länge du inte blandar in mig... "
+ "Förstått? I vilket fall som helst så har du nu följande ..."
+ f" typ bonuses (eller nåt sånt):\n "
+ f"+{rebirthBonus(rebirths)} XP per meddelande"
)
for lvl in range(lvl + 1):
try:
await removeRole(userID, ("LvL " + str(lvl + 1)))
except:
pass
# Removes the level-earned roles from the user, testing
# all possible roles they could have (1->lvl + 1).
if rebirths == 1:
grammarCase = "Rebirth"
# In the case that this' the user's first rebirth, for the
# string to be grammatically correct, `grammarCase` gets
# defined as a singular "Rebirth".
else:
grammarCase = "Rebirths"
# `GrammarCase` gets defines as a plural
try:
await addRole(userID, f"{rebirths} {grammarCase}")
except:
pass
else:
if neededLvlsForRebirth(rebirths) - lvl == 1:
# In the case that there's only level left, for the
# string to be grammatically correct, `grammarCase` gets
# defined as a singular "level".
grammarCase = "level" # "Level" in Swedish
else:
grammarCase = "levlar" # "Levels" in Swedish
# `GrammarCase` gets defines as a plural
if userID == 410123402196811806:
# Me, the creator - Temerold. Instead of giving me an
# attitude, it treats me with respect. Since I'm its creator.
await ctx.channel.send(
f"Wow! {ctx.message.author.mention}, chilla! Jag vet att "
+ "du äger stället, och att det är dina regler som gäller "
+ f"här, men du är i level {lvl}. Du behöver komma upp i "
+ f"minst {neededLvlsForRebirth(rebirths)} levlar. Du har"
+ f" alltså {neededLvlsForRebirth(rebirths) - lvl} "
+ f"{grammarCase} kvar."
)
else:
await ctx.channel.send(
f"Wow! {ctx.message.author.mention}, chilla! Kom inte in "
+ "som om du äger stället. Mina regler gäller här, och du "
+ f"är i level {lvl}. Du behöver komma upp i, *minst*, "
+ f"{neededLvlsForRebirth(rebirths)} levlar. Du har alltså"
+ f" {neededLvlsForRebirth(rebirths) - lvl} {grammarCase} "
+ "kvar. Kom igen, din lata ek!"
)
# **An attitide**
@bot.event
async def on_message(message):
if str(message.author) != "penid#7202" and randint(1, 1) == 1:
userID = str(message.author.id)
with open("data.json", "r") as file:
data = json.load(file)
if userID not in data:
print("Added: " + userID)
data[userID] = {"lvl": 1, "xp": 0, "rebirths": 0}
# Creates new user with one level, zero XP, and zero rebirths
with open("data.json", "w") as file:
json.dump(data, file, indent=4)
await addXP(userID)
await levelUp(userID)
await bot.process_commands(message)
async def addXP(userID):
with open("data.json", "r") as file:
data = json.load(file)
data[userID]["xp"] += xpAdded(data[userID]["rebirths"])
# Adds xpAdded XP to the user
with open("data.json", "w") as file:
json.dump(data, file, indent=4)
async def levelUp(userID):
global loggers_emoji
with open("data.json", "r+") as file:
data = json.load(file)
lvl = data[userID]["lvl"]
xp = data[userID]["xp"]
rebirths = data[userID]["rebirths"]
if xp >= neededXpToLvlUp(lvl, rebirths):
data[userID]["lvl"] += 1
data[userID]["xp"] = 0
with open("data.json", "w") as file2:
json.dump(data, file2, indent=4)
await bot.get_channel(727588975681863731).send(
f"Loggers! {loggers_emoji} <@{userID}> kom upp i **level "
+ f"{lvl + 1}**! "
+ (f"{loggers_emoji} ") * 6
)
print(f"{userID} kom upp i level {lvl + 1}")
try:
await addRole(userID, ("LvL " + str(lvl)))
except:
pass
async def addRole(userID, role):
global guild
print(guild)
for member in guild.members:
if int(member.id) == int(userID):
await member.add_roles(discord.utils.get(guild.roles, name=role))
async def removeRole(userID, role):
global guild
for member in guild.members:
if int(member.id) == int(userID):
await member.remove_roles(discord.utils.get(guild.roles, name=role))
bot.run(token)