--- title: DCbot tags: 教學簡報 --- # Dc bot製作 --- # 簡易物件導向 ---- ## 什麼是物件? - Python 中的變數都是一個物件 - 每個物件都又自己的屬性和方法 - 相同種類的物件有相同的屬性和方法 - 也稱作相同型別 ---- ### 先來寫好我們的物件 ```python ##檔名module class Car: #我們用clas來定義物件,而Car就是物件(object) def __init__(self, make, model, year, color) self.make = make self.model = model self.year = year self.color = color #這個物件的屬性(property) def drive(self): print(f"This {self.model} is driving") def drive(self): print(f"This {self.model} is stopped") ##這個物件方法(method) ``` ---- ### 接著我們的主程式 ```python from module import Car ##從module導入物件Car car_1 = Car("Chevy","Corvette","2021","blue") car_2 = Car("Ford","Mustang",2022,"red") car_1.drive() print(car_1.model) ``` --- # DC bot ---- ### 記得下載我們需要用到的模組,在CMD打下列句子 ``` pip install discord.py ``` ---- ### 接著到DC developer創建機器人,完成設定利用網址邀請至伺服器  ---- ### 請確保intent(權限)是打開的狀態  ### 接著我們來快速的看一下DC bot的建置 ---- ### DC bot quick start ```python import discord ##將模組導入 intents = discord.Intents.default() intents.message_content = True#開啟DCbot的權限 client = discord.Client(intents=intents) ##與 Discord 的連接 @client.event async def on_ready(): print(f'We have logged in as {client.user}') ##機器人完成登錄和設置 @client.event async def on_message(message): ##當機器人收到消息時觸發事件 if message.author == client.user: ##確保忽略來自我們自己的消息 return if message.content.startswith('$hello'): await message.channel.send('Hello!') ##收到$hello時輸出Hello! client.run('your token here')#放進你的Token ``` ---- # 如何知道其他機器人的製作發法 ---- ## 1.去看官方API看看各種機器人的用法 https://discordpy.readthedocs.io/en/stable/api.html ---- ## 2.去看影片或爬論壇,很多人都會分享如何寫出一種機器人的功能 ### 記得找比較新的影片或論壇,bot的語法會更新 https://github.com/ https://www.reddit.com/user/criddit_/ https://www.youtube.com/ ---- ### 開始製作機器人!!! ### 只要想你想做什麼就講出來,我們都會支援你
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up