Anyway, I'm at home, so let's make a (site) screenshot function bot on Discord!

Hello! Is what Mosu and T-taku. I have some free time in Corona, so I decided to write an article! Please keep in touch with me ...: boy_tone1:

A little digression

https://discordapp.com/oauth2/authorize?client_id=476012428170362880&permissions=2147347828&scope=bot This bot is run by me T-taku, and if you want to see what the screenshot command looks like, please put it in and give it a try!

What to complete this time

Create a bot that will give you a screenshot of your site on Discord! Please note: ** This is not an introduction to Discord.py. ** For account creation, see the great article below: here

environment

Let's make it right away!

This time we will make it on the premise of Windows 10. In addition, we will proceed on the assumption that Google Chrome is also included.

Introducing Chrome driver

Install the Chrome Driver that suits your environment from here. Please note that if you do not enter the correct version, you will get an error!

pip settings

pip install discord.py pillow chromedriver chromedriver-binary selenium

Implementation

Let's implement it now! First, let's take a screenshot.


from PIL import Image
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
Web="https://yahoo.co.jp"
FILENAME = "screen.png "
options=Options()
options.set_headless(True)
options.add_argument('--disable-dev-shm-usage')
options.add_argument('start-maximized')
options.add_argument('disable-infobarse')
options.add_argument('--disable-extensions')
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
options.binary_location="Where there is Chrome"
driver=webdriver.Chrome(chrome_options=options,executable_path=r"Chrome driver location")
driver.get(web)
driver.set_window_size(1280, 720)
driver.save_screenshot(FILENAME)
driver.quit()

Send to Discord

It is designed to work at a minimum. (Perhaps...)

import discord
from discord.ext import commands
from PIL import Image
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

bot = commands.Bot(command_prefix='!',activity=d.Activity(name='During startup!',type=d.ActivityType.watching))

@bot.event
async def on_ready():
    #Login
    print('Login infomation>>>')
    print(bot.user.name)
    print(bot.user.id)
    print('------')

@bot.event
async def on_message(message):
    ctx = await bot.get_context(message)#Take ctx
    if message.author.bot:
        return #Does not respond to bots
    await bot.invoke(ctx)

@bot.command()
async def ss(ctx,web):
    try:
        FILENAME = "screen.png "
        options=Options()
        options.set_headless(True)
        options.add_argument('--disable-dev-shm-usage')
        options.add_argument('start-maximized')
        options.add_argument('disable-infobarse')
        options.add_argument('--disable-extensions')
        options.add_argument('--disable-gpu')
        options.add_argument('--no-sandbox')
        options.binary_location="Where Chrome is"
        driver=webdriver.Chrome(chrome_options = options,executable_path=r"Where the Chrome driver is")
        if not "http" in str(web):
            try:
                driver.get("http://"+str(web))
            except:
                driver.get("https://"+str(web))
        else:
            driver.get(str(web))
        if 'IP addres' in driver.page_source:
            await ctx.send("You cannot access this web page.")
        else:
            driver.set_window_size(1280, 720)
            driver.save_screenshot('screenshot.png')
            file = discord.File("screenshot.png ", filename="image.png ")
            embed = d.Embed(title="screenshot", description=f"{web}")
            embed.set_image(url="attachment://image.png ")
            await ctx.send(file=file,embed=embed)
            driver.quit()
    except:
        await ctx.send("Access was not possible due to an error.")
bot.run("TOKEN HERE")

that's all

How was it? I made an article with a little difficulty. Please add it to your bot!

As I said at the beginning of the article, if you add my bot, it will encourage the creation of the article and the development of the bot, so thank you!: https://discordapp.com/oauth2/authorize?client_id=476012428170362880&permissions=2147347828&scope=bot

If you find it helpful, I would be more than happy to receive LGTM!

Well then! (I would be grateful if you could tell me if you made a mistake!)

Recommended Posts

Anyway, I'm at home, so let's make a (site) screenshot function bot on Discord!
Let's make a Discord Bot.
Let's make a multilingual site using flask-babel
Let's make a Twitter Bot with Python!
I tried to make a translation BOT that works on Discord using googletrans
If you want to make a discord bot with python, let's use a framework
Make a bot for Skype on EC2 (CentOS)
Make a BOT that shortens the URL of Discord
Make a parrot return LINE Bot on AWS Cloud9
[For play] Let's make Yubaba a LINE Bot (Python)
[Super easy] Let's make a LINE BOT with Python.