我正在尝试为不和谐的服务器制作一个验证机器人(并且很有趣)。我注意到我无法将命令作为函数调用。我已经为仅启用该功能的单个python文件提供了解决方案。但是,当我想在单独的python文件中调用say
命令时,它将无法正常工作。
或者:如何在常规函数中使用channel.send()
async def say_c(message, channel_id):
await bot.wait_until_ready()
channel = bot.get_channel(channel_id)
await channel.send(message)
def say(*args, channel_id):
message = ""
for word in args:
message = message + word + " "
bot.loop.create_task(say_c(message, channel_id))
注意: say_c
是在不和谐服务器上放置内容的设备,而say
是我在文件中调用的函数。此方法有效,但不适用于单独的python文件。
答案 0 :(得分:0)
要能够从其他“文件”(通常称为module
)中使用它,它们必须位于同一工作空间中,也就是说,它们必须位于同一文件夹中(可能还有其他文件夹)设置,但这不在此问题的范围内。)
my_discord_bot/
├── discord_bot.py
├── other.py
假设您在discord_bot.py
中有上面发布的代码。
为了能够从other.py
进行调用,您需要将以下内容放入other.py
文件中:
from discord_bot import say
say('hello')