「discord.ext」
「discord.ext.tasks」セクション
v1.1.0から新しく実装
Botのループ処理に対する最も一般的な手法の1つで、このモジュールは以下の用途に最適です。
- 処理キャンセルに関するエラーハンドリング
- インターネット接続が切れた場合の処理
- 繰り返し処理に間隔を持たせる
Py-cord extensionのゴールはあなたの悩みをすべて取り除くことです。
構築例
Cog内でのループ処理
Cogのメイン処理を記述しているクラスに記述することで繰り返し処理を簡単に構築できます。
簡単な例.py
from discord.ext import tasks, commands
class MyCog(commands.Cog):
def __init__(self):
self.index = 0
self.printer.start()
def cog_unload(self):
self.printer.cancel()
@tasks.loop(seconds=5.0)
async def printer(self):
print(self.index)
self.index += 1
再接続中のエラーハンドリング.py
import asyncpg
from discord.ext import tasks, commands
class MyCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.data = []
self.batch_update.add_exception_type(asyncpg.PostgresConnectionError)
self.batch_update.start()
def cog_unload(self):
self.batch_update.cancel()
@tasks.loop(minutes=5.0)
async def batch_update(self):
async with self.bot.pool.acquire() as con:
# batch update here...
pass
一定期間のループ処理.py
from discord.ext import tasks
@tasks.loop(seconds=5.0, count=5)
async def slow_count():
print(slow_count.current_loop)
@slow_count.after_loop
async def after_slow_count():
print('done!')
slow_count.start()
Bot起動前のループ処理.py
from discord.ext import tasks, commands
class MyCog(commands.Cog):
def __init__(self, bot):
self.index = 0
self.bot = bot
self.printer.start()
def cog_unload(self):
self.printer.cancel()
@tasks.loop(seconds=5.0)
async def printer(self):
print(self.index)
self.index += 1
@printer.before_loop
async def before_printer(self):
print('waiting...')
await self.bot.wait_until_ready()
キャンセル処理に挿入するループ処理.py
from discord.ext import tasks, commands
import asyncio
class MyCog(commands.Cog):
def __init__(self, bot):
self.bot= bot
self._batch = []
self.lock = asyncio.Lock()
self.bulker.start()
async def do_bulk(self):
# bulk insert data here
...
@tasks.loop(seconds=10.0)
async def bulker(self):
async with self.lock:
await self.do_bulk()
@bulker.after_loop
async def on_bulker_cancel(self):
if self.bulker.is_being_cancelled() and len(self._batch) != 0:
# if we're cancelled and we have some data left...
# let's insert it to our database
await self.do_bulk()
「discord.ext.tasks.Loop」クラス
- 引数
- coro:ループ処理後のコルーチン
- seconds:ループさせる秒数※
- hours:ループさせる時間数※
- minutes:ループさせる分数※
- time:ループさせる時間
- count: (ドキュメント内に明記なし)
- reconnect: (ドキュメント内に明記なし)
※引数timeが明示されている場合は無視される
「discord.ui」
UI構築用ライブラリ
Bot UI Kitページ
「discord.ui.InputText」セクション
InputTextクラスの引数
- style(discord.InputTextStyle)
- テキストスタイルの適用
- custom_id(任意[str])
- IDの割り当て
- label(str)
- ラベルの追加
- 45文字まで挿入可能
- placeholder(任意[str])
- 入力フォームの追加
- 100文字まで挿入可能
- min_length(任意[int])
- メッセージの長さの最小値を決定
- 既定値は0
- 4000が上限
- max_length(任意[int])
- メッセージの長さ上限
- 1以上4000以下の整数が入力可能
- ただし、min_lengthよりも大きい
- required(任意[bool])
- Trueで入力必須項目にする
- 既定値はTrue
- value(任意[str])
- 入力初期値
- row(1から5までの[int])
- 入力欄の行の位置(1~5)
「discord.ui.Modal」セクションの和訳
Modalクラスの引数
- children(discord.ui.InputText)
- Modalに表示されるメッセージ
- title(任意の文字列)
- Modalのタイトル
- custom_id(任意の文字列 ※省略可)
- ModalにつけることができるカスタムID
- timeout(Float型の数 ※省略可)
- 最後の操作から一定時間操作がなかった場合、Modal処理を中止させる
Modalクラスのメゾッド
- await callback(interaction)
- Modal送信後の処理を行う
- 引数「interaction」
- discord.Interaction
- add_item(item)
- Modalにテキスト入力欄を追加する
- 引数「item」
- discord.ui.InputText
- remove_item(item)
- add_itemで追加した入力欄を削除する
- 引数「item」
- dicord.ui.InputText
- stop()
- Modal操作の監視を終了する
- await wait()
- Modal操作の終了を待つ
- bool値を返す
- await on_error(error, interaction)
- エラーハンドラー
- await on_timeout()
- タイムアウト後の処理