@ Nemesis's Try to implement Yubaba in Java and @ nekozuki_dev's Try to implement Yubaba in Discord Bot This is the discord.py version of (http://qiita.com/nekozuki_dev/items/485d47a459a63f59400f). If you are new to discord.py, please read something like Practical Discord Bot in Python (discordpy explanation). (I don't think) This is the first article, so I'm sorry if there is something difficult to read. ** Light theme attention! ** **
Scanner keiyakusho = new Scanner(System.in);
String name = keiyakusho.nextLine();
In the original, input is requested from the console like this,
"You have a nickname!" That's why I will use a nickname this time.
System.out.println("From now on your name is"+newName+"It is. Mind you,"+newName+"That's right. I'll reply when I understand"+newName+"!!");
In the original, it only outputs the result, but since you can change the nickname in the Discord API, I will actually change the nickname.
I will actually write the code.
Use the one that is Official prepared.
import discord
client = discord.Client()
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
await message.channel.send('Hello!')
client.run('your token here')
Now you have a bot that responds when you send $ hello
.
User.mentioned_in
(ClientUser.mentioned_in
Use /ja/latest/api.html#discord.ClientUser.mentioned_in)) to make a mention judgment.
if message.content.startswith('$hello'):
await message.channel.send('Hello!')
To
if client.user.mentioned_in(message):
await message.channel.send('Hello!')
To ~~ A precious scene where Yubaba speaks English ~~
await message.channel.send('Hello!')
I will change here.
System.out.println("It's a contract. Write your name there.");
Scanner keiyakusho = new Scanner(System.in);
String name = keiyakusho.nextLine();
System.out.println("Hung."+name +"I mean. It's a luxurious name.");
Random random = new Random();
int newNameIndex = random.nextInt(name.length());
String newName = name.substring(newNameIndex,newNameIndex+1);
System.out.println("From now on your name is"+newName+"It is. Mind you,"+newName+"That's right. I'll reply when I understand"+newName+"!!");
This is the original code. If you rewrite this in Python,
import random
#~~~~~~~~~
print("It's a contract. Write your name there.")
name = input()
print(f"Hung.{name}I mean. It's a luxurious name.")
new_name = random.choice(name)
print(f"From now on your name is{new_name}It is. Mind you,{new_name}That's right. I'll reply when I understand{new_name}!!")
It will be like this. If you rewrite this to the discord.py specification ...
import random
#~~~~~~~~~
await message.channel.send("It's a contract. Write your name there.")
await message.channel.send(f"(You are in the contract`{message.author.display_name}`I wrote.)")
name = message.author.display_name
await message.channel.send(f"Hung.`{name}`I mean. It's a luxurious name.")
new_name = random.choice(name.replace(" ",""))
await message.channel.send(f"From now on your name is`{new_name}`It is."
f"Mind you,`{new_name}`That's right. I'll reply when I understand`{new_name}`!!")
await message.author.edit(nick=new_name)
await message.channel.send(f"(Your nickname is`{new_name}`Became.)")
It will be like this. (I am changing the server from here.) It's too early, so I'll keep you waiting for a while.
Use asyncio.sleep
to slow down the response.
** Note that if you don't use asyncio.sleep
, the whole bot will stop! ** **
import random
import asyncio
#~~~~~~~~~
await message.channel.send("It's a contract. Write your name there.")
await asyncio.sleep(1)
await message.channel.send(f"(You are in the contract`{message.author.display_name}`I wrote.)")
name = message.author.display_name
await asyncio.sleep(2)
await message.channel.send(f"Hung.`{name}`I mean. It's a luxurious name.")
new_name = random.choice(name.replace(" ",""))
await asyncio.sleep(4)
await message.channel.send(f"From now on your name is`{new_name}`It is."
f"Mind you,`{new_name}`That's right. I'll reply when I understand`{new_name}`!!")
await message.author.edit(nick=new_name)
await message.channel.send(f"(Your nickname is`{new_name}`Became.)")
Now this. The result of incorporating this into the main code is ... I like it already completed! ~~ No objection ~~
I made Yubaba on discord.py. Since this is the first article, it may be difficult to read. In that case, please leave a comment.
Thank you for reading to the end.
import discord
import random
import asyncio
client = discord.Client()
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
@client.event
async def on_message(message):
if message.author == client.user:
return
if client.user.mentioned_in(message):
await message.channel.send("It's a contract. Write your name there.")
await asyncio.sleep(1)
await message.channel.send(f"(You are in the contract`{message.author.display_name}`I wrote.)")
name = message.author.display_name
await asyncio.sleep(2)
await message.channel.send(f"Hung.`{name}`I mean. It's a luxurious name.")
new_name = random.choice(name.replace(" ",""))
await asyncio.sleep(4)
await message.channel.send(f"From now on your name is`{new_name}`It is."
f"Mind you,`{new_name}`That's right. I'll reply when I understand`{new_name}`!!")
await message.author.edit(nick=new_name)
await message.channel.send(f"(Your nickname is`{new_name}`Became.)")
client.run("your token here")
Forbidden spits if the person who tried to do it has higher authority. If possible, that's a problem, so Yoshi!
Recommended Posts