I usually make RPA locally with selenium or pyautogui and automate the work that has many boring work </ s> procedures and causes human error, but it will be inconvenient if it does not continue to work online.
But I don't know how to get there.
I tried to touch Django and Flask, but I still don't understand what it is, so I made it with a discordbot for my acquaintance's TRPG and learned while playing.
Practical Discord Bot in Python (discordpy explanation) There is a god, so I used it as a reference.
discord.py
Abbreviation
if message.content == '/yagi':
result = "Mmm"
await message.channel.send(result)
With this, I was basically able to return a reply to the command. Then you can reply to the command / yagi.
And if there is a dice roll -Specify a command ・ Write the process ·return You can complete it with. (Amateur thinking)
discord.py
Abbreviation
s = message.content
dicecheck = re.match('^/[1-9]{1}[D]', s)
if dicecheck:
dice = 1
else:
dice = 0
If it is written as / nDN, it is judged that it is a dice roll.
discord.py
Abbreviation
if dice > 0:
await message.channel.send('Don't miss it!')
i = 0
count = int(message.content[1:2]) + 1
dim = int(message.content[3:])
lst = []
for i in range(1, count):
rand_num = random.randint(1, dim)
lst.append(rand_num)
await message.channel.send(lst)
Oh, let's put on a magic spell that will disappear.
I tried to deploy it.
Yeah, I'm touching. That's why I was able to do it without problems so far, so I will try to see if selenium works with the bot deployed on heroku.
discord.py
Abbreviation
options = Options()
options.binary_location = '/app/.apt/usr/bin/google-chrome'
options.add_argument('--headless')
options.add_argument('--disable-gpu')
browser = webdriver.Chrome(options=options)
browser.implicitly_wait(10)
As a result, I was able to go with this. (Addition of selenium and chrome to heroku is required separately) After that, I feel like I'll do my best for find_element.
Then you can go to beautiful soup, right? When
discord.py
Abbreviation
elif message.content == '/Shake':
res = requests.get('http://www.jma.go.jp/jp/quake/00000000093.html')
soup = BeautifulSoup(res.text, 'html.parser')
result = soup.select_one('table.textframe').get_text(strip=True)
await message.channel.send(result)
After that, I thought it would be convenient to open a browser, but webbrowser.open_new is useless (naturally ...) So, let's cheat here by making it a URL if something like discord's URL format is written. You just have to move!
discord.py
Abbreviation
elif message.content == '/Search':
await message.channel.send("https://www.google.co.jp")
Solution. Muscles solve everything.
So even an amateur who didn't know heroku or deploy at all could do it. Qiita is the best ...
After that, you can extend it by making the command part fuzzy or adding processing for it. I don't do TRPG, so I don't know what the function is ...
Recommended Posts