資訊之芽 2022 Python 語法班
Author: Sean 韋詠祥
Note:
日期:2022-06-12
課程時間:14:00 - 15:20
discord.py
pip3 install discord.py
Discord.py
QuickStartNote:
請各位自己搜尋,這裡刻意不提供超連結
discord.errors.HTTPException: 401 Unauthorized
Traceback (most recent call last):
File "bot.py", line 17, in <module>
client.run('your token here')
File "site-packages/discord/client.py"
await self.login(*args, bot=bot)
discord.errors.LoginFailure: Improper token has been passed.
import discord
client = discord.Client()
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!ping'):
await message.channel.send('Nice to meet you!')
client.run('OTgy....xzfU')
Note:
https://discordpy.readthedocs.io/en/stable/quickstart.html
name = 'Sean'
time = '14:30'
# 基本用法
print('Hey ' + name + ', it\'s ' + time + ' now.')
# 格式化文字
print(f'Hey {name}, it\'s {time} now.')
# 用 'string'.format(...) 處理格式化
print('Hey {}, it\'s {} now.'.format(name, time))
# 如果想指定變數順序呢?
print('{1}: Hey {0}, how are you?'.format(name, time))
# 讓我們再看一次前面的例子
print('We have logged in as {0.user}'.format(client))
pip3 install python-dotenv
from os import getenv
from dotenv import load_dotenv
load_dotenv()
TOKEN = getenv('TOKEN')
import discord
from os import getenv
from dotenv import load_dotenv
# Load TOKEN from .env file
load_dotenv()
TOKEN = getenv('TOKEN')
# Initial our bot client
client = discord.Client()
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
@client.event
async def on_message(message):
if message.author == client.user:
return # Ignore messages from myself, prevent loop
# In this example, we use ! as command prefix
if message.content.startswith('!ping'):
await message.channel.send('Hey there!')
# Must after all functions
client.run(TOKEN)
if message.content.startswith('$play'):
await client.change_presence(
status=discord.Status.dnd,
activity=discord.Game(name="資訊之芽"))
await message.channel.send('I like playing!')
if message.content.startswith('!watch'):
await client.change_presence(
status=discord.Status.idle,
activity=discord.Activity(
type=discord.ActivityType.watching,
name="Apple 發表會"))
請參考 ActivityType
投影片連結:https://hackmd.io/@Sean64/dc-bot
下一堂課內容:
or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
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.
Do you want to remove this version name and description?
Syncing