---
# System prepended metadata

title: DCbot week1
tags: [教學簡報]

---

---
title: DCbot week1
tags: 教學簡報
---

# Dc bot基礎功能


---

### 還記得Quick Start嗎?

----

```python
import discord #導入disocrd相關語法
from discord.ext import commands #導入commands語法
intents = discord.Intents.all() #開啟機器人所有功能

bot = commands.Bot(command_prefix='!',intents=intents)#指定"bot"為機器人本身並且設定接收指令的符號，並確認開啟功能

@bot.event#遭遇事件
async def on_ready():#當機器人準備好的時候
    print(f'We have logged in as {bot.user}')#發送以上訊息

@bot.event#遭遇事件
async def on_message(message):#當機器人接收到訊息
    if message.author == bot.user:#確認發送訊息者是否為機器人本身
        return#如果是的話就return不發送訊息

    if message.content.startswith('$hello'):#如果訊息為$hello
        await message.channel.send('Hello!')#發送Hello!

bot.run('你的token')#code與機器人連接的橋樑
```

---

# 成員加入、離開偵測

----

### 如何知道怎麼做?

----

## 首先

----

### 想到加入，就有機會自然而然聯想到join
### 接著利用Ctrl + F 開啟搜尋功能
<img src="https://cdn.discordapp.com/attachments/678292614398869513/1075779755560931389/image.png" >

----
## 看到英文不要怕，讀他！(用翻譯也OK，嘗試理解)
<img src="https://media.discordapp.net/attachments/678292614398869513/1075780356206252142/image.png" >

----

## 成員加入
```python
@bot.event#遇到事件
async def on_member_join(member):#當有成員加入
    channel = bot.get_channel(頻道ID)#抓取發送訊息的頻道
    await channel.send(f'歡迎{member}的到來')#發送訊息
```

----

# 成員離開
```python
@bot.event#遇到事件
async def on_member_remove(member):#當有成員離開
    channel = bot.get_channel(頻道ID)#抓取發送訊息的頻道
    await channel.send(f'{member}，我們緬懷他')#發送訊息
```


---

# 機器人狀態

----

# 關鍵字"status"

----

## 結構
```python

@bot.event
async def on_message(message):
    if message.content.startswith('afk'): #如果接受到afk的訊息
        await bot.change_presence(status=discord.Status.idle, activity=None)#變成閒置狀態
    elif message.content.startswith('vinish'):#如果接受到vinish的訊息
        await bot.change_presence(status=discord.Status.invisible, activity=None)#變成線下狀態
    elif message.content.startswith('on'):#如果接受到on的訊息
        await bot.change_presence(status=discord.Status.online, activity=None)#變成線上狀態
    await bot.process_commands(message) #避免與commands衝突
```



# 對話其他應用

---

```python

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

    def check(m1):
        return m1.content == "hello"

    msg = await bot.wait_for("message", check=check)
    m2 = await ctx.send(f"Hello {msg.author}!")
    await asyncio.sleep(2)
    await m2.delete() 
```

---

# 刪除訊息(關鍵字purge)

----

```python
@bot.command()
async def ddle(ctx):
    def is_me(m):
        return True
    channel = ctx.channel
    await channel.send("How many")
    msg = await bot.wait_for('message', check=is_me)
    x = int(msg.content)
    deleted = await channel.purge(limit=x, check=is_me)
    await channel.send(f'Deleted {len(deleted)} message(s)') 
```

---

## 最後來做點一應用

----
```python
@bot.command()
async def tag(ctx,member:discord.Member):
        id = member.id
        channel = ctx.channel
        await channel.send("How many time")
        def check(m):
            return True

        msg = await bot.wait_for('message', check=check)
        y = int(msg.content)
        for x in range(y):
            await channel.send("<@" + str(id) + ">")
```

