Skip to content

Instantly share code, notes, and snippets.

@CyberRex0
Last active October 8, 2024 06:43
Show Gist options
  • Select an option

  • Save CyberRex0/51a70b07322bd99ac35d5d3b961d8fe2 to your computer and use it in GitHub Desktop.

Select an option

Save CyberRex0/51a70b07322bd99ac35d5d3b961d8fe2 to your computer and use it in GitHub Desktop.
Discord.pyにおいて、ライブラリを経由しないで返信をする方法

ライブラリを経由しないで返信を使う

async def reply(basemsg, message, reply):
   heads = {
    "Content-Type":"application/json",
    "Authorization":f"Bot {TOKEN}"
   }
   data = {
    "content":message,
    "message_reference": {
     "message_id":basemsg.id
    },
    "allowed_mentions": {
     "replied_user":reply
    }
   }
   r = requests.post(f'https://discord.com/api/channels/{basemsg.channel.id}/messages', data=json.dumps(data), headers=heads)
   if r.status_code!=200:
      raise Exception(f'Gateway returned an error: \n{r.text}')
      return False
   return True

message_referenceの中で、message_idに返信先のメッセージIDを指定することで返信可能

@メンションを切るには、allowed_mentionsの中にreplied_user: Falseを明示する

(デフォルトではOFFとリファレンスには書いてあったが、どうやらONになってるらしいので、こうする必要がある)

この関数の例では await reply(message, 'Hello', False) とすると、受信したメッセージに@メンションOFF(False)で「Hello」と言う

参考にしたドキュメント

Discord公式APIドキュメンテーション

https://discord.com/developers/docs/resources/channel#create-message

@grarich

grarich commented Nov 21, 2020

Copy link
Copy Markdown

コルーチン内でrequests を使うのであれば普通の関数にしてもいいと思うし、ボット内で使うのなら aiohttp を使うといいと思う。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment