# DC bot Part2 & 線上託管 & Cogs ## 點名 & 記得交社費 ![qrcode](https://hackmd.io/_uploads/rkowOQEEp.png) --- # Replit https://replit.com/ 註冊帳號 ### Ceate Repl ![螢幕擷取畫面 2023-11-08 123656.png](https://hackmd.io/_uploads/SkbYl5_Q6.png) ### Add Secret ![螢幕擷取畫面 2023-11-08 124433.png](https://hackmd.io/_uploads/SybDE5O7a.png) ![螢幕擷取畫面 2023-11-08 124602.png](https://hackmd.io/_uploads/r1f_N9_Q6.png) >按add secret > ![螢幕擷取畫面 2023-11-08 125622.png](https://hackmd.io/_uploads/BkMMH5OQT.png) >加這幾行導入secret內容 ### 最後應該長這樣 ![螢幕擷取畫面 2023-11-08 124847.png](https://hackmd.io/_uploads/HkU2S9OXp.png) ### 按Run執行,bot上線 ![螢幕擷取畫面 2023-11-08 125102.png](https://hackmd.io/_uploads/Byly8quQT.png) --- # 線上託管 ## keep alive 下載相關檔案 : https://drive.google.com/drive/folders/1XhSlW9mJCFuAedfgPx53Uwf8nYHAgV9p ### 把keep_alive.py上傳到repl裡 ![螢幕擷取畫面 2023-11-08 160401.png](https://hackmd.io/_uploads/rJtVb6_Qa.png) ### 加入下面的程式碼 ```python= import keep_alive keep_alive.keep_alive() ``` ![螢幕擷取畫面 2023-11-08 160915.png](https://hackmd.io/_uploads/H1d2Mad7T.png) ## UptimeRobot >bot監視器 https://uptimerobot.com/ 註冊帳號 ### add new monitor ![螢幕擷取畫面 2023-11-12 135316](https://hackmd.io/_uploads/HJZ2_JAQT.png) ![螢幕擷取畫面 2023-11-12 141315](https://hackmd.io/_uploads/B1QzpJRXT.png) ### url取得 ![螢幕擷取畫面 2023-11-12 140952](https://hackmd.io/_uploads/ByFL2yRQT.png) ![螢幕擷取畫面 2023-11-12 141126](https://hackmd.io/_uploads/Hkfykg0QT.png) ### 然後記得回去按Run --- # Cogs > 把你的功能模組化 ### 類別(class) >物件導向(OOP) 其實就是分門別類 以"學校"為例 其實我們創建的是名稱是"學校"的物件(定義何為"學校") 用法: ```python= class school: #constructor def __init__(self, student): #attribute self.table=10 self.chair=5 self.student=student #method def good_morning(self): print("Hello!!!") #method def student_num(self): print(self.student) ``` ```python= TCFSH = school(1000) TCGS = school(2000) print(TCFSH.table) #10 print(TCGS.table) #10 print(TCFSH.student) #1000 print(TCGS.student) #2000 TCFSH.student_num() #1000 TCFSH.good_morning() #"Hello!!!" ``` ### 寫法 main.py : ```python= for fn in os.listdir("cogs"): if fn.endswith(".py"): bot.load_extension(f"cogs.{fn[:-3]}") @bot.event async def on_ready(): print(f"目前登入身份 --> {bot.user}") channel=bot.get_channel(int(os.environ['contest_channel'])) await bot.change_presence(status=nextcord.Status.online, activity=ectivity) @bot.command() async def load(ctx,extension): bot.load_extension(f"cogs.{extension}") await ctx.send("loaded cog!") @bot.command() async def unload(ctx,extension): bot.unload_extension(f"cogs.{extension}") await ctx.send("unloaded cog!") @bot.command() async def reload(ctx,extension): bot.reload_extension(f"cogs.{extension}") await ctx.send("reloaded cog!") ``` func.py : ```python= import nextcord from nextcord.ext import commands class func(commands.Cog): def __init__(self,bot): self.bot = bot self.number = 10 @commands.command() async def my_func(self, ctx): await ctx.send("嗨嗨") def setup(bot): bot.add_cog(roles(bot)) ``` ### cogs資料夾 記得新增一個cogs資料夾 把所有cog檔案都放進去(main.py和keep_alive.py不用放) ## 沒了