前提・実現したいこと
Discordで稼働するBotを作成中
特定のmessage_idを持つ投稿へリアクションをすると
それに合わせたroleが付与される機能を実装させようとしています
発生している問題
@client.event配下のコードが含まれる場合 Botが起動しない状態です
該当のソースコード
py
1import discord 2from discord.ext import commands 3import os 4import traceback 5import time 6 7#本件以前から存在した関数 8bot = commands.Bot(command_prefix='/') 9token = os.environ['DISCORD_BOT_TOKEN'] 10 11#本件の契機に追加した関数 12client = discord.Client() 13 14#本件とは関係の無いerror処理 15@bot.event 16async def on_command_error(ctx, error): 17 orig_error = getattr(error, "original", error) 18 error_msg = ''.join(traceback.TracebackException.from_exception(orig_error).format()) 19 await ctx.send(error_msg) 20 21#実装すると起動しない部分(役職add) 22@client.event 23async def on_raw_reaction_add(payload): 24 message_id = payload.message_id 25 if message_id == 673725675131240458: 26 guild_id = payload.guild_id 27 guild = discord.utils.find(lamba g : g.id == guild_id, client.guilds) 28 if payload.emoji.name == 'lxmage': 29 role = discord.utils.get(guild.role, name='mage') 30 elif payload.emoji.name == 'lxfighter': 31 role = discord.utils.get(guild.role, name='fighter') 32 elif payload.emoji.name == 'lxmarksman': 33 role = discord.utils.get(guild.role, name='marksman') 34 elif payload.emoji.name == 'lxtank': 35 role = discord.utils.get(guild.role, name='tank') 36 elif payload.emoji.name == 'lxsupport': 37 role = discord.utils.get(guild.role, name='support') 38 elif payload.emoji.name == 'lxassassin': 39 role = discord.utils.get(guild.role, name='assassin') 40 else: 41 role = discord.utils.get(guild.role, name=payload.emoji.name) 42 if role is not None: 43 member = discord.utils.find(lamba m : m.id == payload.user_id, guild.members) 44 if member is not None: 45 await member.add_roles(role) 46 print("added") 47 else: 48 print("Role not found") 49 50#実装すると起動しない部分(役職remove) 51@client.event 52async def on_raw_reaction_remove(payload): 53 message_id = payload.message_id 54 if message_id == 673725675131240458: 55 guild_id = payload.guild_id 56 guild = discord.utils.find(lamba g : g.id == guild_id, client.guilds) 57 if payload.emoji.name == 'lxmage': 58 role = discord.utils.get(guild.role, name='mage') 59 elif payload.emoji.name == 'lxfighter': 60 role = discord.utils.get(guild.role, name='fighter') 61 elif payload.emoji.name == 'lxmarksman': 62 role = discord.utils.get(guild.role, name='marksman') 63 elif payload.emoji.name == 'lxtank': 64 role = discord.utils.get(guild.role, name='tank') 65 elif payload.emoji.name == 'lxsupport': 66 role = discord.utils.get(guild.role, name='support') 67 elif payload.emoji.name == 'lxassassin': 68 role = discord.utils.get(guild.role, name='assassin') 69 else: 70 role = discord.utils.get(guild.role, name=payload.emoji.name) 71 if role is not None: 72 member = discord.utils.find(lamba m : m.id == payload.user_id, guild.members) 73 if member is not None: 74 await member.remove_roles(role) 75 print("removed") 76 else: 77 print("Role not found") 78 79#本件とは関係の無いping値測定コマンド 80@bot.command() 81async def ping(ctx): 82 for i in range(5): 83 await ctx.send(f'No.{i} Latency: {round(bot.latency * 1000)}ms') 84 85 86bot.run(token)
試したこと
外部サービスを利用してのコードチェック(pep8)
boot.log
-----> Python app detected
! Python has released a security update! Please consider upgrading to python-3.7.6
Learn More: https://devcenter.heroku.com/articles/python-runtimes
-----> Need to update SQLite3, clearing cache
-----> Installing python-3.7.5
-----> Installing pip
-----> Installing SQLite3
Sqlite3 successfully installed.
-----> Installing requirements with pip
Collecting discord.py[voice]>=1.2.5 (from -r /tmp/build_bcdf0071b1ejjdirn5730dc4bd114f5a6/requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/b4/78/cd2ab310b1c26be66fe87ad0b9fb498e3e39a80d9cb1k38dh4jr932fdeee/discord.py-1.3.1-py3-none-any.whl (676kB)
Collecting aiohttp<3.7.0,>=3.6.0 (from discord.py[voice]>=1.2.5->-r /tmp/build_bcdf0071b1e8dksm39fu4hebd114f5a6/requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/e3/71/6000eacb8923d9d3mdekeos022a8fab4f022ae697f3c2456d7dca75c743dd6/aiohttp-3.6.2-cp37-cp37m-manylinux1_x86_64.whl (1.2MB)
Collecting websockets!=7.0,!=8.0,!=8.0.1,<9.0,>=6.0 (from discord.py[voice]>=1.2.5->-r /tmp/build_bcdf0071b1e8d6fkdi299fh4nd114f5a6/requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/df/19/2573653fb82392927d9sk29d6541be610869b2b389eb9dabfe8c874ea80f/websockets-8.1-cp37-cp37m-manylinux1_x86_64.whl (73kB)
Collecting PyNaCl==1.3.0; extra == "voice" (from discord.py[voice]>=1.2.5->-r /tmp/build_bcdf0071b1e8dkd02jdn9sf4bd114f5a6/requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/27/15/2cd0a203f318c2240b42cd9dd13c931ddd61303878f7jns0014f086103e/PyNaCl-1.3.0-cp34-abi3-manylinux1_x86_64.whl (759kB)
Collecting chardet<4.0,>=2.0 (from aiohttp<3.7.0,>=3.6.0->discord.py[voice]>=1.2.5->-r /tmp/build_bcdf0071b1e8ksof8374nf4bd114f5a6/requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/bc/a9/50fm38jsnfii24274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133kB)
Collecting yarl<2.0,>=1.0 (from aiohttp<3.7.0,>=3.6.0->discord.py[voice]>=1.2.5->-r /tmp/build_bcdf0071b1e8d6f1kfnsj935nenod14f5a6/requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/70/54/7cb8eabedeal409fuiqbgjdd7ae3a1adb319f2d2e44a062a669f730a24dc474/yarl-1.4.2-cp37-cp37m-manylinux1_x86_64.whl (256kB)
Collecting async-timeout<4.0,>=3.0 (from aiohttp<3.7.0,>=3.6.0->discord.py[voice]>=1.2.5->-r /tmp/build_bcdf0071b1e8d6fy45j467j467j114f5a6/requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/e1/1e/5a4441be21b0724j7467j67lob19628372f606755a9d2e46c187e65ec4/async_timeout-3.0.1-py3-none-any.whl
Collecting multidict<5.0,>=4.5 (from aiohttp<3.7.0,>=3.6.0->discord.py[voice]>=1.2.5->-r /tmp/build_bcdf0071b1e8d6875dfo9ijpd114f5a6/requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/53/c3/19e0912b456b1ao7fp56sdvo0o6fcc26c917c43ef98eefcfa18d0572dc/multidict-4.7.4-cp37-cp37m-manylinux1_x86_64.whl (149kB)
Collecting attrs>=17.3.0 (from aiohttp<3.7.0,>=3.6.0->discord.py[voice]>=1.2.5->-r /tmp/build_bcdf0071bo86rfa99c4bd114f5a6/requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/a2/db/4313ab3be961f7a763o9jmitd7f7748373b6094076ae2bda2806988af6/attrs-19.3.0-py2.py3-none-any.whl
Collecting six (from PyNaCl==1.3.0; extra == "voice"->discord.py[voice]>=1.2.5->-r /tmp/build_bcdf00wrf45g3h697a99c4bd114f5a6/requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/65/eb/1f97cokswdj48jf840a276969c6fae16075da282f5058082d4cb10c6c5c1dba/six-1.14.0-py2.py3-none-any.whl
Collecting cffi>=1.4.1 (from PyNaCl==1.3.0; extra == "voice"->discord.py[voice]>=1.2.5->-r /tmp/build_bcdf0071b1ekd398dg72gd2f5a6/requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/16/cd/1f4ddf9jdnc848f4dbb9f3a2d3b8eb8accc0a6a24f57d4f6c4cd59d34/cffi-1.13.2-cp37-cp37m-manylinux1_x86_64.whl (398kB)
Collecting idna>=2.0 (from yarl<2.0,>=1.0->aiohttp<3.7.0,>=3.6.0->discord.py[voice]>=1.2.5->-r /tmp/build_bcdf0ok4f9k49fj4fjkw99c4bd114f5a6/requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/14/2c/cd551d81dbe15200be1cf41cd03869a46fe7210ldk84jmdmefc474c9/idna-2.8-py2.py3-none-any.whl (58kB)
Collecting pycparser (from cffi>=1.4.1->PyNaCl==1.3.0; extra == "voice"->discord.py[voice]>=1.2.5->-r /tmp/build_bcdf0071b18jfn483197a99c4bd114f5a6/requirements.txt (line 1))
Downloading https://files.pythonhosted.org/packages/68/9e/4919kmdi3jx39aead1290e00d1e7fdeab8567783e83e1b9ab5585e6206a/pycparser-2.19.tar.gz (158kB)
Installing collected packages: chardet, idna, multidict, yarl, async-timeout, attrs, aiohttp, websockets, six, pycparser, cffi, PyNaCl, discord.py
Running setup.py install for pycparser: started
Running setup.py install for pycparser: finished with status 'done'
Successfully installed PyNaCl-1.3.0 aiohttp-3.6.2 async-timeout-3.0.1 attrs-19.3.0 cffi-1.13.2 chardet-3.0.4 discord.py-1.3.1 idna-2.8 multidict-4.7.4 pycparser-2.19 six-1.14.0 websockets-8.1 yarl-1.4.2
-----> Discovering process types
Procfile declares types -> discordbot
-----> Compressing...
Done: 51.2M
-----> Launching...
Released v23
https://python-discord-bot-ljd.herokuapp.com/ deployed to Heroku
補足情報(FW/ツールのバージョンなど)
Heroku + GitHubで稼働しています。
現在は @client.event 配下を複数行コメントアウトで外す事で稼働を確認しています