SiriusKoan
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- type: slide tags: Sprout --- # Discord Bot siriuskoan --- ## Discord Bot 可以怎麼玩 (credit to Sean) - 身份組機器人 - 音樂機器人 - 歡迎訊息 - 活躍度排名、小遊戲 - 搜尋、訂閱 ---- ### 身份組 Bot ![](https://i.imgur.com/dYipNNv.png) ---- ### 音樂 Bot ![](https://i.imgur.com/JsmoWgy.png) ---- ### 歡迎訊息 ![](https://i.imgur.com/03GQgBb.png) ---- ### 活躍度排名 ![](https://i.imgur.com/aRFpvEF.png) ---- ### 小遊戲 ![](https://i.imgur.com/xv5WCDP.png) ---- ### 訂閱 ![](https://i.imgur.com/zN1QIh7.png) ---- ### 搜尋貓貓 ![](https://i.imgur.com/6ZwE2W5.png) ---- ### 團購訂餐 ![](https://i.imgur.com/XGDEcm0.png) --- ## 新增機器人到自己的伺服器 1. 建立 application & bot 2. 建立伺服器 3. 把 bot 邀請進去 步驟截圖請見[去年簡報](https://hackmd.io/@Sean64/dc-bot#/2) --- ## 安裝 discord.py ``` $ python3 -m pip install discord.py ``` --- ## 建立 Discord Bot ```python= import discord intents = discord.Intents.default() intents.message_content = True client = discord.Client(intents=intents) @client.event async def on_ready(): print(f"We have logged in as {client.user}") @client.event async def on_message(message): if message.author == client.user: return if message.content.startswith("$hello"): await message.channel.send(f"hello, {message.author}") client.run("TOKEN") ``` ---- ### 建立指令 ```python= import discord from discord.ext import commands intents = discord.Intents.default() intents.message_content = True client = discord.Client(intents=intents) bot = commands.Bot(command_prefix="!", intents=intents) @bot.command() async def ping(ctx): await ctx.send("pong") bot.run("TOKEN") ``` --- ## 補充 - Decorator ```python= @bot.command() async def ping(ctx): await ctx.send("pong") ``` ---- ### 補充 - Decorator - Python 的語法糖 - 把你宣告的函式包進一個已經寫好的函式裡 ---- ### 補充 - Decorator ```python= import time from functools import wraps def timeit(func): @wraps(func) def wrapper(*args, **kwargs): start = time.time() res = func(*args, **kwargs) end = time.time() print('Time spent in %s is %f seconds.' % (func.__name__, end - start)) return res return wrapper def fib(n): if n <= 2: return 1 return fib(n - 1) + fib(n - 2) @timeit def run(): print(fib(40)) if __name__ == '__main__': run() ``` ---- ### 補充 - Decorator [更多範例](https://towardsdatascience.com/10-fabulous-python-decorators-ab674a732871) --- ## 補充 - async - 遇到耗時間但你不用做事的事情的時候,可以把 CPU 拿去做其他事情以避免浪費 - e.g., 網路傳輸、資料庫讀寫、... ---- ### 補充 - async ```python= import time import asyncio async def do_something2(i): print(f"第 {i} 次開始") await asyncio.sleep(2) print(f"第 {i} 次結束") if __name__ == "__main__": start = time.time() loop = asyncio.get_event_loop() tasks = [loop.create_task(do_something2(i + 1)) for i in range(5)] loop.run_until_complete(asyncio.wait(tasks)) loop.close() print(f"time: {time.time() - start} (s)") ``` --- ## 補充 - Token - Token 很重要 - 不能寫在檔案裡面讓大家看光光 - 要寫到其他地方 ---- ### 補充 - Token - 可以把 token 放到環境變數裡面 ``` # windows cmd $ set token=TOKEN # mac $ export token=TOKEN ``` ---- ### 補充 - Token - 然後在 python 裡面讀出來 - 給 discord bot 去跑 ```python= import os TOKEN = os.getenv("token") ... client.run(TOKEN) ``` --- ## 更多功能 - [Discord Bot Template](https://github.com/kkrypt0nn/Python-Discord-Bot-Template) - Google

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully