[PYTHON] I made a discord bot

Trigger

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.

Basic processing

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)

Try rolling the dice

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)
  • Promise that n is 1-9 and N is your favorite number in nDN. It's a pretty powerful technique, but I'll add the process of throwing the N-side dice n times. Does random return really random numbers? I will leave the Culdcept perspective at this time.

Oh, let's put on a magic spell that will disappear.

I tried to deploy it.

キャプチャ.PNG

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.

Try running selenium

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

Try beautiful soup

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)
  • It is assumed that the earthquake information of the Japan Meteorological Agency will be pulled into the sample. Now it's okay if an earthquake hits during the session! Don't panic / shake it and you'll know! I want to return that it hasn't shaken. Recognize.

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!

I want to open a browser

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

I made a discord bot
I made a wikipedia gacha bot
〇✕ I made a game
When I made a Discord Bot, my classmates destroyed my computer
I made a Discord chat reading bot by imitating the appearance
I made a stamp substitute bot with line
I made a Mattermost bot with Python (+ Flask)
Let's make a Discord Bot.
I made a python text
I made a Discord bot in Python that translates when it reacts
[AWS] I made a reminder BOT with LINE WORKS
I made a Twitter BOT with GAE (python) (with a reference)
I made a household account book bot with LINE Bot
I made a LINE BOT with Python and Heroku
I made a CUI-based translation script (2)
I made a fortune with Python.
I made a CUI-based translation script
I made a daemon with Python
I made a LINE BOT that returns parrots with Go
[AWS] I made a reminder BOT with LINE WORKS (implementation)
[Python] I made a Line bot that randomly asks English words.
I made a new AWS S3 bucket
I made a dash docset for Holoviews
I made a payroll program in Python!
I made a character counter with Python
Beginner: I made a launcher using dictionary
I made a Twitter bot that mutters Pokemon caught by #PokemonGO
I made a Twitter Bot with Go x Qiita API x Lambda
I made a script to display emoji
I made a slack bot that notifies me of the temperature
I made a life game with Numpy
I made a stamp generator with GAN
I made a browser automatic stamping tool.
After studying Python3, I made a Slackbot
I made a roguelike game with Python
I made a simple blackjack with Python
I made a configuration file with Python
I made a library for actuarial science
I made a WEB application with Django
I made a neuron simulator with Python
I made a python dictionary file for Neocomplete
I made a competitive programming glossary with Python
I made a weather forecast bot-like with Python.
I made a useful tool for Digital Ocean
I made a crazy thing called typed tuple
I made a router config collection tool Config Collecor
I made a simple Bitcoin wallet with pycoin
I made a downloader for word distributed expression
I made a tool to compile Hy natively
I made a tool to get new articles
I tried to operate Linux with Discord Bot
I made a random number graph with Numpy
I made a peeping prevention product for telework.
I made a simple RSS reader ~ C edition ~
I made a Caesar cryptographic program in Python.
I made a bin picking game with Python
I made a Python Qiita API wrapper "qiipy"
I made a QR code image with CuteR
I made a LINE BOT that returns a terrorist image using the Flickr API
Discord bot memorandum ① bot creation
I made a Line Bot that uses Python to retrieve unread Gmail emails!