iT邦幫忙

2024 iThome 鐵人賽

DAY 8
0
Python

用 Python 打造你的 Discord BOT系列 第 8

[Day 08] 觸發條件 (三):指令 ── message command 與 user command

  • 分享至 

  • xImage
  •  

今天接著繼續介紹指令:訊息指令與用戶指令。

進度

訊息指令與用戶指令的概念差不多,就決定合併一起介紹了。

什麼是訊息指令 (message command)?

訊息指令,顧名思義,就是作用在訊息的指令。使用方式很簡單,只要在訊息上面按滑鼠右鍵,就會看到「應用程式」的選單,再選擇要使用的指令即可。(沒有預設指令)

什麼是用戶指令 (user command)?

同理,用戶指令就是作用在用戶上的指令,一樣是對著用戶按下滑鼠右鍵,再進入「應用程式」選單選擇要使用的指令。(同樣沒有預設指令)

對自己:

對其他伺服器成員:

第一個訊息指令與用戶指令

這兩種指令的撰寫方式非常相似,就一起介紹了。(範例程式簡化自 Github 上的範例)

# day08.py

import discord
from discord import app_commands

GUILD_ID = "your guild ID"
MY_GUILD = discord.Object(id=GUILD_ID)


class MyClient(discord.Client):
    def __init__(self, *, intents: discord.Intents):
        super().__init__(intents=intents)
        self.tree = app_commands.CommandTree(self)

    async def setup_hook(self):
        self.tree.copy_global_to(guild=MY_GUILD)
        await self.tree.sync(guild=MY_GUILD)


intents = discord.Intents.default()
client = MyClient(intents=intents)


@client.event
async def on_ready():
    print(f'Logged in as {client.user} (ID: {client.user.id})')

@client.tree.context_menu(name="Show Join Date")
async def show_join_date(interaction: discord.Interaction, member: discord.Member):
    # The format_dt function formats the date time into a human readable representation in the official client
    await interaction.response.send_message(
        f"{member} joined at {discord.utils.format_dt(member.joined_at)}"
    )

@client.tree.context_menu(name="Report to Moderators")
async def report_message(interaction: discord.Interaction, message: discord.Message):
    await interaction.response.send_message(
        f"Thanks for reporting this message by {message.author.mention} to our moderators.",
    )

client.run('token')

與之前的 Quickstart (Day 04) 一樣,可以大致上把這段程式分成三個階段:

  1. 建立 Client
  2. 設定 Client
  3. 啟用 Client

今天的重點在第二階段的兩個函數:show_join_datereport_message。其中,show_join_date 是用戶指令,而 report_message 是訊息指令。

這兩個函數的寫法非常接近,有以下幾點注意事項:

  1. 都是使用 context_menu。其中,name不是必要參數,但如果不填的話就會直接使用函數名作為訊息指令或是用戶指令的名稱。
  2. 函數需要有「剛好」兩個參數,第一個一定是 interaction
  3. 函數的第二個參數一定要加上 type hint (用來指定是訊息指令或是用戶指令)。

    type hint 只能是以下這幾個其中一個:discord.Message, discord.User, discord.Member, 或是 「discord.Member | discord.User

測試

訊息指令

啟動後,就可以看到原本空的選單已經有剛剛建立的訊息指令了

執行結果:

用戶指令

用戶指令的選單也有更新

執行結果:

小結

今天把剩下的兩個指令:訊息指令 (message command) 和用戶指令 (user command) 都介紹完了,明天會介紹指令的其他細節,並補上之前 slash command 沒有介紹完的內容。

明後兩天都在高雄參加 PyCon,好期待啊!
/images/emoticon/emoticon37.gif


上一篇
[Day 07] 觸發條件 (二):指令 ── slash command
下一篇
[Day 09] 應用指令的參數與說明
系列文
用 Python 打造你的 Discord BOT14
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言