Python 正规函数';不要在不和谐的环境中工作
当我尝试在discord.py目录中添加一个普通函数时,它将弹出Python 正规函数';不要在不和谐的环境中工作,python,discord.py,Python,Discord.py,当我尝试在discord.py目录中添加一个普通函数时,它将弹出未定义 机器人 import discord from discord.ext import commands import os token = You cant have it prefix = "." client = commands.Bot(command_prefix=prefix) @client.event async def on_ready(): print(f"{cl
未定义
机器人
import discord
from discord.ext import commands
import os
token = You cant have it
prefix = "."
client = commands.Bot(command_prefix=prefix)
@client.event
async def on_ready():
print(f"{client.user.name} is ready!")
for filename in os.listdir("./commands"):
if filename.endswith(".py"):
client.load_extension(f"commands.{filename[:-3]}")
print(f"loaded: {filename[:-3]}")
client.run(token)
焦距
import discord
from discord.ext import commands
class Misc(commands.Cog):
def __init__(self, client):
self.client = client
def print_message(self,message):
print(f"New message received!\nAuthor: {message.author}\nContent: {message.content}\n----------")
@commands.Cog.listener()
async def on_message(self,message):
print_message(message)
def setup(client):
client.add_cog(Misc(client))
完全错误:
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\Jlp02\AppData\Roaming\Python\Python38\site-packages\discord\client.py", line 333, in _run_event
await coro(*args, **kwargs)
File "E:\Discord Bots\Mike\commands\Misc.py", line 15, in on_message
print_message(message)
NameError: name 'print_message' is not defined
Ignoring exception in command None:
discord.ext.commands.errors.CommandNotFound: Command "hi" is not found
Ignoring exception in on_message
Traceback (most recent call last):
File "C:\Users\Jlp02\AppData\Roaming\Python\Python38\site-packages\discord\client.py", line 333, in _run_event
await coro(*args, **kwargs)
File "E:\Discord Bots\Mike\commands\Misc.py", line 15, in on_message
print_message(message)
NameError: name 'print_message' is not defined
print\u message
是一个Misc
类方法,因此必须使用self调用它。print\u message(message)
:
导入不一致
从discord.ext导入命令
类杂项(commands.Cog):
定义初始化(自我,客户机):
self.client=client
def打印信息(自我,信息):
打印(f“收到新邮件!\n收件人:{message.author}\n内容:{message.content}\n------”)
@commands.Cog.listener()
_消息上的异步定义(self,message):
self.print_消息(消息)
def设置(客户端):
客户。添加_cog(杂项(客户))