I've been trying to make a program that would run a discord bot and make it so whenever I press the right arrow key - the bot would send a message to a specific channel, but I get an error and I don't know how to do this.
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()
Here is what pops up in my terminal:
Logged in as Baku#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") RuntimeWarning: Enable tracemalloc to get the object allocation traceback
async
andawait
statements – mousetailawait
such statements, or useasyncio.create_task
to run it in the background – mousetaildef on_press
and await beforemyChannel.send
I get the same error but with 'on_press' instead of 'Messageable.send' and there's no "myChannel.send("sent to specific channel")" – Venylasyncio.create_task
though be sure to store the task since it will be cancelled if garbage collected – mousetail