【问题标题】:Call function outside of @client.event (discord.py)在@client.event (discord.py) 之外调用函数
【发布时间】:2020-07-30 02:23:32
【问题描述】:

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

消息事件处理程序在这里。当收到消息开始游戏时,它会开始做各种事情来让游戏运行。

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: "coroutine 'day' was never awaited day(message)" 与另一个函数一样。

我该如何解决这个问题? 谢谢!

【问题讨论】:

    标签: discord.py


    【解决方案1】:

    好吧,也许你不需要 discord.py。要创建命令,请使用

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

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

    【讨论】:

      猜你喜欢
      • 2021-10-02
      • 2021-03-31
      • 2023-02-10
      • 2021-05-03
      • 2021-03-05
      • 2022-08-21
      • 2021-03-06
      • 2022-10-07
      • 2021-08-19
      相关资源
      最近更新 更多