Advertisement
usrxprd

MainchanBriefBot

May 7th, 2024 (edited)
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.82 KB | None | 0 0
  1. # MainchanBriefBot - A Discord bot that briefs you about what is going on with Mainchan.
  2. # XXX WARNING: This is a prototype; do not use. XXX
  3. # version 0.1.0
  4.  
  5. import os
  6. import discord
  7. import requests
  8. from dotenv import load_dotenv
  9. from bs4 import BeautifulSoup
  10.  
  11. def scrapeFirstPost(url):
  12.  
  13.     # This function returns data from the first post in the given url.
  14.  
  15.     page = requests.get(url)
  16.     soup = BeautifulSoup(page.content, "html.parser")
  17.  
  18.     submissions = soup.find_all("div", class_="submission-content-container")
  19.  
  20.     title_elem = submissions[0].find("span", class_="submission-title")
  21.     title_text = title_elem.text.strip('\n')
  22.     title_link = title_elem.find('a')['href']
  23.  
  24.     comments_elem = submissions[0].find("li", class_="submission-comments")
  25.     comments_link = comments_elem.find('a')['href']
  26.  
  27.     return {"title": title_text, "link": title_link, "comments": comments_link}
  28.  
  29. load_dotenv()
  30. TOKEN = os.getenv('DISCORD_TOKEN')
  31.  
  32. intents = discord.Intents.all()
  33.  
  34. client = discord.Client(intents=intents)
  35.  
  36. # execute only once when connected
  37. @client.event
  38. async def on_ready():
  39.     print(f"{client.user} has connected to Discord")
  40.  
  41. # execute whenever there is a new message
  42. @client.event
  43. async def on_message(message):
  44.  
  45.     # dont do anything if the message was sent by the bot itself
  46.     if message.author == client.user:
  47.         return
  48.  
  49.     if message.content == "whatsup with mainchan" and message.channel.name == "test":
  50.  
  51.         firsthot = scrapeFirstPost("https://mainchan.com/all?sortBy=hotness")
  52.         firstnew = scrapeFirstPost("https://mainchan.com/all?sortBy=new")
  53.  
  54.         # TODO: make sure it is safe to send this text over discord.
  55.         await message.channel.send(f"**hottest post**\ntitle: {firsthot['title']}\ncomments: {firsthot['comments']}\n\n**newest post**\ntitle: {firstnew['title']}\ncomments: {firstnew['comments']}")
  56.  
  57. client.run(TOKEN)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement