iT邦幫忙

2021 iThome 鐵人賽

DAY 7
0
AI & Data

建立FF14資訊Discord chatbot系列 第 7

[DAY 07]查詢各國物品名稱

昨天寫完查詢物品拍賣價格網址後發現...既然都有各國物品名稱了

乾脆多做一個查詢各國物品名稱並附上WIKI連結的功能

那麼就直接附上程式碼~

import discord
import requests
import pandas as pd
import pickle
import difflib
from dotenv import load_dotenv



if __name__ == '__main__':
    #讀取Token
    load_dotenv()
    TOKEN = os.getenv('DISCORD_TOKEN')
    
    intents = discord.Intents.default()
    intents.members = True
    client = discord.Client(intents=intents)
    embed = discord.Embed()
    
    
    @client.event
    #當有訊息時
    async def on_message(message):
        #排除自己的訊息,避免陷入無限循環
        if message.author == client.user:
            return
        
        #翻譯
        if message.content.startswith('?tr '):
            user_word = message.content.replace('?tr ',"")
            user_word = user_word.lstrip().rstrip()
            if user_word in item_dict:
                if "簡體中文" in item_dict[user_word]:
                    user_wordlist = [f"{key} : {value}" for key,value in item_dict[user_word].items()]+[f"[詳細資訊連結](https://ff14.huijiwiki.com/wiki/%E7%89%A9%E5%93%81:{item_dict[user_word]['簡體中文']})"]
                    embed.description = "\n".join(user_wordlist)
                    await message.reply(embed=embed, mention_author=True)
                    # await message.channel.send(embed=embed)
                    
                else:
                    user_wordlist = [f"{key} : {value}" for key,value in item_dict[user_word].items()]+[f"[詳細資訊連結](https://ff14.huijiwiki.com/wiki/%E7%89%A9%E5%93%81:{user_word})"]
                    embed.description = "\n".join(user_wordlist)
                    await message.reply(embed=embed, mention_author=True)

                    # await message.channel.send(embed=embed)
        #查市價
        elif message.content.startswith('?bs '):
            user_word = message.content.replace('?bs ',"")
            user_word = user_word.lstrip().rstrip()
            if user_word in item_dict:
                if "ID" in item_dict[user_word]:
                    embed.description = f"[{user_word}價格網址](https://universalis.app/market/{item_dict[user_word]['ID']})"
                    await message.reply(embed=embed, mention_author=True)

                else:
                    embed.description = f"[{user_word}價格網址](https://universalis.app/market/{user_word})"
                    await message.reply(embed=embed, mention_author=True)

            else:
                #模糊搜尋
                wordsim_list = difflib.get_close_matches(user_word,wordlist,10,cutoff=0.1)
                if len(wordsim_list) > 0:
                    embed.description ="你可能要查詢的詞:\n"+"\n".join(wordsim_list)
                    await message.reply(embed=embed, mention_author=True)

                else:
                    await message.reply("無相關資訊")
client.run(TOKEN) #TOKEN 在剛剛 Discord Developer 那邊「BOT」頁面裡面

呈現結果如下


上一篇
[DAY 06]物品拍賣價格查詢功能(4/4)
下一篇
[DAY 08]新成員進來時靠bot進行說明事項
系列文
建立FF14資訊Discord chatbot30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言