我如何在python中通过我的discord bot的单独函数发送消息?



我正在编写一个小的Discord bot,我想使用我自己的个人服务器,我想尝试将所有命令关键字放入数组中,并为每个命令提供一个函数,以保持我的代码整洁。抱歉,如果这听起来很蠢,但我对Python还是个新手。我想尝试一个基本的垃圾邮件命令来测试我的bot,但是每当我试图让它通过该函数发送消息时,我都会得到这个错误:

File "main.py", line 31
await message.channel.send(f"spam")
^
SyntaxError: 'await' outside async function
下面是我的代码:
import discord
import os
import time
token = os.environ['token']
client = discord.Client()
keywords = ["spam"]
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
prefix = "."
if message.author == client.user:
return
if message.content.startswith(prefix):
mcontent = (str(message.content))[1:len(str(message.content))]
for i in range(len(keywords)):
if keywords[0] in mcontent:
spam(mcontent, message)

def spam(mcontent, message):
n = int(mcontent[4:len(mcontent)])
n2 = 0
while n2 < n:
await message.channel.send(f"spam")
n2 = n2 + 1
time.sleep(1)
client.run(token)

您忘记设置函数async:

async def spam(mcontent, message):
...

相关内容

最新更新