我正在将命令移动到齿轮并对其进行排序,但是我无法继续加载齿轮。
app.py
<div class="colorscripter-code" style="color:#010101;font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace !important; position:relative !important;overflow:auto"><table class="colorscripter-code-table" style="margin:0;padding:0;border:none;background-color:#fafafa;border-radius:4px;" cellspacing="0" cellpadding="0"><tr><td style="padding:6px;border-right:2px solid #e5e5e5"><div style="margin:0;padding:0;word-break:normal;text-align:right;color:#666;font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace !important;line-height:130%"><div style="line-height:130%">1</div><div style="line-height:130%">2</div><div style="line-height:130%">3</div><div style="line-height:130%">4</div><div style="line-height:130%">5</div><div style="line-height:130%">6</div><div style="line-height:130%">7</div><div style="line-height:130%">8</div><div style="line-height:130%">9</div><div style="line-height:130%">10</div><div style="line-height:130%">11</div><div style="line-height:130%">12</div><div style="line-height:130%">13</div><div style="line-height:130%">14</div><div style="line-height:130%">15</div><div style="line-height:130%">16</div><div style="line-height:130%">17</div><div style="line-height:130%">18</div><div style="line-height:130%">19</div><div style="line-height:130%">20</div><div style="line-height:130%">21</div><div style="line-height:130%">22</div><div style="line-height:130%">23</div><div style="line-height:130%">24</div><div style="line-height:130%">25</div><div style="line-height:130%">26</div><div style="line-height:130%">27</div><div style="line-height:130%">28</div><div style="line-height:130%">29</div><div style="line-height:130%">30</div><div style="line-height:130%">31</div><div style="line-height:130%">32</div><div style="line-height:130%">33</div><div style="line-height:130%">34</div><div style="line-height:130%">35</div><div style="line-height:130%">36</div><div style="line-height:130%">37</div></div></td><td style="padding:6px 0;text-align:left"><div style="margin:0;padding:0;color:#010101;font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace !important;line-height:130%"><div style="padding:0 6px; white-space:pre; line-height:130%"><span style="color:#a71d5d">import</span> discord</div><div style="padding:0 6px; white-space:pre; line-height:130%"><span style="color:#a71d5d">import</span> asyncio</div><div style="padding:0 6px; white-space:pre; line-height:130%"><span style="color:#a71d5d">import</span> re</div><div style="padding:0 6px; white-space:pre; line-height:130%"><span style="color:#a71d5d">import</span> json</div><div style="padding:0 6px; white-space:pre; line-height:130%"><span style="color:#a71d5d">from</span> discord.ext <span style="color:#a71d5d">import</span> commands</div><div style="padding:0 6px; white-space:pre; line-height:130%"> </div><div style="padding:0 6px; white-space:pre; line-height:130%"><span style="color:#999999">#Json Part</span></div><div style="padding:0 6px; white-space:pre; line-height:130%">with <span style="color:#066de2">open</span>(<span style="color:#63a35c">'config/setting.json'</span>) as json_file:</div><div style="padding:0 6px; white-space:pre; line-height:130%"> json_data <span style="color:#0086b3"></span><span style="color:#a71d5d">=</span> json.load(json_file)</div><div style="padding:0 6px; white-space:pre; line-height:130%"> token <span style="color:#0086b3"></span><span style="color:#a71d5d">=</span> json_data[<span style="color:#63a35c">"bot_token"</span>]</div><div style="padding:0 6px; white-space:pre; line-height:130%"> prefix <span style="color:#0086b3"></span><span style="color:#a71d5d">=</span> json_data[<span style="color:#63a35c">"prefix"</span>]</div><div style="padding:0 6px; white-space:pre; line-height:130%"> ver <span style="color:#0086b3"></span><span style="color:#a71d5d">=</span> json_data[<span style="color:#63a35c">"ver"</span>]</div><div style="padding:0 6px; white-space:pre; line-height:130%"> </div><div style="padding:0 6px; white-space:pre; line-height:130%"><span style="color:#999999">#Bot Part</span></div><div style="padding:0 6px; white-space:pre; line-height:130%">bot <span style="color:#0086b3"></span><span style="color:#a71d5d">=</span> commands.Bot(command_prefix<span style="color:#0086b3"></span><span style="color:#a71d5d">=</span><span style="color:#63a35c">'%a'</span> % prefix)</div><div style="padding:0 6px; white-space:pre; line-height:130%">client <span style="color:#0086b3"></span><span style="color:#a71d5d">=</span> discord.Client()</div><div style="padding:0 6px; white-space:pre; line-height:130%"> </div><div style="padding:0 6px; white-space:pre; line-height:130%">@client.event </div><div style="padding:0 6px; white-space:pre; line-height:130%">async <span style="color:#a71d5d">def</span> on_ready():</div><div style="padding:0 6px; white-space:pre; line-height:130%"> </div><div style="padding:0 6px; white-space:pre; line-height:130%"> <span style="color:#066de2">print</span>(<span style="color:#63a35c">"Thanks for run this program. The version of this program is %s."</span> % ver)</div><div style="padding:0 6px; white-space:pre; line-height:130%"> <span style="color:#066de2">print</span>(client.user.name)</div><div style="padding:0 6px; white-space:pre; line-height:130%"> <span style="color:#066de2">print</span>(client.user.id)</div><div style="padding:0 6px; white-space:pre; line-height:130%"> </div><div style="padding:0 6px; white-space:pre; line-height:130%"> game <span style="color:#0086b3"></span><span style="color:#a71d5d">=</span> discord.Game(<span style="color:#63a35c">"!n.help | Alpha version"</span>)</div><div style="padding:0 6px; white-space:pre; line-height:130%"> await client.change_presence(status<span style="color:#0086b3"></span><span style="color:#a71d5d">=</span>discord.Status.online, activity<span style="color:#0086b3"></span><span style="color:#a71d5d">=</span>game)</div><div style="padding:0 6px; white-space:pre; line-height:130%"> </div><div style="padding:0 6px; white-space:pre; line-height:130%"><span style="color:#999999">#Cogs Part</span></div><div style="padding:0 6px; white-space:pre; line-height:130%">initial_extensions <span style="color:#0086b3"></span><span style="color:#a71d5d">=</span> [<span style="color:#63a35c">'cogs.manage'</span>,</div><div style="padding:0 6px; white-space:pre; line-height:130%"> <span style="color:#63a35c">'cogs.command'</span>]</div><div style="padding:0 6px; white-space:pre; line-height:130%"><span style="color:#999999">#FIXME: discord.ext.commands.errors.ExtensionNotFound: Extension 'cogs.manage' could not be loaded.</span></div><div style="padding:0 6px; white-space:pre; line-height:130%"><span style="color:#a71d5d">if</span> __name__ <span style="color:#0086b3"></span><span style="color:#a71d5d">=</span><span style="color:#0086b3"></span><span style="color:#a71d5d">=</span> <span style="color:#63a35c">'__main__'</span>:</div><div style="padding:0 6px; white-space:pre; line-height:130%"> <span style="color:#a71d5d">for</span> extenstion <span style="color:#a71d5d">in</span> initial_extensions:</div><div style="padding:0 6px; white-space:pre; line-height:130%"> bot.load_extension(extenstion)</div><div style="padding:0 6px; white-space:pre; line-height:130%"> </div><div style="padding:0 6px; white-space:pre; line-height:130%">client.run(token) </div><div style="padding:0 6px; white-space:pre; line-height:130%"> </div></div><div style="text-align:right;margin-top:-13px;margin-right:5px;font-size:9px;font-style:italic"><a href="http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter</a></div></td><td style="vertical-align:bottom;padding:0 2px 4px 0"><a href="http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white"><span style="font-size:9px;word-break:normal;background-color:#e5e5e5;color:white;border-radius:10px;padding:1px">cs</span></a></td></tr></table></div>
manage.py
<div class="colorscripter-code" style="color:#010101;font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace !important; position:relative !important;overflow:auto"><table class="colorscripter-code-table" style="margin:0;padding:0;border:none;background-color:#fafafa;border-radius:4px;" cellspacing="0" cellpadding="0"><tr><td style="padding:6px;border-right:2px solid #e5e5e5"><div style="margin:0;padding:0;word-break:normal;text-align:right;color:#666;font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace !important;line-height:130%"><div style="line-height:130%">1</div><div style="line-height:130%">2</div><div style="line-height:130%">3</div><div style="line-height:130%">4</div><div style="line-height:130%">5</div><div style="line-height:130%">6</div><div style="line-height:130%">7</div><div style="line-height:130%">8</div><div style="line-height:130%">9</div><div style="line-height:130%">10</div><div style="line-height:130%">11</div><div style="line-height:130%">12</div><div style="line-height:130%">13</div><div style="line-height:130%">14</div><div style="line-height:130%">15</div><div style="line-height:130%">16</div><div style="line-height:130%">17</div><div style="line-height:130%">18</div><div style="line-height:130%">19</div><div style="line-height:130%">20</div><div style="line-height:130%">21</div><div style="line-height:130%">22</div><div style="line-height:130%">23</div><div style="line-height:130%">24</div></div></td><td style="padding:6px 0;text-align:left"><div style="margin:0;padding:0;color:#010101;font-family:Consolas, 'Liberation Mono', Menlo, Courier, monospace !important;line-height:130%"><div style="padding:0 6px; white-space:pre; line-height:130%"><span style="color:#a71d5d">import</span> discord</div><div style="padding:0 6px; white-space:pre; line-height:130%"><span style="color:#a71d5d">import</span> asyncio</div><div style="padding:0 6px; white-space:pre; line-height:130%"><span style="color:#a71d5d">import</span> re</div><div style="padding:0 6px; white-space:pre; line-height:130%"><span style="color:#a71d5d">import</span> json</div><div style="padding:0 6px; white-space:pre; line-height:130%"><span style="color:#a71d5d">from</span> discord.ext <span style="color:#a71d5d">import</span> commands</div><div style="padding:0 6px; white-space:pre; line-height:130%"> </div><div style="padding:0 6px; white-space:pre; line-height:130%"><span style="color:#a71d5d">class</span> manage(Commands.Cog):</div><div style="padding:0 6px; white-space:pre; line-height:130%"> <span style="color:#a71d5d">def</span> __init__(self, bot):</div><div style="padding:0 6px; white-space:pre; line-height:130%"> self.bot <span style="color:#0086b3"></span><span style="color:#a71d5d">=</span> bot</div><div style="padding:0 6px; white-space:pre; line-height:130%"> </div><div style="padding:0 6px; white-space:pre; line-height:130%"> <span style="color:#a71d5d">def</span> command(self):</div><div style="padding:0 6px; white-space:pre; line-height:130%"> <span style="color:#999999">#Json part</span></div><div style="padding:0 6px; white-space:pre; line-height:130%"> with <span style="color:#066de2">open</span>(<span style="color:#63a35c">'config/setting.json'</span>) as json_file:</div><div style="padding:0 6px; white-space:pre; line-height:130%"> json_data <span style="color:#0086b3"></span><span style="color:#a71d5d">=</span> json.load(json_file)</div><div style="padding:0 6px; white-space:pre; line-height:130%"> prefix <span style="color:#0086b3"></span><span style="color:#a71d5d">=</span> json_data[<span style="color:#63a35c">"prefix"</span>]</div><div style="padding:0 6px; white-space:pre; line-height:130%"> </div><div style="padding:0 6px; white-space:pre; line-height:130%"> <span style="color:#999999">#commands part</span></div><div style="padding:0 6px; white-space:pre; line-height:130%"> @commands.command()</div><div style="padding:0 6px; white-space:pre; line-height:130%"> async <span style="color:#a71d5d">def</span> on_message(message):</div><div style="padding:0 6px; white-space:pre; line-height:130%"> <span style="color:#a71d5d">if</span> message.author <span style="color:#0086b3"></span><span style="color:#a71d5d">=</span><span style="color:#0086b3"></span><span style="color:#a71d5d">=</span> client.user:</div><div style="padding:0 6px; white-space:pre; line-height:130%"> <span style="color:#a71d5d">return</span></div><div style="padding:0 6px; white-space:pre; line-height:130%"> </div><div style="padding:0 6px; white-space:pre; line-height:130%"> <span style="color:#a71d5d">def</span> setup(bot):</div><div style="padding:0 6px; white-space:pre; line-height:130%"> bot.add_cog(manageCog(bot))</div></div></td><td style="vertical-align:bottom;padding:0 2px 4px 0"><a href="http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white"><span style="font-size:9px;word-break:normal;background-color:#e5e5e5;color:white;border-radius:10px;padding:1px">cs</span></a></td></tr></table></div>
答案 0 :(得分:0)
manage.py是文件夹cogs
中的子模块吗?因为那是您引用它的方式。参见the documentation for Bot.load_extension
。
此外,您的setup
函数需要是全局的,即不是manage
类中的方法,并且应该使用现有的类,因此您的manage
机器人可能也可以。我不确定manageCog
是什么,但是您没有在任何地方显示它。
有关齿轮的更多信息,请参见https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html。
此外,on_message
是一个事件,而不是命令,因此需要用@commands.Cog.listener()
(而不是@commands.command()
)修饰。无论哪种方式,它都不应该在另一个方法中,而应该在类/齿轮中使用它自己的方法。
供以后参考,您无需将HTML格式的代码粘贴到JavaScript代码段中,只需将代码直接添加到带有代码块的问题中即可。
有关降价的帮助,请参见https://stackoverflow.com/editing-help。