LoginSignup
2
2

More than 3 years have passed since last update.

Discord bot「雲さん」に占い機能を追加した

Last updated at Posted at 2019-06-15

はじめに

以前記事にしたDiscord bot「雲さん」へ追加した機能のご紹介です。
詳しくは「Discord bot「雲さん」を作ってみた」をご参照下さい。

追加機能

関数本体

  • 関数の呼び出しはtext.find("座") > -1とかにして使って下さい。
  • Discordの絵文字はSlack:hoge:とすることで表示できるため、簡単にそれっぽい見た目になっていい感じでした。
horoscope.py
# Horoscope
def uranai(text):
    response_string = ''
    seiza = 100
    date = datetime.datetime.today().strftime("%Y/%m/%d")
    index_st = text.find(' ')+1
    index_ed = text.find('座')+1
    search_text = text[index_st:index_ed]
    print(search_text)
    seiza_list = ("牡羊座", "牡牛座", "双子座", "蟹座", "獅子座", "乙女座", "天秤座", "蠍座", "射手座", "山羊座", "水瓶座", "魚座")
    seiza_list2 = ("おひつじ座", "おうし座", "ふたご座", "かに座", "しし座", "おとめ座", "てんびん座", "さそり座", "いて座", "やぎ座", "みずがめ座", "うお座") 
    try:
        seiza = seiza_list.index(search_text)
    except:
        try:
            seiza = seiza_list2.index(search_text)
        except Exception as e:
            response_string = "君の星が…見えない… :cloud:"
    try:
        res = requests.get(url='http://api.jugemkey.jp/api/horoscope/free/'+ date)
        txt = res.json()["horoscope"][date][seiza]
        job = emoji(int(txt["job"]), "job")
        money = emoji(int(txt["money"]), "money")
        love = emoji(int(txt["love"]), "love")
        total = emoji(int(txt["total"]), "total")
        response_string = "今日の**" + txt["sign"] + "**の運勢は。。。**" + str(txt["rank"]) + "位**!\n" + txt["content"] + "\nラッキーアイテムは**" + txt["item"] + "**で、ラッキーカラーは**" + txt["color"] + "**。\n仕事運:"+job+"\n金 運:" + money + "\n恋愛運:" + love + "\nトータルは..." + total + "です!良い一日を!"
        #print(response_string)
    except Exception as e:
        print("君の星が…見えない… :cloud:")
    return response_string

def emoji(number, category):
    res = ''
    for i in range(number):
        if category == 'job':
            res += ':briefcase: '
        elif category == 'money':
            res += ':moneybag: '
        elif category == 'love':
            res += ':heart: '
        elif category == 'total':
            res += ':star: '
    return res

おまけ

めちゃめちゃ単純なおみくじ。

omikuji.py
# Omikuji
def omikuji():
    response_string = ''
    lots = ["大吉", "吉", "中吉", "小吉", "半吉", "末吉", "末小吉", "平", "凶", "小凶", "半凶", "末凶", "大凶", "未分"]
    random.shuffle(lots)
    lot = lots[0]
    print(lot)
    response_string = "あなたの運勢は...ガシャガシャガシャガシャ...\n\n    " + lot + " ですね!"

    return response_string

使い方

  • 雲さん 双子座の今日の運勢は?
    スクリーンショット 2019-06-16 03.19.43.png

  • 雲さん 占い!
    スクリーンショット 2019-06-16 03.20.01.png

参考

2
2
0

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
2
2