DiscordBot Python

discord.py 特定のユーザーの処理を変える方法

discord.pyであの人にはこの処理をしたいと思う時ありますよね。

ユーザーIDを識別することにより簡単に条件分岐が可能です。

↓ソースコード Python3.9.10

import discord

client = discord.Client()

@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('Hello!')

    if message.author.id == 2343242365425223534543:
#特別な処理
        await message.channel.send("あほ~")

client.run('TOKEN')

ユーザーIDの情報は message.author.id で取得できます。

その情報を特定のユーザーのIDと比較して一致したら処理を走らせます。



    if message.author.id == 2343242365425223534543: <----- ここの数字をユーザーIDに変更してください
#特別な処理
        await message.channel.send("あほ~")

ユーザーIDの取得方法

設定 -> 詳細設定 -> 開発者モードを ON にします。

ユーザーを右クリックして IDをコピー します。

-DiscordBot, Python