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

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

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=&#39;-&#39;, intents=intents)
bot.remove_command(&quot;help&quot;)

async def main():
    async with bot:
        await bot.load_extension(&quot;cogs.cog_function&quot;)
        await bot.start(&#39;TOKEN&#39;)

asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
asyncio.run(main())


@bot.event
async def on_ready():
    print(&#39;We have logged in as {0.user}&#39;.format(bot))

bot.run(&#39;TOKEN&#39;)

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&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中,这是一个在机器人运行之前调用的新挂钩:

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=&#39;-&#39;, intents=intents)
bot.remove_command(&quot;help&quot;)


async def setup_hook():
    await bot.load_extension(&quot;cogs.cog_function&quot;)


bot.setup_hook = setup_hook


@bot.event
async def on_ready():
    print(&#39;We have logged in as {0.user}&#39;.format(bot))


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:

确定