import discordimport requestsimport asynciofrom bs4 import BeautifulSoupfrom discord.ext import commandsimport re# Bot token and channel IDbot_token =''channel_id =# Set up Discord bot with specified command prefix and intentsintents = discord.Intents.default()intents.typing =Falseintents.presences =Falsebot = commands.Bot(command_prefix='!',intents=intents)@bot.eventasyncdefon_ready():# Print bot name when it successfully connects to Discordprint(f'Bot connected as {bot.user.name}') bot.loop.create_task(check_one_piece_chapter())@bot.command()asyncdeftest(ctx):# Test command that sends a message to the channelawait ctx.send('Test message from the bot!')defget_latest_chapter_info(url):# Function to retrieve the latest chapter info from the specified URL response = requests.get(url)if response.status_code ==200: soup =BeautifulSoup(response.content,'html.parser')# Find the element containing the latest chapter information chapter_element = soup.find('a',class_='block border border-border bg-card mb-3 p-3 rounded')if chapter_element: chapter_link = chapter_element['href']# Extract the chapter number from the chapter URL chapter_number = chapter_link.split('/')[-2].split('-')[-1]# Extract the chapter title from the element text chapter_title = chapter_element.get_text(strip=True)# Extract chapter number and title using regex chapter_title_parts = re.search(r'(\d+)([A-Za-z]+)', chapter_title)if chapter_title_parts: chapter_number = chapter_title_parts.group(1) chapter_title =f"One Piece Chapter {chapter_number}"# Construct the full chapter URL chapter_url =f"https://tcbscans.com{chapter_link}"return chapter_number, chapter_title, chapter_url# Return None if the latest chapter info couldn't be retrievedreturnNone,None,Noneasyncdefcheck_one_piece_chapter():# Function to periodically check for new One Piece chapters url ='https://tcbscans.com/mangas/5/one-piece' latest_chapter_number =NonewhileTrue:# Retrieve the latest chapter info chapter_info =get_latest_chapter_info(url)# Compare the latest chapter number with the previously stored valueif chapter_info and chapter_info[0]!= latest_chapter_number: channel = bot.get_channel(channel_id)if channel: chapter_number, chapter_title, chapter_url = chapter_infoif chapter_url:# Send a message to the channel indicating the new chapter releaseawait channel.send(f"{chapter_title} has been released! Read it here: {chapter_url}")else:await channel.send(f"{chapter_title} has been released!")# Update the latest chapter number latest_chapter_number = chapter_info[0]else:print(f"Channel with ID {channel_id} not found.")# Sleep for 1 hour before checking againawait asyncio.sleep(3600)bot.run(bot_token)