def get_record(self, guild: nextcord.Guild) -> int: server_path = get_server_path(guild) with open(server_path + NUMBERS_JSON, "r") as f: json_object = json.load(f) letters = json_object["record"] return letters
async def guess(self, ctx: commands.Context, word: str): """Intentar adivinar la palabra del wordle""" word = word.lower() # Check requirements are met aux = self.correct_input(ctx, word) if aux != None: msg = await ctx.send(aux) await msg.delete(delay=5) return # Add user to json self.add_user(ctx.guild, ctx.author.id) # Send embed await self.create_and_send_embed(ctx, word) # Check if the word was guessed solution = self.get_solution_word(ctx.guild) if word == solution: increase_points(ctx.guild, ctx.author.id, WORD_GUESSED_POINTS) await ctx.send("Palabra correcta!!!!") os.remove(get_server_path(ctx.guild) + WORDLE_JSON) # Get definition definicion = str(dle.search_by_word(word)) await ctx.send('"{}": {}'.format(word, definicion)) self.get_random_word(ctx.guild) await self.bot.channel_send( ctx.guild, "wordle", "Nueva palabra generada, intenta adivinarla con `fur guess`", )
def add_partial_letter(self, ctx: commands.Context, letter: str, index: int): """Adds a letter to the partial letters list Args: guild (nextcord.Guild): guild to add the letter letter (str): letter to add """ server_path = get_server_path(ctx.guild) with open(server_path + WORDLE_JSON, "r+") as f: json_object = json.load(f) letters = json_object["partial_letters"] if letters[index] != "-": return letters[index] = letter f.seek(0) json.dump(json_object, f) f.truncate() increase_points(ctx.guild, ctx.author.id, GREEN_POINTS)
def generate_numbers_file(self, guilds: list) -> None: for guild in guilds: server_path = get_server_path(guild) if not os.path.isfile(server_path + NUMBERS_JSON): # Create file with open(server_path + NUMBERS_JSON, "w+") as f: json_object = json.dumps(JSON_CONTENT) f.write(json_object)
async def list_stickers(self, ctx: commands.Context): """Lista de los stickers añadidos""" stickers_path = get_server_path(ctx.guild) + stickers_subpath output = os.listdir(stickers_path) output.sort() output[:] = [s.replace(".png", "") for s in output] output = ", ".join(output) await ctx.send(output)
def word_guessed(self, guild: nextcord.Guild) -> bool: """Checks if the word of the day was guessed Args: guild (nextcord.Guild): guild to check Returns: bool: True if the word was guessed, False otherwise """ server_path = get_server_path(guild) if os.path.isfile(server_path + WORDLE_JSON): return False return True
def get_solution_word(self, guild: nextcord.Guild) -> str: """Gets the solution word for the guild Args: guild (nextcord.Guild): guild to get the word Returns: str: solution word """ server_path = get_server_path(guild) with open(server_path + WORDLE_JSON, "r") as file: data = json.load(file) return data["word"]
def remove_users(self, guild: nextcord.Guild): """Removes users from json file Args: guild (nextcord.Guild): guild to remove the users """ server_path = get_server_path(guild) with open(server_path + WORDLE_JSON, "r+") as f: json_object = json.load(f) json_object["users"] = [] f.seek(0) json.dump(json_object, f) f.truncate()
def get_correct_letters(self, guild: nextcord.Guild) -> list: """Gets the correct letters Args: guild (nextcord.Guild): guild to get the letters Returns: list: list of correct letters """ server_path = get_server_path(guild) with open(server_path + WORDLE_JSON, "r") as f: json_object = json.load(f) letters = json_object["correct_letters"] return letters
def update_json(self, guild: nextcord.Guild, number: int, last_user: int) -> None: server_path = get_server_path(guild) with open(server_path + NUMBERS_JSON, "r+") as f: json_object = json.load(f) json_object["number"] = number json_object["last_user"] = last_user if json_object["record"] < number: json_object["record"] = number f.seek(0) json.dump(json_object, f) f.truncate()
def get_random_word(self, guild: nextcord.Guild): """Selects a random word from the words dictionary ARgs: guild (nextcord.Guild): guild to get the word """ server_path = get_server_path(guild) if not os.path.isfile(server_path + WORDLE_JSON): lines = open(resources_path + "words.txt").readlines() word = random.choice(lines).strip() # Create file with open(server_path + WORDLE_JSON, "w+") as f: WORDLE_DICT["word"] = word json_object = json.dumps(WORDLE_DICT) f.write(json_object)
async def meme(self, ctx: commands.Context, name: str = None, tipo: str = None): """Meme random de los nuestros""" # If all memes have been sent, delete history memes_path = get_server_path(ctx.guild) + "/memes/" if name is None: output = random.choice(os.listdir(memes_path)) await ctx.send(file=nextcord.File(memes_path + output)) else: uwu = [] for filenames in os.listdir(memes_path): if name.lower() in filenames.lower(): uwu.append(filenames) # check if exists a meme with the filters if len(uwu) == 0: await ctx.send("No hay memes con " + name) return if (tipo == "video" and not any(".mp4" in s for s in uwu)) or ( tipo == "imagen" and not any(".jpg" in s for s in uwu) ): await ctx.send("No hay memes de " + tipo + " que sean de " + name) return if tipo is not None: if tipo == "video": output = "0" while ".mp4" not in output: output = random.choice(uwu) if tipo == "imagen": output = "0" while ".jpg" not in output: output = random.choice(uwu) else: output = random.choice(uwu) await ctx.send(file=nextcord.File(memes_path + output)) logging.info("Meme " + output + " sent")
async def use_sticker(self, ctx: commands.Context, sticker): """Usar un sticker Uso: fur s <nombre_sticker> """ stickers_path = get_server_path(ctx.guild) + stickers_subpath if Path(stickers_path + sticker + ".png").is_file(): stickerName = stickers_path stickerName += sticker stickerName += ".png" await ctx.send(file=nextcord.File(stickerName)) log.info("Sticker " + sticker + " sent") else: await ctx.send("No existe el sticker " + sticker) if sticker in self.bot.all_commands: await ctx.send( "Igual quieres usar el comando `{}`".format(sticker))
def check_sticker(guild: nextcord.Guild, stickerName: str, stickerExtension: str): """checks if a sticker already exists and if file extension is correct Args: guild (nextcord.Guild): guild stickerName ([String]): [Name of sticker to add] stickerExtension ([String]): [Extension of sticker to add] Returns: [0]: [name used by other sticker] [1]: [if file extension is correct, must be jpg or png] """ stickers_path = get_server_path(guild) + stickers_subpath string = os.listdir(stickers_path) string[:] = [s.replace(".png", "") for s in string] string[:] = [s.replace("'", "") for s in string] if stickerName in string: return 0 if stickerExtension in ["jpg", "png"]: return 1
def add_user(self, guild: nextcord.Guild, user: int): """Adds a user to json file Args: guild (nextcord.Guild): guild to add the letter user (int): id of user to add """ # Check if the letter is already in the list server_path = get_server_path(guild) with open(server_path + WORDLE_JSON, "r+") as f: json_object = json.load(f) users = json_object["users"] if user not in users: users.append(user) json_object["users"] = users f.seek(0) json.dump(json_object, f) f.truncate()
def add_to_list(self, ctx: commands.Context, key: str, element: str): """Adds an element to the json file Args: ctx (commands.Context): context of the command key (str): key of the element element (str): element to add """ server_path = get_server_path(ctx.guild) with open(server_path + WORDLE_JSON, "r+") as f: json_object = json.load(f) if element not in json_object[key]: json_object[key].append(element) json_object[key].sort() if key == "correct_letters": increase_points(ctx.guild, ctx.author.id, YELLOW_POINTS) f.seek(0) json.dump(json_object, f) f.truncate()
def add_discarded_letter(self, guild: nextcord.Guild, letter: str): """Adds a letter to the discarded letters list Args: guild (nextcord.Guild): guild to add the letter letter (str): letter to add """ # Check if the letter is already in the list server_path = get_server_path(guild) with open(server_path + WORDLE_JSON, "r+") as f: json_object = json.load(f) letters = json_object["discarded_letters"] if letter not in letters: letters.append(letter) letters.sort() json_object["discarded_letters"] = letters f.seek(0) json.dump(json_object, f) f.truncate()
async def add_sticker(self, ctx: commands.Context, sticker_name: str): """Añade un sticker Uso: - seleccionar una imagen y en el cuadro de "añadir comentario" poner: fur add <nombre_sticker> """ stickers_path = get_server_path(ctx.guild) + stickers_subpath # If not image provided if not ctx.message.attachments: await ctx.send("Error: No se ha añadido una imagen") return # Checks if a picture is correct sticker_extension = ctx.message.attachments[0].url.split(".")[-1] if check_sticker(ctx.guild, sticker_name, sticker_extension) == 0: await ctx.send( "Error: Ya existe un sticker con el nombre {}".format( sticker_name)) return if sticker_extension == "jpg": sticker_fileName = sticker_name + ".jpg" else: sticker_fileName = sticker_name + ".png" stickerUrl = ctx.message.attachments[0].url r = requests.get(stickerUrl, allow_redirects=True) open(stickers_path + sticker_fileName, "wb").write(r.content) convert_pic(stickers_path + sticker_fileName, sticker_name) if sticker_extension == "jpg": os.remove(stickers_path + sticker_fileName) await ctx.send("Sticker " + sticker_name + " añadido")
async def count_memes(self, ctx: commands.Context): """Número de memes añadidos al bot""" memes_path = get_server_path(ctx.guild) + "/memes/" await ctx.send(len(os.listdir(memes_path)))
async def addmeme(self, ctx: commands.Context, meme_name: str): """Añade un meme al bot Uso: fur meme <nombres personas en el meme> """ memes_path = get_server_path(ctx.guild) + "/memes/" meme_extension = "." + ctx.message.attachments[0].filename meme_extension = meme_extension[-4:] count = 1 # Remove " meme_name = meme_name.replace('"', "") # Remove accents meme_name = unicodedata.normalize("NFD", meme_name) meme_name = meme_name.encode("ascii", "ignore") meme_name = meme_name.decode("utf-8") # Capitalize all names meme_name = meme_name.lower() meme_name = meme_name.title() # split into a list names = meme_name.split() # Order names in case they are not in order names = sorted(names) meme_name = " ".join(names) # Count the number to add to the name if meme_extension == ".png" or meme_extension == ".jpg": for x in os.listdir(memes_path): aux2 = x.split(" (", 1) if "jpg" in aux2[1] and aux2[0] == meme_name: count = count + 1 if meme_extension == ".mp4": for x in os.listdir(memes_path): aux2 = x.split(" (", 1) if "mp4" in x and aux2[0] == meme_name: count = count + 1 count = str(count) meme_name = meme_name + " (" + count + ")" meme_name = meme_name.replace(" ", "_") meme_url = ctx.message.attachments[0].url r = requests.get(meme_url, allow_redirects=True) open(memes_path + meme_name + meme_extension, "wb").write(r.content) if meme_extension == ".png": im = Image.open(memes_path + meme_name + meme_extension) rgb_im = im.convert("RGB") meme_extension = ".jpg" rgb_im.save(memes_path + meme_name + meme_extension) os.remove(memes_path + meme_name + ".png") old = memes_path + meme_name + meme_extension newname = meme_name.replace("_", " ") new = memes_path + newname + meme_extension os.rename(old, new) logging.info("Meme " + newname + " added by" + str(ctx.author)) await ctx.send("Meme " + newname + " añadido")