英文:
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
import cmath
import asyncio
from colorsys import rgb_to_hls
import math
import os
import discord
import random
from discord.ext import commands
from discord.ext import tasks
from discord.ext.commands import cooldown, BucketType
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='-', intents=intents)
bot.remove_command("help")
async def main():
async with bot:
await bot.load_extension("cogs.cog_function")
await bot.start('TOKEN')
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
asyncio.run(main())
@bot.event
async def on_ready():
print('We have logged in as {0.user}'.format(bot))
bot.run('TOKEN')
cog_function.py
import math
import discord
from discord.ext import commands
class CogFunction(commands.Cog):
def __init__(self, bot):
self.bot = bot
# commands in here
async def setup(bot):
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
import cmath
import asyncio
from colorsys import rgb_to_hls
import math
import os
import discord
import random
from discord.ext import commands
from discord.ext import tasks
from discord.ext.commands import cooldown, BucketType
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='-', intents=intents)
bot.remove_command("help")
async def main():
async with bot:
await bot.load_extension("cogs.cog_function")
await bot.start('TOKEN')
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
asyncio.run(main())
@bot.event
async def on_ready():
print('We have logged in as {0.user}'.format(bot))
bot.run('TOKEN')
cog_function.py
import math
import discord
from discord.ext import commands
class CogFunction(commands.Cog):
def __init__(self, bot):
self.bot = bot
# commands in here
async def setup(bot):
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!
答案1
得分: 2
你不能多次使用asyncio.run
,在幕后,bot.run
也使用它,这就是为什么它被调用两次。要修复它,你可以简单地将load_extension
移到bot.setup_hook
中,这是一个在机器人运行之前调用的新挂钩:
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='-', intents=intents)
bot.remove_command("help")
async def setup_hook():
await bot.load_extension("cogs.cog_function")
bot.setup_hook = setup_hook
@bot.event
async def on_ready():
print('We have logged in as {0.user}'.format(bot))
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:
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='-', intents=intents)
bot.remove_command("help")
async def setup_hook():
await bot.load_extension("cogs.cog_function")
bot.setup_hook = setup_hook
@bot.event
async def on_ready():
print('We have logged in as {0.user}'.format(bot))
bot.run('TOKEN')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论