This commit is contained in:
2022-02-23 10:52:55 +00:00
parent abb759e311
commit 419dabb564

812
main.py
View File

@@ -1,402 +1,410 @@
import json import os #hi there
import discord def write_pid():
from discord.ext import commands pid = os.getpid()
import random with open('/tmp/maze.pid', 'w') as f:
f.write(str(pid))
client = commands.Bot(command_prefix="go ") print('Hello my(i \'m mazebot) pid is ' + str(pid))
icons = {"player": "", "goal": "", "blank": " "} write_pid()
blanks = []
player = [0, 0] import json
end = [0, 0] import discord
columns = 30 from discord.ext import commands
rows = 10 import random
lvl = 0
walls = [] client = commands.Bot(command_prefix="go ")
walls1 = [] icons = {"player": "", "goal": "", "blank": " "}
generated = False blanks = []
empty = list(range(1, rows - 1, 2)) player = [0, 0]
end = [0, 0]
async def justcheck(ctx): columns = 30
rows = 10
role, to_delete = check_lvl(ctx) lvl = 0
walls = []
try: walls1 = []
generated = False
for roleX in ctx.guild.roles: empty = list(range(1, rows - 1, 2))
if roleX.name == role:
role = roleX async def justcheck(ctx):
if role not in ctx.author.roles: role, to_delete = check_lvl(ctx)
await ctx.author.add_roles(role)
await ctx.channel.send(f"Congratulations to <@{ctx.author.id}> for getting role {role.name}") try:
for r in ctx.guild.roles: for roleX in ctx.guild.roles:
if r.name in to_delete: if roleX.name == role:
try: role = roleX
await ctx.author.remove_roles(r)
except Exception as e: if role not in ctx.author.roles:
await ctx.send(e) await ctx.author.add_roles(role)
except Exception as e: await ctx.channel.send(f"Congratulations to <@{ctx.author.id}> for getting role {role.name}")
await ctx.send(e)
for r in ctx.guild.roles:
def check_lvl(ctx): if r.name in to_delete:
role = None try:
await ctx.author.remove_roles(r)
with open("data.json", "r") as f: except Exception as e:
levels = json.load(f) await ctx.send(e)
except Exception as e:
lvel = int(levels[str(ctx.guild.id)][str(ctx.author.id)]) await ctx.send(e)
role_names = ["|maze noob|", def check_lvl(ctx):
"|maze novice|", role = None
"|maze gamer|",
"|small maze progamer|", with open("data.json", "r") as f:
"|medium maze progamer|", levels = json.load(f)
"|big maze progamer|",
"|giant maze progamer|", lvel = int(levels[str(ctx.guild.id)][str(ctx.author.id)])
"|extreme maze progamer|",
"|ultra maze progamer|", role_names = ["|maze noob|",
"|giant extreme maze progamer|", "|maze novice|",
"|giant extreme ultra maze progamer|"] "|maze gamer|",
lower_roles = 10 "|small maze progamer|",
"|medium maze progamer|",
if lvel >= 100000: "|big maze progamer|",
role = role_names[-1] "|giant maze progamer|",
lower_roles -= 1 "|extreme maze progamer|",
elif lvel >= 40000: "|ultra maze progamer|",
role = role_names[-2] "|giant extreme maze progamer|",
lower_roles -= 2 "|giant extreme ultra maze progamer|"]
elif lvel >= 20000: lower_roles = 10
role = role_names[-3]
lower_roles -= 3 if lvel >= 100000:
elif lvel >= 10000: role = role_names[-1]
role = role_names[-4] lower_roles -= 1
lower_roles -= 4 elif lvel >= 40000:
elif lvel >= 5000: role = role_names[-2]
role = role_names[-5] lower_roles -= 2
lower_roles -= 1 elif lvel >= 20000:
elif lvel >= 1000: role = role_names[-3]
role = role_names[-6] lower_roles -= 3
lower_roles -= 2 elif lvel >= 10000:
elif lvel >= 500: role = role_names[-4]
role = role_names[-7] lower_roles -= 4
lower_roles -= 3 elif lvel >= 5000:
elif lvel >= 250: role = role_names[-5]
role = role_names[-8] lower_roles -= 1
lower_roles -= 4 elif lvel >= 1000:
elif lvel >= 100: role = role_names[-6]
role = role_names[-9] lower_roles -= 2
lower_roles -= 5 elif lvel >= 500:
elif lvel >= 50: role = role_names[-7]
role = role_names[-10] lower_roles -= 3
lower_roles -= 6 elif lvel >= 250:
role = role_names[-8]
roles_to_remove = [] lower_roles -= 4
for r in range(lower_roles): elif lvel >= 100:
roles_to_remove.append(role_names[r]) role = role_names[-9]
lower_roles -= 5
return role, roles_to_remove elif lvel >= 50:
role = role_names[-10]
def generate_maze(): lower_roles -= 6
global player
global blanks roles_to_remove = []
global walls1 for r in range(lower_roles):
global generated roles_to_remove.append(role_names[r])
for _ in range(rows):
walls1.append([]) return role, roles_to_remove
counter2 = 0
for x in walls1: def generate_maze():
for _ in range(columns): global player
if counter2 not in empty: global blanks
x.append("|") global walls1
else: global generated
x.append(" ") for _ in range(rows):
counter2 += 1 walls1.append([])
for x in walls1: counter2 = 0
kde = random.randint(0, columns - 1) for x in walls1:
blanks.append(kde) for _ in range(columns):
x[kde] = icons["blank"] if counter2 not in empty:
player[0] = rows - 2 x.append("|")
player[1] = blanks[rows - 2] else:
end[0] = 0 x.append(" ")
end[1] = blanks[0] counter2 += 1
generated = True for x in walls1:
kde = random.randint(0, columns - 1)
blanks.append(kde)
def display_maze(): x[kde] = icons["blank"]
global generated player[0] = rows - 2
if not generated: player[1] = blanks[rows - 2]
generate_maze() end[0] = 0
end[1] = blanks[0]
global lvl generated = True
global player
global walls1
walls2 = [] def display_maze():
for x in walls1: global generated
walls2.append(x) if not generated:
walls_display = [] generate_maze()
counter1 = 0
for x in walls1: global lvl
kde = blanks[counter1] global player
x[kde] = icons["blank"] global walls1
counter1 += 1 walls2 = []
for x in walls1:
walls2[player[0]][player[1]] = icons["player"] walls2.append(x)
walls2[end[0]][end[1]] = icons["goal"] walls_display = []
for x in range(0, rows - 1): counter1 = 0
for i in range(0, columns - 1): for x in walls1:
walls_display.append(" ") kde = blanks[counter1]
walls_display.append(walls1[x][i]) x[kde] = icons["blank"]
walls_display.append("\n") counter1 += 1
walls_s = f"LEVEL: {lvl}\n"
for x in walls_display: walls2[player[0]][player[1]] = icons["player"]
walls_s += x walls2[end[0]][end[1]] = icons["goal"]
print(len(walls_s)) for x in range(0, rows - 1):
if len(walls_s) > 2000: for i in range(0, columns - 1):
raise Exception("Too long") walls_display.append(" ")
return walls_s walls_display.append(walls1[x][i])
walls_display.append("\n")
walls_s = f"LEVEL: {lvl}\n"
async def go_left(): for x in walls_display:
if not generated: walls_s += x
generate_maze() print(len(walls_s))
where = [0, 0] if len(walls_s) > 2000:
where[0] = player[0] raise Exception("Too long")
where[1] = player[1] - 1 return walls_s
if player[1] != 0:
if walls1[where[0]][where[1]] == icons["blank"]:
walls1[player[0]][player[1]] = icons["blank"] async def go_left():
player[0] = where[0] if not generated:
player[1] = where[1] generate_maze()
where = [0, 0]
where[0] = player[0]
async def go_right(): where[1] = player[1] - 1
if not generated: if player[1] != 0:
generate_maze() if walls1[where[0]][where[1]] == icons["blank"]:
where = [0, 0] walls1[player[0]][player[1]] = icons["blank"]
where[0] = player[0] player[0] = where[0]
where[1] = player[1] + 1 player[1] = where[1]
if player[1] != columns - 2:
if walls1[where[0]][where[1]] == icons["blank"]:
walls1[player[0]][player[1]] = icons["blank"] async def go_right():
player[0] = where[0] if not generated:
player[1] = where[1] generate_maze()
where = [0, 0]
where[0] = player[0]
async def go_up(ctx): where[1] = player[1] + 1
global lvl if player[1] != columns - 2:
global generated if walls1[where[0]][where[1]] == icons["blank"]:
if not generated: walls1[player[0]][player[1]] = icons["blank"]
generate_maze() player[0] = where[0]
f = open("data.json", "r") player[1] = where[1]
js = json.load(f)
f.close()
if not str(ctx.guild.id) in js: async def go_up(ctx):
js[str(ctx.guild.id)] = {} global lvl
js[str(ctx.guild.id)]["LVL"] = 0 global generated
js[str(ctx.guild.id)][str(ctx.author.id)] = 0 if not generated:
if not str(ctx.author.id) in js[str(ctx.guild.id)]: generate_maze()
js[str(ctx.guild.id)][str(ctx.author.id)] = 0 f = open("data.json", "r")
js = json.load(f)
where = [0, 0] f.close()
where[0] = player[0] - 1 if not str(ctx.guild.id) in js:
where[1] = player[1] js[str(ctx.guild.id)] = {}
if player[1] == end[1] and player[0] == end[0]: js[str(ctx.guild.id)]["LVL"] = 0
js[str(ctx.guild.id)][str(ctx.author.id)] += 10 js[str(ctx.guild.id)][str(ctx.author.id)] = 0
js[str(ctx.guild.id)]["LVL"] += 1 if not str(ctx.author.id) in js[str(ctx.guild.id)]:
generated = False js[str(ctx.guild.id)][str(ctx.author.id)] = 0
lvl = js[str(ctx.guild.id)]["LVL"]
await ctx.send(display_maze()) where = [0, 0]
if walls1[where[0]][where[1]] == icons["blank"] or walls1[where[0]][where[1]] == icons["goal"]: where[0] = player[0] - 1
walls1[player[0]][player[1]] = icons["blank"] where[1] = player[1]
player[0] = where[0] if player[1] == end[1] and player[0] == end[0]:
player[1] = where[1] js[str(ctx.guild.id)][str(ctx.author.id)] += 10
js[str(ctx.guild.id)][str(ctx.author.id)] += 1 js[str(ctx.guild.id)]["LVL"] += 1
f = open("data.json", "w") generated = False
json.dump(js, f, indent=4) lvl = js[str(ctx.guild.id)]["LVL"]
f.close() await ctx.send(display_maze())
await justcheck(ctx) if walls1[where[0]][where[1]] == icons["blank"] or walls1[where[0]][where[1]] == icons["goal"]:
walls1[player[0]][player[1]] = icons["blank"]
@client.command() player[0] = where[0]
@commands.has_permissions(administrator=True) player[1] = where[1]
async def regenerate(ctx): js[str(ctx.guild.id)][str(ctx.author.id)] += 1
"""Regenerates the maze. Only admins can use it.""" f = open("data.json", "w")
generated = False json.dump(js, f, indent=4)
await ctx.send(display_maze()) f.close()
await justcheck(ctx)
@client.command()
async def redraw(ctx): @client.command()
"""Redraws the maze""" @commands.has_permissions(administrator=True)
await ctx.send(display_maze()) async def regenerate(ctx):
"""Regenerates the maze. Only admins can use it."""
@client.command() generated = False
async def left(ctx, times=1): await ctx.send(display_maze())
"""Goes left. You can specify how much: 1-30 Default 1"""
if times > columns: @client.command()
times = 30 async def redraw(ctx):
for i in range(times): """Redraws the maze"""
await go_left() await ctx.send(display_maze())
await ctx.send(display_maze())
@client.command()
@client.command() async def left(ctx, times=1):
async def right(ctx, times=1): """Goes left. You can specify how much: 1-30 Default 1"""
"""Goes right. You can specify how much: 1-30 Default 1""" if times > columns:
if times > columns: times = 30
times = 30 for i in range(times):
for i in range(times): await go_left()
await go_right() await ctx.send(display_maze())
await ctx.send(display_maze())
@client.command()
@client.command() async def right(ctx, times=1):
async def up(ctx, times=1): """Goes right. You can specify how much: 1-30 Default 1"""
"""Goes up. You can specify how much: 1-30 Default 1""" if times > columns:
if times > rows: times = 30
times = 30 for i in range(times):
for i in range(times): await go_right()
await go_up(ctx) await ctx.send(display_maze())
await ctx.send(display_maze())
@client.command()
@client.command() async def up(ctx, times=1):
async def get_level(ctx, member: discord.Member): """Goes up. You can specify how much: 1-30 Default 1"""
"""Gets the level of a member""" if times > rows:
f = open("data.json", "r") times = 30
js = json.load(f) for i in range(times):
f.close() await go_up(ctx)
if str(member.id) in js[str(ctx.guild.id)]: await ctx.send(display_maze())
await ctx.send(js[str(ctx.guild.id)][str(member.id)])
else: @client.command()
await ctx.send("Member not found.") async def get_level(ctx, member: discord.Member):
"""Gets the level of a member"""
@client.command() f = open("data.json", "r")
async def get_my_level(ctx): js = json.load(f)
"""Gets your own level""" f.close()
member = ctx.author if str(member.id) in js[str(ctx.guild.id)]:
f = open("data.json", "r") await ctx.send(js[str(ctx.guild.id)][str(member.id)])
js = json.load(f) else:
f.close() await ctx.send("Member not found.")
if str(member.id) in js[str(ctx.guild.id)]:
await ctx.send(js[str(ctx.guild.id)][str(member.id)]) @client.command()
else: async def get_my_level(ctx):
await ctx.send("Member not found.") """Gets your own level"""
member = ctx.author
@client.command() f = open("data.json", "r")
async def tell_about(ctx, member: discord.Member): js = json.load(f)
"""Tell your friend about mazebot! Type his name after the command.""" f.close()
await ctx.send(f"Hello <@{member.id}>!\nGo ahead and play the maze game!\nIt is a little buggy but you can win it!\nTry typing 'go help' to see the commands and 'go info'.\n<@{ctx.author.id}>") if str(member.id) in js[str(ctx.guild.id)]:
await ctx.send(js[str(ctx.guild.id)][str(member.id)])
@client.command() else:
async def info(ctx): await ctx.send("Member not found.")
"""Info how to play"""
await ctx.send(f"Hello <@{ctx.author.id}>!\n I see that you want help with this game.\nType redraw to draw the maze and use the commands to move.\nIf you cannot fit through a hole, I suggest to to 1-2 characters in each direction and trying again.\nGo ahead and play the maze game!\nIf you are stuck, your admin can regenerate the maze.\nIt is a little buggy but you can win it!\nTry typing 'go help' to see the commands.\n") @client.command()
async def tell_about(ctx, member: discord.Member):
@client.command() """Tell your friend about mazebot! Type his name after the command."""
async def add_roles(ctx): await ctx.send(f"Hello <@{member.id}>!\nGo ahead and play the maze game!\nIt is a little buggy but you can win it!\nTry typing 'go help' to see the commands and 'go info'.\n<@{ctx.author.id}>")
# adding level roles
@client.command()
role_names = ["|maze noob|", async def info(ctx):
"|maze novice|", """Info how to play"""
"|maze gamer|", await ctx.send(f"Hello <@{ctx.author.id}>!\n I see that you want help with this game.\nType redraw to draw the maze and use the commands to move.\nIf you cannot fit through a hole, I suggest to to 1-2 characters in each direction and trying again.\nGo ahead and play the maze game!\nIf you are stuck, your admin can regenerate the maze.\nIt is a little buggy but you can win it!\nTry typing 'go help' to see the commands.\n")
"|small maze progamer|",
"|medium maze progamer|", @client.command()
"|big maze progamer|", async def add_roles(ctx):
"|giant maze progamer|", # adding level roles
"|extreme maze progamer|",
"|ultra maze progamer|", role_names = ["|maze noob|",
"|giant extreme maze progamer|", "|maze novice|",
"|giant extreme ultra maze progamer|"] "|maze gamer|",
"|small maze progamer|",
names = [] "|medium maze progamer|",
for role in ctx.guild.roles: "|big maze progamer|",
names.append(role.name) "|giant maze progamer|",
"|extreme maze progamer|",
do_not_do = False "|ultra maze progamer|",
for role in role_names: "|giant extreme maze progamer|",
await ctx.send(f"Adding {role}...") "|giant extreme ultra maze progamer|"]
if role in names:
do_not_do = True names = []
await ctx.send(f"Exiting, role {role} exists.") for role in ctx.guild.roles:
try: names.append(role.name)
if do_not_do is False:
await ctx.guild.create_role(name=role_names[9], colour=discord.Color.from_rgb(255, 255, 255)) do_not_do = False
await ctx.guild.create_role(name=role_names[8], colour=discord.Color.from_rgb(0, 255, 0)) for role in role_names:
await ctx.guild.create_role(name=role_names[7], colour=discord.Color.from_rgb(255, 255, 0)) await ctx.send(f"Adding {role}...")
await ctx.guild.create_role(name=role_names[6], colour=discord.Color.from_rgb(255, 0, 255)) if role in names:
await ctx.guild.create_role(name=role_names[5], colour=discord.Color.from_rgb(200, 209, 33)) do_not_do = True
await ctx.guild.create_role(name=role_names[4], colour=discord.Color.from_rgb(178, 186, 28)) await ctx.send(f"Exiting, role {role} exists.")
await ctx.guild.create_role(name=role_names[3], colour=discord.Color.from_rgb(174, 219, 48)) try:
await ctx.guild.create_role(name=role_names[2], colour=discord.Color.from_rgb(207, 118, 227)) if do_not_do is False:
await ctx.guild.create_role(name=role_names[1], colour=discord.Color.from_rgb(30, 179, 72)) await ctx.guild.create_role(name=role_names[9], colour=discord.Color.from_rgb(255, 255, 255))
await ctx.guild.create_role(name=role_names[0], colour=discord.Color.from_rgb(22, 97, 43)) await ctx.guild.create_role(name=role_names[8], colour=discord.Color.from_rgb(0, 255, 0))
await ctx.send(f"Exiting, finished.") await ctx.guild.create_role(name=role_names[7], colour=discord.Color.from_rgb(255, 255, 0))
except Exception as e: await ctx.guild.create_role(name=role_names[6], colour=discord.Color.from_rgb(255, 0, 255))
await ctx.send(e) await ctx.guild.create_role(name=role_names[5], colour=discord.Color.from_rgb(200, 209, 33))
await ctx.guild.create_role(name=role_names[4], colour=discord.Color.from_rgb(178, 186, 28))
@client.command() await ctx.guild.create_role(name=role_names[3], colour=discord.Color.from_rgb(174, 219, 48))
async def delete_roles(ctx): await ctx.guild.create_role(name=role_names[2], colour=discord.Color.from_rgb(207, 118, 227))
role_names = ["|maze noob|", await ctx.guild.create_role(name=role_names[1], colour=discord.Color.from_rgb(30, 179, 72))
"|maze novice|", await ctx.guild.create_role(name=role_names[0], colour=discord.Color.from_rgb(22, 97, 43))
"|maze gamer|", await ctx.send(f"Exiting, finished.")
"|small maze progamer|", except Exception as e:
"|medium maze progamer|", await ctx.send(e)
"|big maze progamer|",
"|giant maze progamer|", @client.command()
"|extreme maze progamer|", async def delete_roles(ctx):
"|ultra maze progamer|", role_names = ["|maze noob|",
"|giant extreme maze progamer|", "|maze novice|",
"|giant extreme ultra maze progamer|"] "|maze gamer|",
"|small maze progamer|",
roles_to_remove = [] "|medium maze progamer|",
for role in ctx.guild.roles: "|big maze progamer|",
if role.name in role_names: "|giant maze progamer|",
await ctx.send(f"Removing {role.name}...") "|extreme maze progamer|",
roles_to_remove.append(role) "|ultra maze progamer|",
"|giant extreme maze progamer|",
for role in roles_to_remove: "|giant extreme ultra maze progamer|"]
try:
await ctx.guild.roles[ctx.guild.roles.index(role)].delete() roles_to_remove = []
except Exception as e: for role in ctx.guild.roles:
await ctx.send(e) if role.name in role_names:
await ctx.send(f"Exiting, finished.") await ctx.send(f"Removing {role.name}...")
roles_to_remove.append(role)
@client.event
async def on_guild_join(guild): for role in roles_to_remove:
role_names = ["|maze noob|", try:
"|maze novice|", await ctx.guild.roles[ctx.guild.roles.index(role)].delete()
"|maze gamer|", except Exception as e:
"|small maze progamer|", await ctx.send(e)
"|medium maze progamer|", await ctx.send(f"Exiting, finished.")
"|big maze progamer|",
"|giant maze progamer|", @client.event
"|extreme maze progamer|", async def on_guild_join(guild):
"|ultra maze progamer|", role_names = ["|maze noob|",
"|giant extreme maze progamer|", "|maze novice|",
"|giant extreme ultra maze progamer|"] "|maze gamer|",
"|small maze progamer|",
names = [] "|medium maze progamer|",
for role in guild.roles: "|big maze progamer|",
names.append(role.name) "|giant maze progamer|",
"|extreme maze progamer|",
do_not_do = False "|ultra maze progamer|",
for role in role_names: "|giant extreme maze progamer|",
print(f"Adding {role}...") "|giant extreme ultra maze progamer|"]
if role in names:
do_not_do = True names = []
print(f"Exiting, role {role} exists.") for role in guild.roles:
try: names.append(role.name)
if do_not_do is False:
await guild.create_role(name=role_names[9], colour=discord.Color.from_rgb(255, 255, 255)) do_not_do = False
await guild.create_role(name=role_names[8], colour=discord.Color.from_rgb(0, 255, 0)) for role in role_names:
await guild.create_role(name=role_names[7], colour=discord.Color.from_rgb(255, 255, 0)) print(f"Adding {role}...")
await guild.create_role(name=role_names[6], colour=discord.Color.from_rgb(255, 0, 255)) if role in names:
await guild.create_role(name=role_names[5], colour=discord.Color.from_rgb(200, 209, 33)) do_not_do = True
await guild.create_role(name=role_names[4], colour=discord.Color.from_rgb(178, 186, 28)) print(f"Exiting, role {role} exists.")
await guild.create_role(name=role_names[3], colour=discord.Color.from_rgb(174, 219, 48)) try:
await guild.create_role(name=role_names[2], colour=discord.Color.from_rgb(207, 118, 227)) if do_not_do is False:
await guild.create_role(name=role_names[1], colour=discord.Color.from_rgb(30, 179, 72)) await guild.create_role(name=role_names[9], colour=discord.Color.from_rgb(255, 255, 255))
await guild.create_role(name=role_names[0], colour=discord.Color.from_rgb(22, 97, 43)) await guild.create_role(name=role_names[8], colour=discord.Color.from_rgb(0, 255, 0))
print(f"Exiting, finished.") await guild.create_role(name=role_names[7], colour=discord.Color.from_rgb(255, 255, 0))
except Exception as e: await guild.create_role(name=role_names[6], colour=discord.Color.from_rgb(255, 0, 255))
await ctx.send(e) await guild.create_role(name=role_names[5], colour=discord.Color.from_rgb(200, 209, 33))
await guild.create_role(name=role_names[4], colour=discord.Color.from_rgb(178, 186, 28))
await guild.create_role(name=role_names[3], colour=discord.Color.from_rgb(174, 219, 48))
client.run("ODAzNjY2NzA1NzgyOTMxNDU3.YBBG8g.zJGxEaFYnfOqEWtRAbC8m08hTT4") await guild.create_role(name=role_names[2], colour=discord.Color.from_rgb(207, 118, 227))
await guild.create_role(name=role_names[1], colour=discord.Color.from_rgb(30, 179, 72))
await guild.create_role(name=role_names[0], colour=discord.Color.from_rgb(22, 97, 43))
print(f"Exiting, finished.")
except Exception as e:
await ctx.send(e)
client.run("ODAzNjY2NzA1NzgyOTMxNDU3.YBBG8g.zJGxEaFYnfOqEWtRAbC8m08hTT4")