如何导入 discord bot 对象以在其他模块中使用
how to import discord bot object for use in other module
我试图减少我的 discord bot 主文件中的行数,所以我创建了一个模块来处理某些命令,问题是我必须将 bot 对象传递给主文件之外的每个函数在模块中,这增加了我传递给每个函数的变量数量并且它变得非常混乱。
在下面的示例中,KsBot.py 是我的主要机器人文件,BasicCommand.py 是我编写简单命令并将其导入到 KsBot.py 以供使用的地方,KsBot 是机器人对象
在KsBot.py中:
if message.content.startswith("//ping"):
await BasicCommand.ping(KsBot,message.channel)
在BasicCommand.py中:
async def ping(bot,channel):
KsBot = bot
await KsBot.send_message(channel,"pong")
我想在 BasicCommand.py 中添加一个变量来表示 KsBot,这样我就不必在每个函数中都传入 bot 对象,我已经尝试将 bot 对象本身导入 BasicCommand.py 通过将此添加到代码的顶部:
from KsBot import KsBot
但它给我一个错误提示:
ImportError: cannot import name 'KsBot'
有人可以向我解释为什么会发生此错误,以及是否有任何方法可以传递此 bot 对象。我是编程新手 discord.py 所以任何替代建议也很受欢迎,谢谢 :D
一个好的 discord.py 机器人不会手动调用来自 on_message
的所有命令,您应该使用 @bot.command(pass_context=True)
添加命令。
去掉 def on_message(...):
,因为你在那里没有做任何有意义的事情。
一个文件的粗略结构(不拆分):
from discord.ext import commands
bot = commands.Bot('//')
@bot.command(pass_context=True)
async def ping(ctx):
channel = ctx.message.channel
await bot.send_message(channel, "pong")
bot.run("TOKEN")
就这么简单!
现在,多拆分成多个文件,最简单的方法就是自己定义builtins.bot
。或者,您可以查看第二个文件中带有 def setup(bot):
的 bot.load_extension("filename")
。两者都是有效的方法,但后者最适用于齿轮。
下面的例子是第一种方法:
# ksbot.py
from discord.ext import commands
import builtins
bot = commands.Bot('//')
builtins.bot = bot
import basiccommand
@bot.command(pass_context=True)
async def ping(ctx):
channel = ctx.message.channel
await bot.send_message(channel, "pong")
bot.run("TOKEN")
# basiccommand.py
from discord.ext import commands
from builtins import bot
@bot.command(pass_context=True)
async def hello(ctx):
channel = ctx.message.channel
await bot.send_message(channel, "hi")
现在您有两个命令,ping 发送 "pong",hello 发送 "hi"。
如果有必须加一个on_message
句柄,一定要在on_message
末尾加上bot.process_commands(message)
,上面的例子不需要你有事件句柄。
旁注:按照惯例,文件名应小写。
我试图减少我的 discord bot 主文件中的行数,所以我创建了一个模块来处理某些命令,问题是我必须将 bot 对象传递给主文件之外的每个函数在模块中,这增加了我传递给每个函数的变量数量并且它变得非常混乱。
在下面的示例中,KsBot.py 是我的主要机器人文件,BasicCommand.py 是我编写简单命令并将其导入到 KsBot.py 以供使用的地方,KsBot 是机器人对象
在KsBot.py中:
if message.content.startswith("//ping"):
await BasicCommand.ping(KsBot,message.channel)
在BasicCommand.py中:
async def ping(bot,channel):
KsBot = bot
await KsBot.send_message(channel,"pong")
我想在 BasicCommand.py 中添加一个变量来表示 KsBot,这样我就不必在每个函数中都传入 bot 对象,我已经尝试将 bot 对象本身导入 BasicCommand.py 通过将此添加到代码的顶部:
from KsBot import KsBot
但它给我一个错误提示:
ImportError: cannot import name 'KsBot'
有人可以向我解释为什么会发生此错误,以及是否有任何方法可以传递此 bot 对象。我是编程新手 discord.py 所以任何替代建议也很受欢迎,谢谢 :D
一个好的 discord.py 机器人不会手动调用来自 on_message
的所有命令,您应该使用 @bot.command(pass_context=True)
添加命令。
去掉 def on_message(...):
,因为你在那里没有做任何有意义的事情。
一个文件的粗略结构(不拆分):
from discord.ext import commands
bot = commands.Bot('//')
@bot.command(pass_context=True)
async def ping(ctx):
channel = ctx.message.channel
await bot.send_message(channel, "pong")
bot.run("TOKEN")
就这么简单!
现在,多拆分成多个文件,最简单的方法就是自己定义builtins.bot
。或者,您可以查看第二个文件中带有 def setup(bot):
的 bot.load_extension("filename")
。两者都是有效的方法,但后者最适用于齿轮。
下面的例子是第一种方法:
# ksbot.py
from discord.ext import commands
import builtins
bot = commands.Bot('//')
builtins.bot = bot
import basiccommand
@bot.command(pass_context=True)
async def ping(ctx):
channel = ctx.message.channel
await bot.send_message(channel, "pong")
bot.run("TOKEN")
# basiccommand.py
from discord.ext import commands
from builtins import bot
@bot.command(pass_context=True)
async def hello(ctx):
channel = ctx.message.channel
await bot.send_message(channel, "hi")
现在您有两个命令,ping 发送 "pong",hello 发送 "hi"。
如果有必须加一个on_message
句柄,一定要在on_message
末尾加上bot.process_commands(message)
,上面的例子不需要你有事件句柄。
旁注:按照惯例,文件名应小写。