我正在使用python编写Discord机器人,但遇到了问题。我希望机器人打印接收到的消息,但是它执行了两次。而且我不知道为什么要打印两次。
https://github.com/PGillner/Discord-IPL-Bot/tree/master
上面是导致问题的3个“模块”的链接,它尚在开发中,没有太多的代码,应该相当容易浏览。感谢您对其为何两次打印的任何帮助!
Main.py
import discord
import asyncio
from discord import Status
from Status import Member_Status
from User import User_Who
from Matchmaking import MM
runtime = 0
client = discord.Client()
@client.event
async def on_member_update(before, after):
await Member_Status(before, after) ## Complete
@client.event
async def on_message(message):
await User_Who(message) ##Incomplete
if await User_Who(message):
await client.delete_message(message)
async def my_background_task():
await client.wait_until_ready()
while not client.is_closed:
global run_time
await MM() ##Incomplete
print("Runtime: {}s".format(runtime))
runtime += 10
await asyncio.sleep(10)
@client.event
async def on_ready():
print("------Logged in as------")
print(" {}".format(client.user.name))
print(" {}".format(client.user.id))
print("------------------------")
client.loop.create_task(my_background_task())
client.run("NDU0OTQ1NTcyNzI2NzY3NjI3.Df04DA.paEHHr5h1frdHFLsth2THp7FFBs")
User.py
import discord
import asyncio
from Print import Print_MSG
from Queue_join import Queue_Join
from Queue_leave import Queue_Leave
from match_room_1 import M_R_1
from match_room_2 import M_R_2
from match_room_3 import M_R_3
from match_room_4 import M_R_4
from match_room_5 import M_R_5
client = discord.Client()
async def User_Who(message):
if message.author != client.user:
await Print_MSG(message) ## Complete
if str(message.channel) == "queue":
if message.content.startswith("!join"):
await Queue_Join() ##Incomplete
elif message.content.startswith("!leave"):
await Queue_Leave() ##Incomplete
else:
return True
elif str(message.channel) == "match_room_1":
await M_R_1() ##Incomplete
elif str(message.channel) == "match_room_1":
await M_R_2() ##Incomplete
elif str(message.channel) == "match_room_1":
await M_R_3() ##Incomplete
elif str(message.channel) == "match_room_1":
await M_R_4() ##Incomplete
elif str(message.channel) == "match_room_1":
await M_R_5() ##Incomplete
Print.py
import discord
import asyncio
async def Print_MSG(message):
timestamp = str(message.timestamp)
timestamp = timestamp[:-7]
timestamp = timestamp[11:]
print("Channel: {} | {} [{}]: {}".format(message.channel, timestamp, message.author, message.content))
答案 0 :(得分:0)
await User_Who(message) ##Incomplete
if await User_Who(message):
您的User_Who
协程运行两次,每个协程包含Print_MSG
。
is_complete = await User_Who(message) ##Incomplete
if is_complete:
此外,您正在过度使用async
/ await
。我可以知道您为什么也为Print_MSG
这么做的原因吗?它只是打印消息。