LoginSignup
0
0

More than 1 year has passed since last update.

discord、どう動いてる?

Posted at

動機

普段使用しているpycordはどのようにして動いているのか気になったので。

仕組み

ベース:
APIのバージョンは8以上が推奨されています。参考

下準備

from pprint import pprint

API_VERSION = 9 
BASE_URL = "https://discord.com/api/v{}".format(API_VERSION)

TOKEN = "YOUR_BOT_TOKEN"
HEADER = {"Authorization": "Bot {}".format(TOKEN)}

APP_ID = 000000000000000000 # BotのID
GUILD_ID = 111111111111111111 # 対象とするサーバーのID
CHANNEL_ID = 222222222222222222 # 対象とするチャンネルのID

メッセージ送信

MESSAGE_SEND = "/channels/{}/messages"

url = BASE_URL + MESSAGE_SEND.format(CHANNEL_ID)

json = {"content": "aaaaa"}

r = requests.post(url=url, json=json, headers=HEADER)
print(r)
pprint(r.json())

###### 出力 ######
<Response [200]>
{'attachments': [],
 'author': {'avatar': None,
            'avatar_decoration': None,
            'bot': True,
            'discriminator': '3837',
            'id': 'BotID',
            'public_flags': 0,
            'username': 'BotName'},
 'channel_id': '222222222222222222',
 'components': [],
 'content': 'aaaaa',
 'edited_timestamp': None,
 'embeds': [],
 'flags': 0,
 'id': 'MessageID',
 'mention_everyone': False,
 'mention_roles': [],
 'mentions': [],
 'pinned': False,
 'referenced_message': None,
 'timestamp': '2022-04-28T10:33:50.150000+00:00', # datetime.datetime
 'tts': False,
 'type': 0}

メッセージ編集

MESSAGE_EDIT = "/channels/{}/messages/{}"
MESSAGE_ID = 333333333333333333

url = BASE_URL + MESSAGE_EDIT.format(CHANNEL_ID, MESSAGE_ID)
json = {"content": "message was edited!"}
r = requests.patch(url=url, json=json, headers=HEADER)
print(r)
pprint(r.json())

#### 出力 ####
<Response [200]>
{'attachments': [],
 'author': {'avatar': None,
            'avatar_decoration': None,
            'bot': True,
            'discriminator': '3837',
            'id': 'BotID',
            'public_flags': 0,
            'username': 'BotName'},
 'channel_id': 'ChannelID',
 'components': [],
 'content': 'message was edited!',
 'edited_timestamp': '2022-04-28T11:17:59.394637+00:00',
 'embeds': [],
 'flags': 0,
 'id': 'MessageID',
 'mention_everyone': False,
 'mention_roles': [],
 'mentions': [],
 'pinned': False,
 'timestamp': '2022-04-28T10:14:17.819000+00:00',
 'tts': False,
 'type': 0}

メッセージ削除

MESSAGE_DEL = "/channels/{}/messages/{}"
MESSAGE_ID = 333333333333333333

url = BASE_URL + MESSAGE_DEL.format(CHANNEL_ID, MESSAGE_ID)
r = requests.delete(url=url, headers=HEADER)
print(r)

#### 出力 ####
<Response [204]>

メッセージ削除 (複数)

MESSAGE_BULK = "/channels/{}/messages/bulk-delete"
MESSAGE_LIST = [444444444444444444, 555555555555555555, ...] # 2~100件まで

url = BASE_URL + MESSAGE_BULK.format(CHANNEL_ID)
json = {"messages": MESSAGE_LIST}
r = requests.post(url=url, json=json, headers=HEADER)
print(r)

#### 出力 ####
<Response [204]>

次はアプリケーションコマンドについて書こうかなと思います。(最近忙しいので時間がかかります。)

指摘ありましたらお願いします。

0
0
0

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
0
0