在@ client.event(discord.py)之外调用函数

时间:2020-04-16 18:14:15

标签: discord.py

我正在制作一个不和谐的机器人来玩游戏,使用直接和开放消息命令来注册玩家的动作。

消息事件处理程序在这里。收到启动游戏的消息时,它将开始做各种事情来使游戏运行。

import discord
import random
import datetime
client = discord.Client()


@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))


@client.event
async def on_message(message):
    if message.author == client.user:
        return


    if "!ww start" == message.content.lower() and not(discord.ChannelType == "private"):
        # Do stuff
        night(message)


    if "!ww save " in message.content.lower() and discord.ChannelType == "private":
        save(message)


    if "!ww kill " in message.content.lower() and discord.ChannelType == "private":
        kill(message)


    if "!ww vote " in message.content.lower() and discord.ChannelType == "private":
        vote(message)


    if "!ww view " in message.content.lower() and discord.ChannelType == "private":
        view(message)


    if "!ww remove" == message.content.lower():
        remove(message)


    if ("!ww add" == message.content.lower()):
        add(message)


    if (message.content == '!ww count'):
        count(message)

    if discord.ChannelType == "private":
        night(message)

    day(message)
'''

One example of a function I want to call is here, like others it makes use of the message and channel. This is in line with the above block of code.
'''
async def night(message):
    with open("GameData.txt", mode="r", encoding="utf-8") as my_file:
        gameData = my_file.read().replace('\n', '')
    if gameData[-1] != " ":
        gameData = gameData + " "
    gameData = list(gameData.split(", "))

    if gameData[4] != "Night":
        return
    for i in range(len(gameData)):
        if gameData[i] == ".":
            return

    # Read who was killed
    channel = client.get_channel(int(gameData[1]))
    if gameData[2] == gameData[0]:
        await channel.send("Someone was saved in the night")
    else:
        killed = gameData[2]
        await channel.send(killed + " was killed in the night")
        remove(message)

    await message.channel.send("It is now day, the game will continue when you all make your decision")
    nightClean()


client.run("No")

最终,两个函数在运行后会相互调用。仅当通过其他功能处理的其他消息输入进行了其他输入时,它们才会运行。

目前,我收到一条错误消息:RuntimeWarning:“协程'day'从未等待过一天(消息)”,以及其他功能。

我该如何解决? 谢谢!

1 个答案:

答案 0 :(得分:0)

好吧,也许你不熟悉 discord.py。创建命令使用

@client.command()
async def command_name(ctx, commands args like user to ban etc):
    Do stuff

这是一个命令。不要使用 on_message 函数。