間違っているとこもあるかもしれません。
nextcord-API Introduction
ライブラリはPyPIから直接入手できます。
python3 -m pip install -U nextcord
Windowsを使用している場合は、代わりに以下を使用する必要があります
py -3 -m pip install -U nextcord
音声を利用するには、nextcordの代わりにnextcord [voice]を使用する必要があります。
python3 -m pip install -U nextcord[voice]
最新版を入手する場合
pip install --user -U git+https://github.com/nextcord/nextcord
単純な例:
import nextcord
client = nextcord.Client()
@client.event
async def on_message(message):
print(f'Message from {messsage.author}: {message.content}')
client.run('token')
tokenはここから入手できます。
Clients
Client
class: nextcord.Client(*, loop=None, **options)
パラメータについては省略
# 準備
import nextcord
client = nextcord.Client()
@client.event
async def on_message(message):
print('msg')
client.run('token')
@ event
イベントを登録するデコレータ
イベントは コルーチン でなければいけません。違う場合はTypeError
が発生します。
ex):
@client.event
async def on_ready():
print('Ready!')
例外:
TypeError
user
クライアント自身。ログインしていない場合、None
の値を持つ。
ex):
# idを代入
client_user = client.user
Type:
Optional[ClientUser]
property: guilds
クライアントの所属するギルド
ex):
# ギルドの所属するリスト
guild_list = list(client.guilds)
# ギルドの数を数える
guild_count = len(client.guilds)
Type:
List[Guild]
property: emojis
クライアントの持つ絵文字
Type:
List[Emoji]
run(*args, **kwargs)
ex):
token='xxxxxxxxxxxxxxxxxxxxxxxx.xxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxx'
client.run('token')
property users
クライアントが認識することができる(そのbotの)ユーザー全部
ex):
bot_users = list(client.users)
Type:
List[User]
get_channel(id, /)
id:与えられたチャンネルID
IDからチャンネルを発見します。
channel_id = xxxxxxxxxxxxxxxxxx
channel = client.get_channel(user_id)
await message.channel.send(str(channel))
Parameters
id (int) – The ID to search for.
戻り値
The returned channel or None if not found.
Return type:
Optional[Union[abc.GuildChannel, Thread, abc.PrivateChannel]]
get_guild(id, /)
id:与えられたギルドID
IDからギルドを発見します。
guild_id = xxxxxxxxxxxxxxxxxx
guild = client.get_guild(user_id)
await message.channel.send(str(guild))
例外
id (int) – 検索する ID
戻り値
ギルド。 または見つからない場合: None
Return type
Optional[Guild]
get_user(id, /)
Intentをオンにしなければ使えません。オンにする方法はこちら。
ユーザー名を取得します。
intents = discord.Intents.all()
client = nextcord.Client(intents=intents)
user_id = xxxxxxxxxxxxxxxxxx
user = client.get_user(user_id)
await message.channel.send(str(user))
例外:
id (int) – The ID to search for.
戻り値:
The user or None if not found.
Return type:
Optional[User]
get_all_channels()
クライアントの所属するギルドのうち、アクセス可能な全てのチャンネルを返します。
channelid = xxxxxxxxxxxxxxxxxx
for channel in bot.get_all_channels():
if channel.id == channelid:
await message.channel.send("Founded!!")
また、これは
for guild in client.guilds:
for channel in guild.channels:
yield channel
と同義です。
get_all_members()
クライアントの所属するギルドのうち、見つけられる全てのユーザーを返します。
また、これは
for guild in client.guilds:
for member in guild.members:
yield member
と同義です。
wait_for(event, *, check=None, timeout=None)
これは、ユーザーがメッセージに返信するのを待つ、メッセージに反応する、または自己完結型の方法でメッセージを編集するために使用できます。
if message.content.startswith('$greet'):
channel = message.channel
await channel.send('Say hello!')
def check(m):
return m.content == 'hello' and m.channel == channel
msg = await client.wait_for('message', check=check)
await channel.send(f'Hello {msg.author}!')
又は、
if message.content.startswith('$thumb'):
channel = message.channel
await channel.send('Send me that 👍 reaction, mate')
def check(reaction, user):
return user == message.author and str(reaction.emoji) == '👍'
try:
reaction, user = await client.wait_for('reaction_add', timeout=60.0, check=check)
except asyncio.TimeoutError:
await channel.send('👎')
else:
await channel.send('👍')
Parameters
event (str)
– イベント名, event referenceに似ていますが、 イベント参照に似ていますが、on_
はありません。それぞれのイベントからon_
を外して代入します。
これを使う場合、try~exceptが必要となります。
check (Optional[Callable[…, bool]])
– 何を待つかを確認するための語。引数は、待機中のイベントのパラメーターを満たす必要があります。
timeout (Optional[float])
– asyncio.TimeoutErrorが発生するまで待機する秒数。
例外:
asyncio.TimeoutError –timeout
に達した場合。
戻り値:
引数なし、またはイベント参照で渡されたパラメーターを反映するタプルを返します
Return type:
特になし
await: change_presence(*, activity=None, status=None)
〇〇をプレイ中
の時などに使います。
game = nextcord.Game("with the API")
await client.change_presence(status=nextcord.Status.idle, activity=game)
Parameters
- activity (Optional[BaseActivity]) – 行われているアクティビティ。アクティビティが実行されていない場合はなし。
- status (Optional[Status]) – 変更するステータス(状態)を示します。 Noneの場合、
Status.online
が使用されます。
(ほかにもStatus.do_not_disturb
等があります。)
例外
InvalidArgument – activity パラメータが適切でない場合。
await: create_guild(*, name, region=, icon=..., code=...)
新しいギルドを作るときに使います。
クライアントのみが入っており、メッセージ送信者は参加していません。
await client.create_guild(name='name')
Parameters
- name (str) – ギルドの名前
- region (VoiceRegion) – The region for the voice communication server. Defaults to VoiceRegion.us_west.
- icon (Optional[bytes]) – The bytes-like object representing the icon. See ClientUser.edit() for more details on what is expected.
- code (str) – The code for a template to create the guild with.
Raises
HTTPException – Guild creation failed.
InvalidArgument – Invalid icon image format given. Must be PNG or JPG.
Returns
The guild created. This is not the same guild that is added to cache.
Return type
Guild
await: create_dm(user)
こちらはあまり使いません。
await message.author.send("message")
がよく使われています。
await message.author.create_dm(user)
AutoShardedClient
class: nextcord.AutoShardedClient(*args, loop=None, **kwargs)
await: change_presence(*, activity=None, status=None, shard_id=None)
こちらとあまり変わりません。
game = nextcord.Game("with the API")
await client.change_presence(status=nextcord.Status.idle, activity=game)
とりあえず nextcord.Client
についての紹介。
間違っていたら修正お願いします。