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
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
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
It works like this. Let's dig a little deeper.
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 **.
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.
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.
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.
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