-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMemeBot.py
90 lines (79 loc) · 3.18 KB
/
MemeBot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# imports
import discord ,discord.message,env,random,time
from selenium import webdriver
from selenium.webdriver.common.by import By
# discord init
intents = discord.Intents.default()
intents.message_content = True
Client = discord.Client(intents=intents)
# selenium init
options = webdriver.ChromeOptions()
options.add_argument("--ignore-certificate-errors")
options.add_argument('--headless')
driver = webdriver.Chrome(options=options)
# when bot is ready (Bot start)
@Client.event
async def on_ready():
print("Bot is Ready and Online")
# when user types the command in discord
@Client.event
async def on_message(message):
if message.author == Client.user:
return
msg = str(message.content).split()
if msg[0] == ">meme":
randomArticle = random.randint(1,80)
await message.channel.send(env.DEFAULT)
x = ""
try:
meme = getImgUrl(driver=driver , tag=msg[1] , article=str(randomArticle))
await message.channel.send(meme)
except:
await message.channel.send(env.ERROR)
# gets the image url from the website
def getImgUrl(driver,tag,article=1,):
def scrollDown(driver):
driver.execute_script("window.scrollTo(0,document.body.scrollHeight);")
time.sleep(2)
driver.execute_script("window.scrollTo(0,document.body.scrollHeight);")
time.sleep(2)
driver.execute_script("window.scrollTo(0,document.body.scrollHeight);")
# check if tag exists or not
try:
driver.get(env.SOURCE + tag)
except:
print("Something went wrong")
scrollDown(driver=driver)
memeUrl = ""
# checks if generated article number exists or not
try:
memeUrl = driver.find_element(by=By.XPATH,value=f'/html/body/div[5]/div/div[2]/section/div[2]/article[{article}]/div[1]/a/picture/img')
except:
# checks if next (+1) article exists or not
scrollDown(driver=driver)
try:
aboveArticle = int(article) + 1
x = str(aboveArticle)
memeUrl = driver.find_element(by=By.XPATH,value=f'/html/body/div[5]/div/div[2]/section/div[2]/article[{x}]/div[1]/a/picture/img')
except:
# from the original article number , this randomly boils down to 1st article
belowArticle = int(article) - 1
while belowArticle < int(article):
print(belowArticle)
try:
x = str(belowArticle)
memeUrl = driver.find_element(by=By.XPATH,value=f'/html/body/div[5]/div/div[2]/section/div[2]/article[{x}]/div[1]/a/picture/img')
except:
if belowArticle == 1:
print(env.ERROR)
break
else:
belowArticle = random.randint(1,belowArticle)
else:
x = str(belowArticle)
memeUrl = driver.find_element(by=By.XPATH,value=f'/html/body/div[5]/div/div[2]/section/div[2]/article[{x}]/div[1]/a/picture/img')
break
#print(memeUrl.get_attribute('alt')) -- debug code
return memeUrl.get_attribute('src')
# runs disord bot
Client.run(env.TOKEN)