Discord.py 使机器人通过按键向特定频道发送消息

时间:2021-03-23 09:52:18

标签: python discord discord.py

我一直在尝试制作一个程序,该程序可以运行不和谐机器人,并在我按下右箭头键时让它运行 - 机器人会向特定频道发送消息,但我收到错误消息,但我没有不知道该怎么做。

import discord, os, pyperclip
from pynput import keyboard

client = discord.Client()
with open('token.txt', 'r') as f:
    token = f.readline()


def on_press(key):
    if key == keyboard.Key.right:
        myChannel = client.get_channel(763382459504132136)
        myChannel.send("sent to specific channel")

@client.event
async def on_ready():
    print(f"Logged in as {client.user}")

with keyboard.Listener(on_press=on_press) as listener:
    client.run(token)
listener.join()

这是我的终端中弹出的内容:

以巴库身份登录#8196 C:\Users\Venyl\Desktop\VS CODE\Code 2020\discordbot\discordbot.py:12: RuntimeWarning: coroutine 'Messageable.send' was never awaited myChannel.send("sent to specific channel") 运行时警告:启用 tracemalloc 以获取对象分配回溯

1 个答案:

答案 0 :(得分:0)

我将我的部分代码从 myChannel.send("sent to specific channel") 更改为 client.loop.create_task(myChannel.send('sent to specific channel')) 并且它有效。