LoginSignup
35
22

More than 3 years have passed since last update.

Discord.pyで湯婆婆を作る

Last updated at Posted at 2020-11-08

はじめに

@Nemesis さんの Javaで湯婆婆を実装してみる と、@nekozuki_dev さんの Discord Botで湯婆婆を実装してみるのdiscord.py版です。
discord.py初見の方は Pythonで実用Discord Bot(discordpy解説) みたいなやつを読んで下さい。(いないと思うけど)
初記事なので見にくいところがあったらごめんなさい。
ライトテーマ注意!

置き換え

名前の入力

Scanner keiyakusho = new Scanner(System.in);
String name = keiyakusho.nextLine();

原作ではこのようにコンソールから入力を求めていますが、

ニックネーム

「ニックネームがあるじゃん!」
というわけで今回はニックネームを使います。

出力

System.out.println("今からお前の名前は"+newName+"だ。いいかい、"+newName+"だよ。分かったら返事をするんだ、"+newName+"!!");

原作では結果を出力するだけになっていますが、Discord APIではニックネームを変更することができるので、実際にニックネームを変更しようと思います。

コードを書く

実際にコードを書いていきます。

基本のBot

公式が用意しているやつを使います。

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')

これで$helloと送信すると反応してくれるBotが出来ました。
Hello!

メンションで反応するようにする

User.mentioned_in(ClientUser.mentioned_in)を使ってメンション判定をします。

if message.content.startswith('$hello'):
  await message.channel.send('Hello!')

if client.user.mentioned_in(message):
  await message.channel.send('Hello!')

にします。
メンションで反応
湯婆婆が英語を話す貴重なシーン

反応を変える

await message.channel.send('Hello!')

ここを変えていきます。


System.out.println("契約書だよ。そこに名前を書きな。");

Scanner keiyakusho = new Scanner(System.in);
String name = keiyakusho.nextLine();

System.out.println("フン。"+name +"というのかい。贅沢な名だねぇ。");

Random random = new Random();
int newNameIndex = random.nextInt(name.length());
String newName = name.substring(newNameIndex,newNameIndex+1);

System.out.println("今からお前の名前は"+newName+"だ。いいかい、"+newName+"だよ。分かったら返事をするんだ、"+newName+"!!");

これは原作のコードです。
これをPythonでかき直すと、

import random
#~~~~~~~~~
print("契約書だよ。そこに名前を書きな。")

name = input()

print(f"フン。{name}というのかい。贅沢な名だねぇ。")

new_name = random.choice(name)

print(f"今からお前の名前は{new_name}だ。いいかい、{new_name}だよ。分かったら返事をするんだ、{new_name}!!")

こうなります。
これをdiscord.py仕様に書き直すと…

import random
#~~~~~~~~~
await message.channel.send("契約書だよ。そこに名前を書きな。")

await message.channel.send(f"(あなたは契約書に`{message.author.display_name}`と書き込んだ。)")

name = message.author.display_name

await message.channel.send(f"フン。`{name}`というのかい。贅沢な名だねぇ。")

new_name = random.choice(name.replace(" ",""))

await message.channel.send(f"今からお前の名前は`{new_name}`だ。"
f"いいかい、`{new_name}`だよ。分かったら返事をするんだ、`{new_name}`!!")

await message.author.edit(nick=new_name)

await message.channel.send(f"(あなたのニックネームは`{new_name}`になった。)")

こうなります。
はっや!!!
(ここからサーバーを変えています。)
早すぎるので少し待たせていきます。

ロースペックにする

asyncio.sleepを使って応答を遅くします。
asyncio.sleepを使わないとBot全体が止まるので注意!

import random
import asyncio
#~~~~~~~~~
await message.channel.send("契約書だよ。そこに名前を書きな。")
await asyncio.sleep(1)
await message.channel.send(f"(あなたは契約書に`{message.author.display_name}`と書き込んだ。)")

name = message.author.display_name
await asyncio.sleep(2)
await message.channel.send(f"フン。`{name}`というのかい。贅沢な名だねぇ。")

new_name = random.choice(name.replace(" ",""))
await asyncio.sleep(4)
await message.channel.send(f"今からお前の名前は`{new_name}`だ。"
f"いいかい、`{new_name}`だよ。分かったら返事をするんだ、`{new_name}`!!")

await message.author.edit(nick=new_name)

await message.channel.send(f"(あなたのニックネームは`{new_name}`になった。)")

こうなりました。
これをメインコードに組み込んだ結果が…
完成
もう完成でいいね!異論は認めない

最後に

discord.pyで湯婆婆を作りました。
初記事なので見にくいところがあるかもしれません。
そういうときはコメントでお願いします。

最後まで読んでいただいてありがとうございました。

今回のソース

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("契約書だよ。そこに名前を書きな。")
        await asyncio.sleep(1)
        await message.channel.send(f"(あなたは契約書に`{message.author.display_name}`と書き込んだ。)")

        name = message.author.display_name
        await asyncio.sleep(2)
        await message.channel.send(f"フン。`{name}`というのかい。贅沢な名だねぇ。")

        new_name = random.choice(name.replace(" ",""))
        await asyncio.sleep(4)
        await message.channel.send(f"今からお前の名前は`{new_name}`だ。"
        f"いいかい、`{new_name}`だよ。分かったら返事をするんだ、`{new_name}`!!")

        await message.author.edit(nick=new_name)

        await message.channel.send(f"(あなたのニックネームは`{new_name}`になった。)")

client.run("your token here")

補足

やろうとした人の権限が上だとForbidden吐きます。
できたらそれはそれで問題なのでヨシ!

35
22
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
35
22