タイトルがすごいですが内容は普通にDiscordBotを作った話です。
#今回の問題の文
if not message.author.id == client.user.id:
ms = message.content # imageコマンドの判定
ms = ms[:6] # すべてのメッセージの最初の六文字だけ切り取って判定している
if ms == "!image":
overlap = 0
ms = (message.content[7:]) # msをメッセージだけにしている(!imageを削る)
filepath1 = "./downloads" # あとで結合するため
files1 = os.listdir(filepath1) # すべてのディレクトリ名をfilesに入れている
leng = len(files1) # filesの配列の長さを判定
i = 0
for i in range(leng):
if ms == files1[i]:
overlap = 1
break
i += 1
if overlap == 1:
print("スクレイピングせずにローカル内にある画像を使用しました") # 確認用
filepath = './downloads/' + ms + "/" # ファイル入ってる場所
files = os.listdir(filepath) # そのファイルの中身の名前を変数に入れてる
files = files[0] # 配列で入るのでその中の一番目
path1 = filepath + files # 画像の最終的なpathを生成
await message.channel.send(file=discord.File(path1)) # 画像を送信
else:
print("ローカル内に画像を確認できなかったためスクレイピングしました") # 確認用
response = google_images_download.googleimagesdownload() # グーグル画像スクレイピングライブラリ
arguments = {"keywords": ms, "limit":1,"print_urls":True}#検索ワードと枚数指定
paths = response.download(arguments) # スクレイピング、
print(paths) # ログを出力
ms = (message.content[7:])
filepath = './downloads/' + ms + "/" # ファイルのどのファイルに入っているのかpathを生成
files = os.listdir(filepath) # そのファイルの中身の名前を変数に入れてる
files = files[0] # 配列で入るのでその中の一番目
path1 = filepath + files # 画像の最終的なpathを生成
print(path1)
await message.channel.send(file=discord.File(path1)) # 画像を送信
こちらユーザーが送った文字をpathの中に入れてるのでどこに保存するかをユーザーが指定できてしまうのです!!!
それをクラスメイトが発見して僕のパソコンの中身がめちゃくちゃになりました。
いろんなところに無駄なファイルを作られました、一番ひどいのは「...」←この名前のファイルを作られたことです。
この意味わかんないファイルはWindowsのバグらしくて「...」これを開くと一つ前のディレクトリが開きます。
なのでDesktopにこのファイルがある場合そのファイルを開くと中にはDesktopがあるのです。
しかもそのファイル普通には消せないのです、Windowsのコマンドプロントとかでrm ...
とかでも消せません...
なのでCygwinで消しました。消すの大変でした。
このようなサービスをメインPCで実行するのは絶対にやめた方がいいです
やばい人たちに**されます
###ファイル構成
DiscordBot/
├ discordbot.py
└ img/
├ hare.png
├ harekumorri.png
├ jikoku.png
├ kumori.png
└ downloads
###実行環境
- Windows10Pro
- Python 3.8
import datetime
import io
import os
import time
import aiohttp
import discord
import requests
import schedule
from discord.ext import commands
from google_images_download import google_images_download
from PIL import Image, ImageDraw, ImageFont, ImageTk
# 自分のBotのアクセストークンに置き換えてください
TOKEN = "自分のトークン"
# 接続に必要なオブジェクトを生成
client = discord.Client()
# 起動時に動作する処理
@client.event
async def on_ready():
# 起動したらターミナルにログイン通知が表示される
print('ログインしました')
@client.event
async def on_message(message):
# メッセージ送信者がBotだった場合は無視する
if message.author.bot:
return
# 「/neko」と発言したら「にゃーん」が返る処理
if message.content == '/neko':
await message.channel.send('にゃーん')
if message.content == '/konn':
t_now = datetime.datetime.now().time()
t_now = str(t_now)
t_now = t_now[:2]
print(t_now)
t_now = int(t_now)
print(type(t_now))
print(t_now)
if 5 <= t_now < 12:
await message.channel.send('おはようございます')
elif 12 <= t_now < 18:
await message.channel.send('こんにちは')
elif 18 <= t_now < 24:
await message.channel.send('こんばんは')
elif 0 <= t_now < 5:
await message.channel.send('もう夜遅くです!寝ましょう!')
if message.content == '明日の天気':
url = 'http://weather.livedoor.com/forecast/webservice/json/v1?city=130010'
api_data = requests.get(url).json()
weather = api_data['forecasts'][1]
result = weather["telop"]
await message.channel.send(api_data['title'] + '\n' '明日の天気は' + result)
if result == "晴れ":
await message.channel.send(file=discord.File('./img/hare.png'))
elif result == "曇り":
await message.channel.send(file=discord.File("./img/kumori.png"))
else:
await message.channel.send(file=discord.File("./img/harekumori.jpg"))
if message.content == '今日の天気':
url = 'http://weather.livedoor.com/forecast/webservice/json/v1?city=130010'
api_data = requests.get(url).json()
weather = api_data['forecasts'][0]
result = weather["telop"]
await message.channel.send(api_data['title'] + '\n' '今日の天気は' + result)
if result == "晴れ":
await message.channel.send(file=discord.File('.\\img\\hare.png'))
elif result == "曇り":
await message.channel.send(file=discord.File(".\\img\\kumori.jpg"))
else:
await message.channel.send(file=discord.File(".\\img\\harekumori.png"))
if message.content == '極秘コマンド>>うんち<<':
await message.channel.send('https://i.gzn.jp/img/2017/10/19/golden-unko-jewelry/00_m.jpg')
# if message.content == 'ンアッーー':
# ar1t = (
# '```(≧Д≦)' + '\n' +
# '.......................,,,z=~’゙’+”ッ彡ッ,、' + '\n' +
# '....................,ィ´............“‘:’;:;ッ;,' + '\n' +
# '................’ ゙´`゙ミ゙ッ,..............“‘,`,' + '\n' +
# '...........,/.......`、゙ミ.................゙:;:,' + '\n' +
# '........./........... =ヾ、゙シシ=;,z,..........゙;シ::ミ' + '\n' +
# '......./........,r,´.../.´`ヽ.゛゙`.........,゙彡:ミ' + '\n' +
# '..... /......, ‘-、.`ヽ....................ミ;::彡;:' + '\n' +
# '... ,’.....,シ´`` ヽ`i`!.............. ,,彡;::シ:彡' + '\n' +
# '..;.....、(´  ̄`ヽ / ‘..............シ:シ;:ミ::シ”' + '\n' +
# '´:::::.ヾ..... ̄´..............’ `,シミ゙' + '\n' +
# ':::::::::::::.`:ヽ、...........…:;’_,ソ’゙’' + '\n' +
# 'ンアッーーーーーーーーーーーーーーーーーーーーーーーーーーーー```')
# await message.channel.send(ar1t)
if client.user in message.mentions:
reply = f'{message.author.mention} 呼んだ?'
await message.channel.send(reply)
if message.content == '時刻':
# 画像の読み込み
img = Image.open("./img/jikoku.png")
# drawインスタンスを生成
draw = ImageDraw.Draw(img)
# フォントの設定(フォントファイルのパスと文字の大きさ)
font = ImageFont.truetype("./fonts/BIZ-UDGothicB.ttc", 200)
# 文字を書く
dt = datetime.datetime.now()
dt = str(dt)
dt = (dt[11:-10])
print(dt)
draw.text((310, 250), dt, fill=(0, 0, 0), font=font)
img.save("./img/jikoku1.png")
print("ok")
await message.channel.send(file=discord.File(".\\img\\jikoku1.png"))
if message.content == '増税':
await message.channel.send('https://pbs.twimg.com/media/EFUS5YlU0AIAwok?format=jpg&name=small')
if message.content == 'kusa':
await message.channel.send(file=discord.File('.\\img\\kusa.jpg'))
if message.content == 'かわいい子':
await message.channel.send(file=discord.File('.\\img\\1.jpg'))
if message.content == "いいね":
await message.channel.send(file=discord.File('.\\img\\good.png'))
if not message.author.id == client.user.id:
ms = message.content # imageコマンドの判定
ms = ms[:6] # すべてのメッセージの最初の六文字だけ切り取って判定している
if ms == "!image":
overlap = 0
ms = (message.content[7:]) # msをメッセージだけにしている(!imageを削る)
filepath1 = "./downloads" # あとで結合するため
files1 = os.listdir(filepath1) # すべてのディレクトリ名をfilesに入れている
leng = len(files1) # filesの配列の長さを判定
i = 0
for i in range(leng):
if ms == files1[i]:
overlap = 1
break
i += 1
if overlap == 1:
print("スクレイピングせずにローカル内にある画像を使用しました") # 確認用
filepath = './downloads/' + ms + "/" # ファイル入ってる場所
files = os.listdir(filepath) # そのファイルの中身の名前を変数に入れてる
files = files[0] # 配列で入るのでその中の一番目
path1 = filepath + files # 画像の最終的なpathを生成
await message.channel.send(file=discord.File(path1)) # 画像を送信
else:
print("ローカル内に画像を確認できなかったためスクレイピングしました") # 確認用
response = google_images_download.googleimagesdownload() # グーグル画像スクレイピングライブラリ
arguments = {"keywords": ms, "limit":1,"print_urls":True}#検索ワードと枚数指定
paths = response.download(arguments) # スクレイピング、
print(paths) # ログを出力
ms = (message.content[7:])
filepath = './downloads/' + ms + "/" # ファイルのどのファイルに入っているのかpathを生成
files = os.listdir(filepath) # そのファイルの中身の名前を変数に入れてる
files = files[0] # 配列で入るのでその中の一番目
path1 = filepath + files # 画像の最終的なpathを生成
print(path1)
await message.channel.send(file=discord.File(path1)) # 画像を送信
if message.content == "履歴":
files1 = os.listdir("./downloads")
await message.chnanel.send(files1)
print("履歴コマンド")
if message.content == "!helpC":
await message.channel.send("特になし")
# Botの起動とDiscordサーバーへの接続
client.run(TOKEN)