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)
Related
My discord bot connects just fine, event on_ready is working with no problems. However when I call any of the commands declared with #bot.command, the bot doesn't recognize the message as a command (at least that's what I think it's happening).
Code:
import discord
import pickle
from discord.ext import commands
from discord.ext.commands import bot
token = ""
bot = commands.Bot(command_prefix='!', case_insensitive=True, intents=discord.Intents.default())
#bot.event
async def on_ready():
print('We have logged in as {0.user}'.format(bot))
#bot.command()
async def test(ctx):
print('received')
await ctx.send('msg received')
bot.run(token)
That's pretty much it, when I run it the bot connects just fine, but the !test command doesn't work.
Let me know if I didn't add important information, first time asking something here.
EDIT: It's working locally, it doesn't work when it's being hosted on heroku (bot connects and is online, doesn't recognize commands).
Requirements file:
git+https://github.com/Rapptz/discord.py
discord~=1.7.3
setuptools~=60.2.0
I have simplest python program of discord bot
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
#bot.event
async def on_message(msg):
print(msg.content)
bot.run('token')
And it prints just empty string. Before that I tried bot.command() but bot simply doesn't responds to it probably because message is empty so like there's no command. I saw this problem mostly occurs for selfbot clients but in my case client is bot. Would be glad for any help
You have to enable the message intents on https://discord.com/developers/applications
and need to pass them to your commands.Bot
bot = commands.Bot(command_prefix="!", intents=discord.Intents.all())
would be an example of how you can do that.
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)
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?
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.