簡體   English   中英

為什么不將消息發送到頻道?

[英]Why won't the message be sent to the channel?

嗨,我是使用Discord.py創建機器人的新手。 盡管現在被卡住了,但我在使用文檔方面相處得很好。

這是來自commands.py的所有代碼。

# All commands
cmds = {
    # The 'cmd' variable I'm using in the second code block
    "ping": {
        "name": "Ping",
        "description": "Ping pong",
        "usage_str": "ping",
        "rank_needed": 0,
        "execute": None
    }
}

# Define the function
def ping(client, message, channel, args):
    print("SENDING MESSAGE!")
    client.send_message(channel, "Pong!")

# Set the execute index in the dictionary to equal the function
cmds["ping"]["execute"] = ping

這是main.py中調用ping函數的代碼。

cmd["execute"](client, message, channel, content)

當我嘗試執行功能時,盡管控制台中已打印“ SENDING MESSAGE”,但沒有消息發送到通道。 為什么是這樣? 我錯過了什么?

您需要await send_message協程,這意味着ping本身必須是協程,並從事件循環內部執行。 我建議使用discord.ext.commands擴展名:

from discord.ext import commands
import discord

bot = commands.Bot("!")

@bot.command(pass_context=True)
async def ping(ctx, channel: discord.Channel, *, message):
    print("SENDING MESSAGE!")
    await bot.send_message(channel, message)

bot.run("token")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM