如何制作多个文件Python Bot

时间:2018-06-04 10:25:17

标签: python python-3.x discord discord.py

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

main.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)

second.py

import discord
from discord.ext import commands

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

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

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

third.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))

0 个答案:

没有答案