実現したいこと
・discordのbotが、最後にメッセージを送信してから30分経過したらメッセージを送信してタイムアウトするようにしたい
・新たにbotがメッセージを送信すると30分のカウントはリセットされるようにしたい
前提
・botは、ユーザーがbotをメンションすることでそれ以降の処理を実行するようにしました。
・botの作成にはdiscord.pyを使用し、タイムアウトさせるためのコードはmain.pyとは別に作成したtimeout.pyに記述しました。コードが一通りかけたため実行すると、コンパイルは通ったのですがdiscordの方からbotをメンションして起動させようとしたところエラーが起きました。
発生している問題・エラーメッセージ
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
該当のソースコード
python
1#main.py 2import discord 3import teaming 4import keep_alive 5import check_bot 6import timeout 7from datetime import datetime 8is_running = False 9 10#discordのbotにアクセスするためのトークン 11TOKEN = 'トークン' 12 13#botにすべての権限を与える 14intents = discord.Intents.all() 15 16#botがメッセージを受け取るか受け取らないか選択できるようにする。 17client = discord.Client(intents = intents) 18 19#botがアクティブかそうでないかを定義する。デフォルトは非アクティブ。 20is_running = False 21 22#起動時に実行されるイベント 23@client.event 24 25#起動時にコンソールにhelloと表示する 26async def on_ready(): 27 print("hello") 28 29#メッセージが送信されたときに起動するイベント 30@client.event 31async def on_message(message): 32 message = message 33 34 #関数内でis_runningを使用する 35 global is_running 36 global sendTime 37 38 #30分以上botが何も送信しなかったらタイムアウト 39 timeout.timeout(message) 40 41 #@everyoneとbotからのメッセージには反応しない 42 check_bot.checkBot(message) 43 44 #メッセージが送信されたチャンネルを取得し、変数に格納する 45 channel = message.channel 46 47 #メッセージの送信元がbotの場合、何も実行しない 48 if message.author.bot: 49 return 50 51 #メンションでbotをアクティブにしてメッセージを送信 52 if client.user.mentioned_in(message): 53 if is_running == False: 54 is_running = True 55 await message.channel.send("おはよー...なにするー...?") 56 #メッセージを送信した時間を取得
python
1#timeout.py 2import asyncio 3from datetime import datetime, timedelta 4 5async def timeout(message): 6 if message == message: 7 await asyncio.sleep(1800) 8 message.channel.send('たったままねてたー...すわってねるねー...') 9 is_running = False 10 return is_running 11pass