discord bot

slash command

2023 寒訓

講師介紹

課程分數計算

前言

Foreword

去年暑訓已經學過了?

這次的內容會跟暑訓有很多不一樣的地方

暑訓教過的現在已經不能用了

ex.slash command, intents...

在這堂課中你可以學到...

課程大綱

運作原理

了解 discord bot 的運作原理

之後實作時會有很大的幫助

python 語法

discord bot 有 python 以及 js 兩種語言的套件,而我們要用的discord.py是建立在python

因此需要先學習 python 語法才可開始實作

實作

開始玩機器人

運作原理

Operating Principle

什麼是 discord bot?

利用程式碼來操控的機器人帳號

1

使用者發送訊息到 discord 伺服器

2

機器人接收到訊息

3

回傳到 replit 上的 discord API

5

機器人發送訊息到 discord 伺服器

4

利用程式處裡訊息

6

使用者接收訊息

基礎 python 語法

難的後面邊寫邊教

變數

變數種類 功能
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

學會使用 API 才能讓你開發得更輕鬆

API 連結

查詢方式

資料結構

Data Structure

什麼是資料結構

電腦中儲存、組織資料的方式

常見資料結構

資料庫

Database

什麼是資料庫?

一系列表中的行和欄建模,以提高處理和資料查詢的效率

使用方式

使用的套件是 replit 裡的 database

https://hackmd.io/@QBe3gR3hR1qK4S8Za3XfLA/BJRiDwAsj

為什麼要用資料庫?
直接用dict存不是比較簡單嗎

機器人有可能因為各種原因中斷程式

存在變數中的資料會不見

小隊任務

練習建立完整的專案

計分方式

每個小隊建立一個完整的專案,依據專案有達成的任務給分

任務編號 內容 分數
任務一 登入小隊的機器人 35pt
任務二 建立 slash command 15pt
任務三 建立含有參數的指令 30pt
任務四 利用 embed 製作小隊個人介紹 30pt
任務五 ??????? 0pt
任務六 ??????????????????? 0pt
任務七 ?????? 0pt
任務八 根據創意評分 90pt
Total 200pt

任務列表

有不會的地方

問講師

問 google

查 discord.py API

保持機器人上線

24/7 discord bot

放到replit上

優點 : 免費

缺點 : 不穩定

申請建中資訊社伺服器

discord bot - slash command

By Howie Chen

discord bot - slash command

2023 四校聯合寒訓

  • 150