Cannot send message from console to discord - python

So I'm trying to send a message from the console to discord as if it were a /say command. Specifically I'm trying to input the channel id and a message, and get the bot to send it.
But when I run it no error appears, the bot goes online, and nothing is sent to the text channel
Here is my code:
import discord
from discord.ext.commands import Bot
TOKEN = open('token.txt').readline()
client = discord.Client()
bot = discord.ext.commands.Bot
channel_entry = input('ID: ')
msg_entry = input('Mensagem: ')
#client.event
async def send_channel_entry():
channel = client.get_channel(channel_entry)
await channel.send(msg_entry)
bot.run(TOKEN)

Of course nothing happens, because send_channel_entry is never called. Have you tried calling the command or activating it via Discord to see if it works?

Related

Discord Bot fails to execute simple commands | Python

I am trying to build a Discord bot; which tells you about the weather condition. But no matter what I did I was unable to execute even the simplest commands such as:
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix="!")
#bot.command()
async def test(ctx, arg):
await ctx.send(arg)
client = discord.Client()
client.run(token)
I just do not get it on Discord side I enter the command "!test hello" to the chat screen and nothing happens. Please help thanks !
The problem is, that you first define a bot using bot = commands.Bot(command_prefix="!") then add the command to the bot, but then create a new client using client = discord.Client() and then run the client.
The problem here is that, you never run the bot, which has the command, so instead of
client = discord.Client()
client.run(token)
use
bot.run(token)

Sending messages in discord.py #tasks.loop()

Goal:
I am simply trying to send a message to a discord channel from a #tasks.loop() without the need for a discord message variable from #client.event async def on_message. The discord bot is kept running in repl.it using uptime robot.
Method / Background:
A simple while True loop in will not work for the larger project I will be applying this principle to, as detailed by Karen's answer here. I am now using #tasks.loop() which Lovesh has quickly detailed here: (see Lovesh's work).
Problem:
I still get an error for using the most common method to send a message in discord using discord.py. The error has to have something to do with the await channel.send( ) method. Neither of the messages get sent in discord. Here is the error message.
Code:
from discord.ext import tasks, commands
import os
from keep_alive import keep_alive
import time
token = os.environ['goofyToken']
# Set Up Discord Client & Ready Function
client = discord.Client()
channel = client.get_channel(CHANNEL-ID)
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#tasks.loop(count=1)
async def myloop(word):
await channel.send(word)
#client.event
async def on_message(message):
msg = message.content
if msg.startswith('!'):
message_to_send = 'Hello World!'
await channel.send(message_to_send)
myloop.start(message_to_send)
keep_alive()
client.run(token)
Attempted Solutions:
A message can be sent from the on_message event using the syntax await message.channel.send('Hello World!). However, I just can't use this. The code is kept running online by uptimerobot, a free website which pings the repository on repl.it. When the robot pings the repository, the message variable is lost, so the loop would stop scanning my data in the larger project I am working on which is giving me this issue.
When using any client.get_* method the bot will try to grab the object from the cache, the channel global variable is defined before the bot actually runs (so the cache is empty). You should get the channel inside the loop function:
#tasks.loop(count=1)
async def myloop(word):
channel = client.get_channel(CHANNEL_ID)
await channel.send(word)

Python Discord bot delete users message

I am working on a discord bot in python3 (discord.py 1.3.3, discord 1.0.1) and I have a need to delete a user message, but I can't figure out how to call the coroutine properly.
I have looked at some other threads, and tried reviewing the documentation (and the discord.py docs) but I haven't been able to figure it out.
Here's what I'm testing with:
import discord
from discord.ext import commands
TOKEN = os.getenv('DISCORD_TOKEN')
bot = commands.Bot(command_prefix='!')
#bot.command(name='deleteme', help='testing command for dev use')
async def deleteme(ctx):
msg = ctx.message.id
print(f'DEBUG: message id is {msg}')
await msg.delete
# await ctx.delete(msg, delay=None) #nope
# await ctx.delete_message(ctx.message) #nope
# await bot.delete_message(ctx.message) #nope
# await command.delete_message(ctx.message) #nope
# await discord.Client.delete_message(msg) #nope
Running this returns the console debug message with an ID number, but the message isn't deleted. If I add a debug print line after await msg.delete it doesn't return. So this tells me where the script is hanging. That said, I still haven't been able to figure out what the proper command should be.
The bots server permissions include "manage messages"
In order to delete a message, you have to use the discord.Message object, for example, you would do:
await ctx.message.delete()
The delete() coroutine is a method of discord.Message

How to make a discord bot react to PAST private messages?

I made a simple discord.py bot that reacts whenver it is PM'd. However, if someone messages it while my bot is offline, it will not react. How can i make it display all messages received while it was online on startup?
Current code:
import discord
from discord.ext import commands
import asyncio
client = commands.Bot(command_prefix='.')
#client.event
async def on_message(message):
if message.guild is None and message.author != client.user:
send_this_console=f'{message.author}: \n{message.content}\n'
print(send_this_console)
await client.process_commands(message)
You'll need to use message history and track the last message sent.
To get when a message was created:
lastMessageTimestamp = message.created_at
You'll need to store this in whatever way you want when your bot is offline (for example with pickle) and do this:
async for message in user.history(after = lastMessageTimestamp):
print(message.content)

discord.py bot doesnt respond to Bot.commands()

Hello I am creating a discord bot i was trying to add single command but bot doesn't respond to any commands
something like
Bot = commands.Bot(command_prefix="!")
#Bot.commands()
async def ping():
print("Pong!")
this thing should respond when I type !ping in to discord client it should print pong in to terminal
but nothing nothing at all
I have tried Bot.add_command(ping) but it says command it's aleady registered i have no idea..
From what I've read from the discord.py docs you should code it like this:
Bot = commands.Bot(command_prefix="!")
#Bot.commands()
async def ping(ctx):
ctx.send("Pong!")
When you use 'print' you print the answer on your idle terminal. 'ctx.send' print the answer on discord chat. And every function on discord.py needs and argument.

Categories