如何制作pythonb的多个文件

2024-07-03 01:30:32 发布

您现在位置:Python中文网/ 问答频道 /正文

如何从多个文件加载命令Python Bot下面是我的主.py以及其他带有命令的python文件。这是正确的方法还是我需要改变什么?我是否需要在所有文件中添加tokenprefixbot = commands.Botbot.run(token)等。在

主.py

token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
prefix = "?"

import discord
from discord.ext import commands
startup_extensions = ["second", "third"]

bot = commands.Bot(command_prefix=prefix)
bot.remove_command("help")

@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print('------')

@bot.command(pass_context=True)
async def hello1(ctx):
    msg = 'Hello {0.author.mention}'.format(ctx.message)
    await bot.say(msg)

bot.run(token)

第二.py

^{pr2}$

第三.py

import discord
from discord.ext import commands

class Third():
def __init__(self, bot):
    self.bot = bot

@commands.command(pass_context=True)
async def hello3(ctx):
    msg = 'Hello{0.author.mention}'.format(ctx.message)
    await bot.say(msg)

def setup(bot):
bot.add_cog(Third(bot))

Tags: 文件pyimporttokenasyncprefixdefbot