Anthony
Anthony

Reputation: 71

I am following a tutorial on how to make a wordle in nextcord and I the lines are going blank after guesses

So, I am following this tutorial on how to make a wordle bot for nextcord, but when I run the code and do /wordle and reply to the message with a valid word, it just says that it is invalid. My Code:

Bot.py

#imports
import os
from dotenv import load_dotenv
import nextcord
from nextcord.ext import commands

from utils import generate_puzzle_embed, is_valid_word, update_embed, random_puzzle_id


load_dotenv()

bot=commands.Bot(command_prefix=[])

GUILD_IDS=[881678018580467745]

@bot.slash_command(name="wordle", description="Play a game of wordle!", guild_ids=GUILD_IDS)
async def wordle(interaction:nextcord.Interaction):
  #generate a puzzle
  puzzle_id=random_puzzle_id()
  embed=generate_puzzle_embed(puzzle_id)
  await interaction.send(embed=embed)
  #display the puzzle
  #send the puzzle as a response to the interaction

@bot.event
async def on_message(message:nextcord.Message):
  ref = message.reference
  if not ref or not isinstance(ref.resolved, nextcord.Message):
    return
  parent=ref.resolved

  if parent.author.id!=bot.user.id:
    return
  if not parent.embeds:
    return
  embed=parent.embeds[0]
  if not is_valid_word(message.content):
    await message.reply("That is an invalid word", delete_after=5)
    await message.delete(delay=5)
    return
  
  #update embed
  update_embed(embed,message.content)
  await parent.edit(embed=embed)
  try:
    await message.delete()
  except Exception:
    pass
bot.run(os.getenv("TOKEN"))

Utils.py

import nextcord
import random


popularWords=open('mainwords.txt').read().splitlines()
allWords=set(word.strip() for word in open('wierdwords.txt'))

EMOJI_CODES={
  "green":{
    "a":"<:green_a:1040307122350604378>",
    "b":"<:green_b:1040307139224285225>",
    "c":"<:green_c:1040307149441601629>",
    "d":"<:green_d:1040307159960916008>",
    "e":"<:green_e:1040307171075829780>",
    "f":"<:green_f:1040307183440629760>",
    "g":"<:green_g:1040307194433896519> ",
    "h":"<:green_h:1040307204961615974>",
    "i":"<:green_i:1040307213748682762>",
    "j":"<:green_j:1040307222636413008>",
    "k":"<:green_k:1040307245767999528>",
    "l":"<:green_l:1040307258401239081>",
    "m":"<:green_m:1040307269918806196>",
    "n":"<:green_n:1040307282229067836>",
    "o":"<:green_o:1040307294765863023>",
    "p":"<:green_p:1040307309865340970>",
    "q":"<:green_q:1040307331935780986>",
    "r":"<:green_r:1040307345701490729>",
    "s":"<:green_s:1040307361547558992>",
    "t":"<:green_t:1040307371785863230>",
    "u":"<:green_u:1040307384649789521>",
    "v":"<:green_v:1040307400864976927>",
    "w":"<:green_w:1040307421798731787>",
    "x":"<:green_x:1040307433404371005>",
    "y":"<:green_y:1040307443684606102>",
    "z":"<:green_z:1040307453834842292>",
  },
  "yellow":{
    "a":"<:yellow_a:1040342088073621574>",
    "b":"<:yellow_b:1040342099834441848>",
    "c":"<:yellow_c:1040342109850435634>",
    "d":"<:yellow_d:1040342120550117446>",
    "e":"<:yellow_e:1040342129152635010>",
    "f":"<:yellow_f:1040342139256721570>",
    "g":"<:yellow_g:1040342149612441783>",
    "h":"<:yellow_h:1040342163843723435>",
    "i":"<:yellow_i:1040342172936974346>",
    "j":"<:yellow_j:1040342183619870922>",
    "k":"<:yellow_k:1040342207795826828>",
    "l":"<:yellow_l:1040342219804135464>",
    "m":"<:yellow_m:1040342231158112276>",
    "n":"<:yellow_n:1040342243501940807>",
    "o":"<:yellow_o:1040342252981080065>",
    "p":"<:yellow_p:1040342268747456632>",
    "q":"<:yellow_q:1040342281120649226>",
    "r":"<:yellow_r:1040342291115671622>",
    "s":"<:yellow_s:1040342304952680469>",
    "t":"<:yellow_t:1040342323348918292>",
    "u":"<:yellow_u:1040342337773121707>",
    "v":"<:yellow_v:1040342351920513054>",
    "w":"<:yellow_w:1040342369414950912>",
    "x":"<:yellow_x:1040342382438252555>",
    "y":"<:yellow_y:1040342393666420878>",
    "z":"<:yellow_z:1040342405221715978>",
  },
  "gray":{
    "a":"<:gray_a:1040310326048071764>",
    "b":"<:gray_b:1040310336001163264>",
    "c":"<:gray_d:1040310359258574928>",
    "d":"<:gray_e:1040310370063093760>",
    "e":"<:gray_f:1040310382662787082>",
    "f":"<:gray_f:1040310382662787082>",
    "g":"<:gray_g:1040310391764426902>",
    "h":"<:gray_h:1040310407253995630>",
    "i":"<:gray_i:1040310417953665125>",
    "j":"<:gray_j:1040310428917563434>",
    "k":"<:gray_k:1040310441932496987>",
    "l":"<:gray_l:1040310454628663306>",
    "m":"<:gray_m:1040310465361875064>",
    "n":"<:gray_n:1040310480524288020>",
    "o":"<:gray_o:1040310492201226310>",
    "p":"<:gray_p:1040310503320326214>",
    "q":"<:gray_q:1040310512925286421>",
    "r":"<:gray_r:1040310528922357832>",
    "s":"<:gray_s:1040310541224247306>",
    "t":"<:gray_t:1040310553731665980>",
    "u":"<:gray_u:1040310573201621032>",
    "v":"<:gray_v:1040310585797136396>",
    "w":"<:gray_w:1040310601248931881>",
    "x":"<:gray_x:1040310616369406034>",
    "y":"<:gray_y:1040310628709044274>",
    "z":"<:gray_z:1040310645683404890>",
  },
}

def generate_blanks():
  """Return a string of 5 emoji chars"""
  return "\N{WHITE MEDIUM SQUARE}"*5

def generate_puzzle_embed(puzzle_id:int) -> nextcord.Embed:
  embed=nextcord.Embed(title="Wordle!")
  embed.description='\n'.join([generate_blanks()]*6)
  embed.set_footer(text=f"ID: {puzzle_id} | To play, use the command /wordle!\n"
  "To guess, reply to this message with your guess"
  )
  return embed

def generate_colered_word(guess: str, answer:str)->str:
  colored_word=[EMOJI_CODES["gray"][letter] for letter in guess]
  answer_letters=list(answer)
  guess_letters=list(guess)
  #change colors to green
  for i in range(len(guess_letters)):
    if guess_letters[i] == answer_letters[i]:
        colored_word[i] = EMOJI_CODES["green"][guess_letters[i]]
        answer_letters[i] = None
        guess_letters[i] = None
# change colors to yellow if same letter and not the same place
  for i in range(len(guess_letters)):
    if guess_letters[i] is not None and guess_letters[i] in answer_letters:
        colored_word[i] = EMOJI_CODES["yellow"][guess_letters[i]]
        answer_letters[answer_letters.index(guess_letters[i])] = None
  return "".join(colored_word)



def random_puzzle_id() -> int:
  return random.randint(0, len(popularWords)-1)

def is_valid_word(word: str) -> bool:
  """Check if this is a valid word"""
  return word.lower() in allWords

def update_embed(embed: nextcord.Embed, guess: str) -> nextcord.Embed:
  puzzle_id=embed.footer.text.split()[1]
  answer=popularWords[int(puzzle_id)]
  colored_word=generate_colered_word(guess, answer)
  empty_slot=generate_blanks()
  embed.description=embed.description.replace(empty_slot,colored_word,1)
  return embed

When I run the bot and do /wordle and reply to the message with a valid word like drone it responds to me with "This is an invalid word.". If I remove the not in the if is_valid_word and I try that again, the line deletes instead of being replaced by the emojis.

Upvotes: 0

Views: 72

Answers (0)

Related Questions