mazebot/main.py
2022-03-20 07:42:05 +00:00

413 lines
13 KiB
Python
Executable File

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