LoginSignup
4

More than 1 year has passed since last update.

posted at

updated at

どうせ、家にいるんだからDiscordで(サイト)スクリーンショット機能ボット作ろうぜ!

こんにちは!T-takuと申すものです。コロナでなんか暇だし、記事を書くことにしました!ちょっとだけお付き合いください...:boy_tone1:

ちょっとした余談

https://discordapp.com/oauth2/authorize?client_id=476012428170362880&permissions=2147347828&scope=bot
このボットは、僕T-takuが運営しているものです、スクリーンショットコマンドがどんなものか確かめたい!って人は、ぜひ入れて試してみてください!

今回完成させるもの

Discordでサイトのスクリーンショットを出してくれるボットを作ろう!
ご注意:Discord.py入門編ではありません。アカウント作成については、以下の素晴らしい記事をご覧ください:こちら

環境

  • Windows 10(実際動かす環境では、Linux-Ubuntu)
  • Python 3.7
  • discord.py 1.3.3
  • Chromedriver 80.0
  • Google Chrome 80.0.3987.149

早速作っていこう!

今回はWindows10を前提に作っていきます。また、Google Chromeも入っている前提で、進めていきます。

Chromedriverの導入

ここから、あなたの環境にあった、ChromeDriverを導入してください。正しいバージョンを入れないとエラーを吐くので、ご注意ください!

pipの設定

pip install discord.py pillow chromedriver chromedriver-binary selenium

実装

さっそく実装していきましょう!まずはスクリーンショット部分です。


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="Chromeがあるとこ"
driver=webdriver.Chrome(chrome_options=options,executable_path=r"Chrome driverの場所")
driver.get(web)
driver.set_window_size(1280, 720)
driver.save_screenshot(FILENAME)
driver.quit()

Discordに送信する

最低限動くようにはなっています。(多分...)

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='起動中!',type=d.ActivityType.watching))

@bot.event
async def on_ready():
    #ログイン
    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)#ctxをとる
    if message.author.bot:
        return #Botには反応しない
    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="Chromeがある場所"
        driver=webdriver.Chrome(chrome_options = options,executable_path=r"Chromeドライバーがある場所")
        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("このWebページにはアクセスできません。")
        else:
            driver.set_window_size(1280, 720)
            driver.save_screenshot('screenshot.png')
            file = discord.File("screenshot.png", filename="image.png")
            embed = d.Embed(title="スクリーンショット", description=f"{web}")
            embed.set_image(url="attachment://image.png")
            await ctx.send(file=file,embed=embed)
            driver.quit()
    except:
        await ctx.send("エラーが発生していたため、アクセスができませんでした。")
bot.run("TOKEN HERE")

以上

いかがでしたか?ちょっとだけ難易度の高い記事を作ってみました。皆さんも自分のボットに追加してみてくださいね!

記事冒頭でも言いましたが、僕のボットを追加してもらえると、記事の作成、ボット開発の励みとなりますので、よろしくお願いします!:
https://discordapp.com/oauth2/authorize?client_id=476012428170362880&permissions=2147347828&scope=bot

参考になれば、LGTMをくださるともっと喜びます!

それでは!(ミスがあったら教えていただけると嬉しいです!)

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
4