How to make a rock-paper-scissors bot that can be easily moved (commentary)

First of all

As a major premise, it may be a little difficult without basic knowledge of python (** if statement **?) Not at all! ~~ Python? What is it delicious? I think that people can understand somehow by studying a little at the following site. I'm a beginner myself, so I don't understand it perfectly. I don't know if it works like this, but I'll leave it as a memorandum. Well, I want to make a rock-paper-scissors bot for the time being! Please copy and move the person appropriately

-Introduction to Python3 (11 lessons in total) -Progate Python Course

Writer's environment

Prerequisites

We will proceed on the assumption that python and discord.py will work in your editor It doesn't work! Please make it work based on the articles posted by other people. Not specifically explained in this article

Let's go to the main subject

Finally to the code

In conclusion, if you copy this code, it will work, ~~ maybe ~~

JankenBot


import discord
import random
client = discord.Client()


@client.event
async def on_message(message):
"""Process the following message"""
    global result, judge
    if message.author.bot:  #Avoid messages from bots
        return

    if message.content == "!! Rock-paper-scissors":
        await message.channel.send("At first, goo, rock-paper-scissors")

        jkbot = random.choice(("Goo", "Choki", "Par"))
        draw = "It's a draw ~"
        wn = "You win!"
        lst = random.choice(("My win! Weak wwwwwwwwwwww What if I quit? Rock-paper-scissors",
                              "I won(∩´∀`)∩, try again!"))

        def jankencheck(m):
            return (m.author == message.author) and (m.content in ['Goo', 'Choki', 'Par'])

        reply = await client.wait_for("message", check=jankencheck)
        if reply.content == jkbot:
            judge = draw
        else:
            if reply.content == "Goo":
                if jkbot == "Choki":
                    judge = wn
                else:
                    judge = lst

            elif reply.content == "Choki":
                if jkbot == "Par":
                    judge = wn
                else:
                    judge = lst

            else:
                if jkbot == "Goo":
                    judge = wn
                else:
                    judge = lst

        await message.channel.send(judge)

Well, it looks like this. If you try this for the time being

無題.png

It works like this. Let's dig a little deeper.

Brief commentary

I can only give a really simple explanation, but there are only a few things that are difficult to understand personally. (~~ The author's lack of knowledge is revealed ~~)

    if message.content == "!! Rock-paper-scissors":
        await message.channel.send("At first, goo, rock-paper-scissors")

First of all in this part **! Rock-paper-scissors ** If entered ** At first, it is written to return Goo, Rock-paper-scissors **. I remember this because I use it not only for this rock-paper-scissors bot but also for making something else (self-discipline)

        jkbot = random.choice(("Goo", "Choki", "Par"))

Here to ** jkbot **

--Goo --Choki ――Par

Any one of them will be randomly assigned.

        lst = random.choice(("My win! Weak wwwwwwwwwwww What if I quit? Rock-paper-scissors",
                              "I won(∩´∀`)∩, try again!"))

Here, somehow I wrote only the second message at first, but I wanted stimulation, so I also randomized the losing message by using a random function. If you don't need it, substitute it normally like ** wn ** or ** draw **.

        def jankencheck(m):
            return (m.author == message.author) and (m.content in ['Goo', 'Choki', 'Par'])

        reply = await client.wait_for("message", check=jankencheck)

** in this part! Waiting for a message from the person who started rock-paper-scissors **, the sent message

--Goo --Choki ――Par

If it is True, continue to the following code ~~ Honestly, I don't really understand this part myself ~~ I'm writing it as if it's something like this.

        if reply.content == jkbot:
            judge = draw
        else:
            if reply.content == "Goo":
                if jkbot == "Choki":
                    judge = wn
                else:
                    judge = lst

            elif reply.content == "Choki":
                if jkbot == "Par":
                    judge = wn
                else:
                    judge = lst

            else:
                if jkbot == "Goo":
                    judge = wn
                else:
                    judge = lst

It ’s easy here, is n’t it? To be honest, I have nothing to explain. ~~ Because I can only say what I saw ~~

        await message.channel.send(judge)

This will send the last value assigned to ** judge **.

Are you unsatisfied?

With this, it should work as a rock-paper-scissors bot without any problems ~~ I want something a little more! For greedy beginners, I also put a code that automatically sends a message when the bot starts. I'm happy with the rock-paper-scissors bot because I'll only write the code for it and things about it below! Thank you for your hard work! Please comment if you have any.

For greedy people

The event when the bot starts

@client.event
async def on_ready():
"""Write the process that occurs when the bot starts below"""

You can write with. This time, we will add a function to notify you on Discord when you start the bot.

kidou.png

It looks like this.

First, decide which channel you want the message to be sent to, and copy the ID of that channel. If you don't know how to copy the ID, please check it yourself, I will omit it.

After copying

channel = client.get_channel(ID)

Please rewrite the ID you copied earlier in the ID part. You can leave the copied numbers as they are

Now you can specify the channel

And finally

await channel.send("etc")  #Please put your favorite sentences in the etc part

If you write, it will be sent automatically when the bot starts.

Finally

First of all, thank you for your hard work. I'm sorry that the explanation may have been difficult to understand due to my poor Japanese ability and lack of vocabulary. : bow:

Since this is an article for beginners, it may be a code that makes you want to make various complaints if you have some knowledge, but please forgive me. I was able to move something even if I didn't understand much at first, including myself! I think that the impression is the way to the next step ...

That's all for this article. If you have any problems or mistakes, please contact us on Twitter.

Recommended Posts

How to make a rock-paper-scissors bot that can be easily moved (commentary)
How to make a slack bot
[Python] Make a graph that can be moved around with Plotly
Make a currency chart that can be moved around with Plotly (2)
Make a currency chart that can be moved around with Plotly (1)
How to install a Python library that can be used by pharmaceutical companies
I tried to make a memo app that can be pomodoro, but a reflection record
How to create a property of relations that can be prefetch_related by specific conditions
How to make a Cisco Webex Teams BOT with Flask
Let's make a diagram that can be clicked with IPython
How to make a Japanese-English translation
How to set up a simple SMTP server that can be tested locally in Python
How to make a crawler --Advanced
How to make a recursive function
How to make a deadman's switch
[Blender] How to make a Blender plugin
How to make a crawler --Basic
Make a Spinbox that can be displayed in Binary with Tkinter
Make a Spinbox that can be displayed in HEX with Tkinter
[Python] How to make a class iterable
How to make a Backtrader custom indicator
How to make a Pelican site map
Python beginners decided to make a LINE bot with Flask (Flask rough commentary)
Create a web app that can be easily visualized with Plotly Dash
Make a Discord Bot that you can search for and paste images
Draw a graph that can be moved around with HoloViews and Bokeh
How to make a dialogue system dedicated to beginners
A memo for making a figure that can be posted to a journal with matplotlib
I tried to create a class that can easily serialize Json in Python
I want to create a priority queue that can be updated in Python (2.7)
How to make a dictionary with a hierarchical structure.
If you want to make a Windows application (exe) that can be actually used now using only Python
Make a chatbot and practice to be popular.
How to make a QGIS plugin (package generation)
I read "How to make a hacking lab"
I tried to make a translation BOT that works on Discord using googletrans
How to make a Raspberry Pi that speaks the tweets of the specified user
"Gazpacho", a scraping module that can be used more easily than Beautiful Soup
A new form of app that works with GitHub: How to make GitHub Apps
Convert images from FlyCapture SDK to a form that can be used with openCV
How to create a USB that Linux and Win10 installer and winpe can boot UEFI
How to filter foreign keys that can be selected on the Django admin screen
You can do it in 3 minutes! How to make a moving QR code (GIF)!
A Python beginner made a chat bot, so I tried to summarize how to make it.
The world's most easy-to-understand explanation of how to make a LINE BOT (1) [Account preparation]
A mechanism to call a Ruby method from Python that can be done in 200 lines
Make a BOT that shortens the URL of Discord
How to make a shooting game with toio (Part 1)
How to make a Python package using VS Code
[Python] How to write a docstring that conforms to PEP8
Basics of PyTorch (2) -How to make a neural network-
How to start a simple WEB server that can execute cgi of php and python
A disocrd bot that even beginners can regularly post Splatoon 2 results to stat.ink for free
I made a module that can be glitched easily, but I can't pass arguments from entry_points
How to display hover text and text that can be executed by clicking with Minecraft plugin
I tried to make a bot that randomly acquires Wikipedia articles and tweets once a day
[Python] A program to find the number of apples and oranges that can be harvested
How to make an interactive LINE BOT 004 (answer the closing date of a listed company)
How to set variables that can be used throughout the Django app-useful for templates, etc.-
Convert mesh data exported from SpriteUV2 to a format that can be imported by Spine
A memo on how to easily prepare a Linux exercise environment