slash command
2023 寒訓
Foreword
這次的內容會跟暑訓有很多不一樣的地方
暑訓教過的現在已經不能用了
ex.slash command, intents...
了解 discord bot 的運作原理
之後實作時會有很大的幫助
discord bot 有 python 以及 js 兩種語言的套件,而我們要用的discord.py是建立在python
因此需要先學習 python 語法才可開始實作
開始玩機器人
Operating Principle
利用程式碼來操控的機器人帳號
使用者發送訊息到 discord 伺服器
機器人接收到訊息
回傳到 replit 上的 discord API
機器人發送訊息到 discord 伺服器
利用程式處裡訊息
使用者接收訊息
難的後面邊寫邊教
變數種類 | 功能 |
---|---|
int | 整數 |
str | 字串 |
bool | True/False |
float | 浮點數 |
import discord
import os
from discord.ext import commands
a = 5
b = a + 5
Prework
終於可以開始寫程式
import discord
from discord import app_commands
from discord.ext import commands
import os
intents = discord.Intents.default()
bot = commands.Bot(intents=intents, command_prefix='!')
@bot.event
async def on_ready():
print("online")
try:
synced = await bot.tree.sync()
print("success")
except:
print('fail')
@bot.tree.command(name='hello')
@app_commands.describe(say="say hello")
async def hello(interaction: discord.Interaction, say: str):
await interaction.response.send_message(say)
bot.run(your_token)
Command Design
@bot.tree.command(name='example_code')
async def example_code(interaction: discord.Interaction):
#回覆的程式
學會使用 API 才能讓你開發得更輕鬆
Data Structure
電腦中儲存、組織資料的方式
Database
一系列表中的行和欄建模,以提高處理和資料查詢的效率
使用的套件是 replit 裡的 database
機器人有可能因為各種原因中斷程式
存在變數中的資料會不見
練習建立完整的專案
每個小隊建立一個完整的專案,依據專案有達成的任務給分
任務編號 | 內容 | 分數 |
---|---|---|
任務一 | 登入小隊的機器人 | 35pt |
任務二 | 建立 slash command | 15pt |
任務三 | 建立含有參數的指令 | 30pt |
任務四 | 利用 embed 製作小隊個人介紹 | 30pt |
任務五 | ??????? | 0pt |
任務六 | ??????????????????? | 0pt |
任務七 | ?????? | 0pt |
任務八 | 根據創意評分 | 90pt |
Total | 200pt |
問講師
問 google
查 discord.py API
24/7 discord bot
優點 : 免費
缺點 : 不穩定