Discord bot.py将不会在使用await bot.start()时运行。

huangapple go评论98阅读模式
英文:

Discord bot.py will not run with await bot.start()

问题

I have been trying to put a command in a separate file in a folder called cogs. I have been trying to find resources however most are for cogs 1.0 and an older version of discord.py

In this one I put bot.load_extension() inside async def main() because it wanted to await, and I ran the main with asyncio.

However it still does not work. Below are my codes without the commands

bot.py

  1. import cmath
  2. import asyncio
  3. from colorsys import rgb_to_hls
  4. import math
  5. import os
  6. import discord
  7. import random
  8. from discord.ext import commands
  9. from discord.ext import tasks
  10. from discord.ext.commands import cooldown, BucketType
  11. intents = discord.Intents.default()
  12. intents.message_content = True
  13. bot = commands.Bot(command_prefix='-', intents=intents)
  14. bot.remove_command("help")
  15. async def main():
  16. async with bot:
  17. await bot.load_extension("cogs.cog_function")
  18. await bot.start('TOKEN')
  19. asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
  20. asyncio.run(main())
  21. @bot.event
  22. async def on_ready():
  23. print('We have logged in as {0.user}'.format(bot))
  24. bot.run('TOKEN')

cog_function.py

  1. import math
  2. import discord
  3. from discord.ext import commands
  4. class CogFunction(commands.Cog):
  5. def __init__(self, bot):
  6. self.bot = bot
  7. # commands in here
  8. async def setup(bot):
  9. await bot.add_cog(CogFunction(bot))

It gave me no error, no tracebacks, etc.
The only line in the Terminal is
PS C:\Users\[MY USERNAME]\Desktop\Discord Bot> & C:/Users/[MY USERNAME]/AppData/Local/Programs/Python/Python39/python.exe "c:/Users/[MY USERNAME]/Desktop/Discord Bot/bot.py" with no additional errors or anything

I tried to remove await bot.start('TOKEN') but obviously it did not work, giving me

Unclosed connector connections: ['[<aiohttp.client_proto.ResponseHandler object at 0x00000247D2BEB220>, 4735.609]'] connector: <aiohttp.connector.TCPConnector object at 0x00000247D2BB9400>

I also tried removing bot.run(TOKEN) but it did not work either.

Any help would be greatly appreciated, thanks!

英文:

I have been trying to put a command in a separate file in a folder called cogs. I have been trying to find resources however most are for cogs1.0 and an older version of discord.py

In this one I put bot.load_extension() inside async def main() because it wanted to await, and I ran the main with asyncio.

However it still does not work. Below are my codes without the commands

bot.py

  1. import cmath
  2. import asyncio
  3. from colorsys import rgb_to_hls
  4. import math
  5. import os
  6. import discord
  7. import random
  8. from discord.ext import commands
  9. from discord.ext import tasks
  10. from discord.ext.commands import cooldown, BucketType
  11. intents = discord.Intents.default()
  12. intents.message_content = True
  13. bot = commands.Bot(command_prefix=&#39;-&#39;, intents=intents)
  14. bot.remove_command(&quot;help&quot;)
  15. async def main():
  16. async with bot:
  17. await bot.load_extension(&quot;cogs.cog_function&quot;)
  18. await bot.start(&#39;TOKEN&#39;)
  19. asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
  20. asyncio.run(main())
  21. @bot.event
  22. async def on_ready():
  23. print(&#39;We have logged in as {0.user}&#39;.format(bot))
  24. bot.run(&#39;TOKEN&#39;)

cog_function.py

  1. import math
  2. import discord
  3. from discord.ext import commands
  4. class CogFunction(commands.Cog):
  5. def __init__(self, bot):
  6. self.bot = bot
  7. # commands in here
  8. async def setup(bot):
  9. await bot.add_cog(CogFunction(bot))

It gave me no error, no tracebacks, etc.
The only line in the Terminal is
PS C:\Users\[MY USERNAME]\Desktop\Discord Bot&gt; &amp; C:/Users/[MY USERNAME]/AppData/Local/Programs/Python/Python39/python.exe &quot;c:/Users/[MY USERNAME]/Desktop/Discord Bot/bot.py&quot; with no additional errors or anything

I tried to remove await bot.start(&#39;TOKEN&#39;) but obviously it did not work, giving me

Unclosed connector
connections: [&#39;[(&lt;aiohttp.client_proto.ResponseHandler object at 0x00000247D2BEB220&gt;, 4735.609)]&#39;]
connector: &lt;aiohttp.connector.TCPConnector object at 0x00000247D2BB9400&gt;

I also tried removing bot.run(TOKEN) but it did not work either.

Any help would be greatly appreciated, thanks!

答案1

得分: 2

你不能多次使用asyncio.run,在幕后,bot.run也使用它,这就是为什么它被调用两次。要修复它,你可以简单地将load_extension移到bot.setup_hook中,这是一个在机器人运行之前调用的新挂钩:

  1. import discord
  2. from discord.ext import commands
  3. intents = discord.Intents.default()
  4. intents.message_content = True
  5. bot = commands.Bot(command_prefix='-', intents=intents)
  6. bot.remove_command("help")
  7. async def setup_hook():
  8. await bot.load_extension("cogs.cog_function")
  9. bot.setup_hook = setup_hook
  10. @bot.event
  11. async def on_ready():
  12. print('We have logged in as {0.user}'.format(bot))
  13. bot.run('TOKEN')
英文:

You can't use asyncio.run more than once, behind the scenes, bot.run also uses it, that's why it's called 2 times. To fix it, you can simply move the load_extension into bot.setup_hook which is a new hook that's called before the bot runs:

  1. import discord
  2. from discord.ext import commands
  3. intents = discord.Intents.default()
  4. intents.message_content = True
  5. bot = commands.Bot(command_prefix=&#39;-&#39;, intents=intents)
  6. bot.remove_command(&quot;help&quot;)
  7. async def setup_hook():
  8. await bot.load_extension(&quot;cogs.cog_function&quot;)
  9. bot.setup_hook = setup_hook
  10. @bot.event
  11. async def on_ready():
  12. print(&#39;We have logged in as {0.user}&#39;.format(bot))
  13. bot.run(&#39;TOKEN&#39;)

huangapple
  • 本文由 发表于 2023年5月11日 09:52:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76223631.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定