LoginSignup
2
3

More than 3 years have passed since last update.

Discord Botがエラーを出したときにWebhookで自動通知する。

Last updated at Posted at 2019-05-18

目標

ボットがエラー出したときにWebhookでエラーを通知してもらう。

環境

・Python3.7
・discord.py rewrite
・discord-webhook 0.4.1

インストール等

することが多いので略します。
★pipでインストールできます。

さっそくソースコード

2つファイルが必要です。

webhookerror.py

from discord_webhook import DiscordWebhook, DiscordEmbed
from discord_webhook import DiscordWebhook, DiscordEmbed

def error_log(message):
    webhook = DiscordWebhook(url='WebhookのURL',username="Webhookの名前")
    data=":exclamation: " + message+":exclamation:"
    embed = DiscordEmbed(title='エラー', description=data, color=0xff0000)
    webhook.add_embed(embed)
    webhook.execute()
main.py
from discord.ext import commands
import discord
from discord.ext import commands as c
bot = commands.Bot(command_prefix='!', activity=d.Activity(name='ボットのエラーをWebhookで流します!', type=d.ActivityType.watching))
import webhookerror

#botのおかしなエラーをすべて取得。
@bot.event
async def on_command_error(ctx,error):
    webhookerror.error_log("エラーが発生しました:" +str(error)+"\nServername:"+str(ctx.guild.name)+"\nName:"+str(ctx.author))

#流れるかテストするためのコマンド
@bot.command()
async def test(ctx):
    prin()

実行

実行してみましょう!
Webhookからメッセージが送信されたら成功です!

最後に

今回はエラーをWebhookにエラー文を流せるようにしてみました。

そして、
discord.py rewrite 正式リリースおめでとうございます! :tada:

追記:なんでdiscord.pyのWebhookのやつ使わなかったかっていうと使い方がわかんなかったから。

2
3
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
2
3