discord bot 教學

建資 陳泓宇

講師介紹

  • 建中資訊社 副社長兼學術
  • DC : chenhowie#1138
  • FB : Howie

什麼是 discord bot ? 

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

discord bot 可以...

  • 音樂即時點歌
  • 罐頭訊息回復
  • 投票
  • 群組代管

如何使用 discord bot

  • 在網路上找別人寫好的
  • 自己撰寫程式碼並架伺服器執行

discord bot 網站

在上面的網站都找不到你要的機器人怎麼辦

自己寫 discord bot

discord bot 運作方式

使用者發送訊息

discord 伺服器

機器人偵測訊息

replit 上的 discord API

處裡訊息

機器人發送訊息

discord 伺服器

使用者接收訊息

創建你的機器人

創建application

新增機器人

基本設定

如何取得 TOKEN

複製這串TOKEN

將機器人加入你的伺服器

機器人權限設定

在網址列貼上底下這串網址

創建你的第一個專案

選擇 python 並為專案命名

code 區

檔案區

Text

執行程式

開始撰寫機器人

登入

import os
import discord
from discord.ext import commands

bot = commands.Bot(command_prefix = "!")








token = os.environ['TOKEN']
bot.run(token)

設定TOKEN

為機器人新增功能

import os
import discord
from discord.ext import commands

bot = commands.Bot(command_prefix = "!")


@bot.command()
async def hello(ctx):
  await ctx.send("早安")


token = os.environ['TOKEN']
bot.run(token)

成功的話會長這樣

實作 - 1

輸入!name時 發送你的名字

為你的機器人加更多指令

import os
import discord
from discord.ext import commands

bot = commands.Bot(command_prefix = "!")

@bot.command()
async def hello(ctx):
  await ctx.send("hello")

@bot.command()
async def ping(ctx):
  await ctx.send("pong")

@bot.command()
async def awa(ctx):
  await ctx.send("ouo")
  await ctx.send("uwu")

token = os.environ['TOKEN']
bot.run(token)

指令中的引數

在ctx後還可以加更多的引數

@bot.command()
async def repeat(ctx, a, b):
  await ctx.send(a)
  await ctx.send(b)

設定指令中變數的型態

@bot.command()
async def repeat(ctx, a:str, b:str):
  await ctx.send(a)
  await ctx.send(b)
@bot.command()
async def add(ctx, a:int, b:int):
  await ctx.send(a+b)
  await ctx.send(a-b)

字串處理

f字串

name = chenhowie
num = 1234
string = f"hello {name} {num}"

print(string) //hello chenhowie 1234

大括號中放變數

實作 - 2

  • 輸入 !hello 學校 名字
  • 輸出 早安,OO國中的XXX

傳送圖片

圖片要放在哪裡?

@bot.command()
async def pic(ctx):
    picture = discord.File("example.png")
    await ctx.send(file=picture)

成功的話會長這樣

實作 - 3

輸入 !pic 生成一張照片(隨便一張都可以)

嵌入訊息 Embed

這種訊息是怎麼傳出來的

@bot.command()
async def embed(ctx):
  embed=discord.Embed(title="標題", description="敘述", color=0x0084ff)
  embed.add_field(name="ouo", value="1", inline=False)
  embed.add_field(name="awa", value="2", inline=False)
  embed.add_field(name="uwu", value="3", inline=False)
  await ctx.send(embed=embed)

成功的話會長這樣

自己刻embed很累

實作 - 4

輸入!embed 標題 敘述 名字 數字 生成一個embed如下

傳送表情符號

<:emoji_name:emoji_id>

要怎麼知道emoji_id

方法 - 1

  • 開啟開發者模式
  • 右鍵複製id

開啟開發者模式

在emoji上右鍵並點copy ID

@bot.command()
async def emoji(ctx):
  await ctx.send("<:angrymention:918163460502728724>")

成功的話會長這樣

mention

<@!user_id>

要怎麼知道一個人的user_id

頭像右鍵點copy ID

@bot.command()
async def mention(ctx):
  await ctx.send("<@!742373231025193000>")

成功的話會長這樣

reply使用指令的人

@bot.command()
async def hello(ctx):
    await ctx.reply("hi")

成功的話會長這樣

ctx.send()    v.s.  ctx.reply()

很多個人一起打指令時會混淆 不知道機器人在回覆誰

很多人一起打指令可以很清楚的知道機器人在回覆誰

隨機發出一則訊息

如果我想要在輸入!hello時

隨機回傳下面一個訊息

  • hello
  • 早安
  • hi
import random


@bot.command()
async def hello(ctx):
  c = random.randint(0, 2)
  if c == 0:
    await ctx.send("hi")
  elif c == 1:
    await ctx.send("早安")
  elif c == 2:
    await ctx.send("hello")

成功的話會長這樣

讓機器人保持上線

from flask import Flask
from threading import Thread

app = Flask('')

@app.route('/')
def home():
    return "Hello. I am alive!"

def run():
  app.run(host='0.0.0.0',port=8080)

def keep_alive():
    t = Thread(target=run)
    t.start()
import os
import discord
from discord.ext import commands
from keep_alive import keep_alive

bot = commands.Bot(command_prefix="!")


keep_alive()
token = os.environ['TOKEN']
bot.run(token)

複製連結

設定 uptime robot

撰寫你的第一個

機器人project

希望機器人有的功能

  • 傳送 !hello 會 回覆 隨機訊息
  • 傳送 !emoji 會 回覆 隨機表情符號
  • 傳送 !pic 會 回覆 隨機圖片
  • 傳送 !rand 會 回覆 隨機對或是錯
  • 傳送 !add 數字 數字 會 回覆 兩個數字相加
  • 可以加更多你想要的指令

傳送!hello會回覆隨機訊息

傳送!emoji會回覆隨機表情符號

傳送!pic會回覆隨機圖片

傳送!rand會隨機回覆yes或是no

傳送!add 數字 數字會回覆兩個數字相加

開發 discord bot

step 1  python 基礎語法

  • discord.py 是建立在 python 語法上開法,因此在開發discord bot 前要先學會 python 的基礎語法
  • 網路上都有很多基礎 python 語法的教學,可以多上網找資料

step 2 了解自己的目的

  • 先知道想要機器人幫你做什麼,而不是盲目的把所有discord.py 用法背下來

step 3 找自己想要的功能要怎麼用

  • 可以到 discord.py API reference 找對應的寫法
  • 或是到網路上搜尋你想要讓機器人做的事情的關鍵字,因為你想要問的東西可能在幾年前就有人在網路上問過了

step 4 整理 code

  • 非常重要 方便日後想要更改code時可以比較好理解你之前在寫什麼

講師之前做過的

project 分享

線上機率學習v2.0

  • 集合各種小遊戲 小功能的機器人

friedshrimp_

  • 罐頭訊息回覆
  • 抽獎功能

verify bot

  • 群組的驗證學校機器人

music bot

  • 之前放在群組中的音樂機器人
  • 音質太差所以被其他機器人取代了

偷宣傳

infor34th

  • 社團的捐獻排行榜

課程結束後...

不清楚可以做什麼應用

  • 上網看看別人的機器人有什麼功能
  • 不一定要做非常有用的機器人,即使是一個罐頭訊息回覆機器人也可以讓你跟朋友的聊天變得更有趣

還想學習更多 discord bot

在寫機器人遇到疑問

  • discord.py API reference 看清楚語法
  • 你有的疑問其他人有很高機率也遇過 可以上
    stack overflow 找找看有沒有一樣或相似的問題
  • 私訊講師 (建議用 discord)

discord-bot-tutorial

By Howie Chen

discord-bot-tutorial

  • 248