Python DC bot

講師:歪伊恩、777

目錄

  • python及IDE
  • 基本建置
  • 簡單指令
  • 模組安裝

python下載

我是用3.9.10寫,新版不確定模組更新狀況

python下載

拜託記得打勾!

拜託記得打勾!

拜託記得打勾!

不要找不到才來問我在哪...

IDE

反正我是用這個

聽說有人會用

或者你有更好的,反正選個喜歡的就好

模組

開啟cmd

輸入這串:

好欸可以開始寫了

pip install discord.py

還是稍微檢查一下

在cmd中輸入:

有看到這個就代表有了喔~

pip list

小小補充一下

可以把東西丟到github比較好管理

創建一隻bot

設定的話先不要動

然後記得複製token

通常在這裡

邀請進伺服器

URL

-->bot打勾

-->最下面有連結

-->邀請進伺服器

基礎指令

import discord
from discord.ext import commands

intents= discord.Intents.all()

bot = commands.Bot(command_prefix="你要的觸發符號",intents = intents)


bot.run("你的TOKEN")

基礎指令-事件觸發

@bot.event
async def on_ready():
    print(">>bot is online<<")

@bot.event 
async def on_member_join(member):
    channel = bot.get_channel(頻道碼)
    await channel.send(f'{member}join!') 
    

@bot.event 
async def on_member_remove(member):
    channel = bot.get_channel(頻道碼)
    await channel.send(f'{member}leave!') 

基礎指令-條件觸發

@bot.command()
async def ping(ctx):
    await ctx.send(f'{round(bot.latency*1000)}(ms)')

其實偷用到了ctx不過我哪有差

最簡單的也是最基礎的ping

補充一下

一些好用ㄉ咚咚

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current Dir (external)",
            "type": "python",
            "request": "launch",
            "cwd":"${fileDirname}",
            "program": "${file}",
            "console": "externalTerminal",
            "env": {"PYTHONPATH": "${workspaceRoot}"},
        },
        {
            "name": "Python: Current Dir (internal)",
            "type": "python",
            "request": "launch",
            "cwd":"${fileDirname}",
            "program": "${file}",
            "console": "integratedTerminal",
            "env": {"PYTHONPATH": "${workspaceRoot}"},
         },
        {
             "name": "Python: Current File (Integrated Terminal)",
             "type": "python",
             "request": "launch",
             "program": "${file}",
             "console": "integratedTerminal",
        },
        {
            "name": "Python 模組",
            "type": "python",
            "request": "launch",
            "module": "enter-your-module-name",
            "justMyCode": true
        }
    ]
}

把launch.json裡面改成這個然後debug選

Python : Current Dir (external)

一些好用ㄉ咚咚

下節應該會教你們怎麼看吧

好欸我水完一堂課了

Made with Slides.com