10
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Discord Bot】発言したユーザのユーザIDを取得する方法【Python】

Last updated at Posted at 2019-02-15

特定のユーザのときだけ別の処理をしたいというときに。


@client.event
async def on_message(message):
    print (message.author.id)

おそらく上のような形でmessageオブジェクトを受け取っていると思うので、

message.author.id

でそのメッセージは発言したユーザのIDを取得することができます。

特定のユーザに意地悪したいときとかに使えますね。

最後に

message.author

ではユーザ名が取得できるのですが、これだとその人が名前変えたときとかに動かなくなるという問題があって、同じ悩みを抱えた人もいるんじゃないかと思って書いてみました。

ちなみにdiscordアプリからは普通にそのユーザを右クリックすることでIDを拾えます。(画像の一番下)

スクリーンショット 2019-02-15 16.43.06.png

もしかしたら権限とかで表示されない可能性もあります。(デベロッパモードじゃないと見れないとか)

10
9
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?