Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # MainchanBriefBot - A Discord bot that briefs you about what is going on with Mainchan.
- # XXX WARNING: This is a prototype; do not use. XXX
- # version 0.1.0
- import os
- import discord
- import requests
- from dotenv import load_dotenv
- from bs4 import BeautifulSoup
- def scrapeFirstPost(url):
- # This function returns data from the first post in the given url.
- page = requests.get(url)
- soup = BeautifulSoup(page.content, "html.parser")
- submissions = soup.find_all("div", class_="submission-content-container")
- title_elem = submissions[0].find("span", class_="submission-title")
- title_text = title_elem.text.strip('\n')
- title_link = title_elem.find('a')['href']
- comments_elem = submissions[0].find("li", class_="submission-comments")
- comments_link = comments_elem.find('a')['href']
- return {"title": title_text, "link": title_link, "comments": comments_link}
- load_dotenv()
- TOKEN = os.getenv('DISCORD_TOKEN')
- intents = discord.Intents.all()
- client = discord.Client(intents=intents)
- # execute only once when connected
- @client.event
- async def on_ready():
- print(f"{client.user} has connected to Discord")
- # execute whenever there is a new message
- @client.event
- async def on_message(message):
- # dont do anything if the message was sent by the bot itself
- if message.author == client.user:
- return
- if message.content == "whatsup with mainchan" and message.channel.name == "test":
- firsthot = scrapeFirstPost("https://mainchan.com/all?sortBy=hotness")
- firstnew = scrapeFirstPost("https://mainchan.com/all?sortBy=new")
- # TODO: make sure it is safe to send this text over discord.
- await message.channel.send(f"**hottest post**\ntitle: {firsthot['title']}\ncomments: {firsthot['comments']}\n\n**newest post**\ntitle: {firstnew['title']}\ncomments: {firstnew['comments']}")
- client.run(TOKEN)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement