Discord bot not coming online, possible certificate error? - python

I wanted to try my hand at making a discord bot however I'm running into an issue where my bot will not come online. I've tried 2 different codes I've found from internet tutorials but neither of them will make the bot appear online or do anything. I've seen someone ask a similar question on here using a similar code to what I have implemented and was currently working with and said that their issues were coming from a certificate error (Discord Bot not coming online) but I'm not sure how I would go about resolving it. For context I'm using Python 3.9 on PyCharm and my code looks like this (I have used an actual token but I've heard its not good to give that out) :
import discord
from discord.ext import commands
client = commands.Bot(command_prefix='.')
#client.event
async def on_ready():
print('bot is ready')
client.run('TOKEN')
Please ask if you need any more information that what I've already provided.
edit1:pycharm says that the import 'import discord' is unused is that why im having issues?
edit2:I have tried using the following command to get the bot to do something but it is still not working-
#client.event
async def ping(ctx):
await ctx.send('pong!')

Related

Discord bot in Python - no commands are working

I want to create a bot that will respond to commands with arguments. I have gotten it to respond to strings in messages, but when i try to make commands working, the bot seems not to react at all. I have studied the documentation, put a lot of effor into trying to find the answer on the internet, but no help. My code can be the exact same as someone else's on the internet, but the bot never responds, while still responding to messages. I wonder if there's something really basic that I'm missing about how the commands are supposed to work.
I have imported the right library, I use the exact same syntax many people are sharing on the internet, yet the commands just don't do anything. I use replit.com which is a browser IDE.
This snippet of code is like half the code from the bot, he's quite dumb so far. Everything i tried other than commands has worked so far. I usually also don't work with Python, but i just followed the syntax i saw in tutorials to not make mistakes. When i run this, the bot logs in successfully, then if i send !tst the bot just doesn't do anything.
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
#bot.command(name='tst')
async def test(ctx):
await ctx.send('testt')
I mainly used this tutorial: https://realpython.com/how-to-make-a-discord-bot-python/#how-to-make-a-discord-bot-in-python
Also the library documentation: https://discordpy.readthedocs.io/en/stable/index.html
import discord
import os
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
#bot.command(name='tst')
async def test(ctx):
await ctx.send('testt')
client = discord.Client()
#client.event
async def on_ready():
print ('Successful login as {0.user}'.format(client))
client.run(os.getenv('TOKEN'))
I didn't want to post the whole thing, but it's really nothing special so here it goes. for a moment i thought that might be the solution, but even if i clear it of all message related code, it still does nothing. Could it be conflicting with the on_ready message? That one just goes to the console on the IDE website..
Based on the full code:
You shouldn't use both discord.Client and commands.Bot, because you are defining two different bots, but run only one of them. commands.Bot extends the discord.Client. Think about commands.Bot as a subclass of discord.Client. Everywhere where you've used client before, now you can use bot.
The code you posted would run correctly like this:
import discord
import os
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
#bot.command(name='tst')
async def test(ctx):
await ctx.send('testt')
#bot.event
async def on_ready():
print ('Successful login as {0.user}'.format(bot))
bot.run(os.getenv('TOKEN'))
Original answer:
(it might be a solution to someone else looking for it)
Are you using the on_message event? That prevents the command processor from getting the messages. You have to call the command processor manually, like so:
#bot.event
async def on_message(message):
# Do your stuff
await bot.process_commands(message)
According to the documentation you should call this function when you override the on_message event.
By default, this coroutine is called inside the on_message() event. If you choose to override the on_message() event, then you should invoke this coroutine as well.

Why won't my on_connect() print the message?

I have a code that's simple as this
bot = discord.Client()
#bot.event
async def on_connect():
print('Hello!')
however, when i run the code it won't print Hello! and it doesn't give any error message, and the bot itself did not even turn online
i can't solve it myself since i couldn't find any info online, or it might be that im just dumb ._.
It's not on_connect, instead, try using on_ready
Your code you have provided also seems to refer to the old async version, instead of rewrite, running your bot and defining it should be:
bot = commands.Bot(command_prefix = prefix)
Your bot also may not be online as you might of missed to actually run your bot via it's token and declaring it.
bot.run('token')
Also make sure you have imported Discord's modules, otherwise everything should be working. It's best if you start by reading and learning about Discord's documentation guide, as this provides the simple, basic and crucial information in setting up your bot, to avoid these small errors. A guide can be found here:
https://discord.com/developers/docs/intro
import discord
import discord.utils
from discord.ext import commands, tasks
#client.event
async def on_ready():
print('Hello!')

Discord.py Program Ends Instantly

I am trying to program a Discord bot in Python using the discord.py module.
When I run the code, it doesn't say anything, waits a little (probably importing/connecting stuff) and then closes - or in IDLE, >> appears.
There is no problem with the code, as it doesn't give any errors. Plus, if I run a command out of on_ready() it works as expected.
So the problem isn't because of not being able to connect to the bot or Discord, is it? Perhaps the asnyc function doesn't work, or the interpreter doesn't recognize it?
Here is the basic form of the main code:
import discord
token = 'blahblah'
client = discord.Client()
#client.event
async def on_message(message):
if message.content=="some input":
msg = 'some output'
await message.channel.send(msg)
#client.event
async def on_ready():
print("All works fine!")
client.run(token)
Since you mention something about it being an issue with async I shall leave an answer that I gave another user about async. https://stackoverflow.com/a/63005581/13950887
Another issue I had recently was due to discord updating their website from discordapp.com and discord.gg to discord.com, a way I found to resolve this was to edit a bunch of the files, one thing I will say is that I wouldn't recommend doing this unless you know what you are doing.

How to fix discord.py bot hosted by Heroku not going online?

I'm trying to get my discord.py bot hosted for free, and I heard Heroku is a good option. My dyno is working perfectly, and it's switched on, but when I deploy, my bot does not go online. I have no errors, it says it's deployed, but my bot never goes online.
I have tried regenerating the bot token, and putting it in my Heroku app, but it still won't work.
import discord
from discord.ext import commands
from discord.ext.commands import bot
import asyncio
import requests
import os
description = '''EchoBot by EchoNoahGaming'''
bot = commands.Bot(command_prefix='-', description=description)
#bot.event
async def on_ready():
print('Logged in as')
print(bot.user.name)
print(bot.user.id)
print('------')
#bot.command()
async def announcement(ctx, *, args):
"""Announcement command!"""
embed=discord.Embed(title="Announcement", description=args, color=0x7700aa)
embed.set_footer(text="By EchoNoahGaming")
await ctx.send("#everyone", embed=embed)
client.run(str(os.environ.get('BOT_TOKEN')))
That is the bot code.
I expected the bot to come online, because there were no errors, but it didn't, and it stayed offline.
The Procfile code is
worker: python3 bot.py
The requirements.txt content is
discord
asyncio
If you need anything else, tell me. The GitHub link is https://github.com/EchoNoahGaming/echobot/blob/master/ but i'll post any files on here so it's easier.
From the discussion in the comments, OP and I figured out what went wrong:
There is a typo on the last line: client.run should be bot.run
The requirements.txt file should be changed to
discord.py
requests
This is because requests isn't part of the Python standard library whereas asyncio is part of the standard library.
Fixing the above seemed to have solve the issues.
An auxiliary code review would be to remove redundant lines, such as
from discord.ext.commands import bot
(which gets overwritten by bot = Bot(...) anyway).
The asyncio and requests modules weren't used in the code, but I'll give you the benefit of the doubt and assume you're planning on using them. If not, remove them from your code (and, if applicable, from requirements.txt). This saves time in building the bot.

Python - Send Messages Upon User Joining

So, I am trying in the latest version of Discord.Py and Python 3.6.4 to detect when a user joins, it will send a message to them welcoming them. My code is as follows:
#client.event
async def on_member_join(member):
await client.send_message(member,"Welcome!")
This code doesn't seem to work, I have looked up already on how others did it but the code is outdated, couldn't find anything on the API either on how I would do this. Any help is appreciated!
MAJOR EDIT: Okay, so the code works fine but I'm getting a 403 Permissions Error. The Bot was re-invited with FULL PERMISSIONS and in server has FULL PERMISSIONS. So I have no idea why this is happening. The bot can send messages anytime anywhere but not specifically under the on_memer_join(member) ... why?
discord.py is going through a bit of a metamorphosis at the moment. There's the "async" branch, with version number 0.16, and there's the "rewrite" branch, version number 1.0, that isn't quite finished but many have started using anyways. To find out which you're using, just try running
import discord
print(discord.__version__)
The code you posted looks good for the "async" branch, so I suspect you're on the "rewrite" branch. Try
#client.event
async def on_member_join(member):
await member.send("Welcome!")
The documentation for the rewrite branch is here
For the Rewritten version of Discord.py, You need to enable Intents
intents = discord.Intents.all()
client = Bot.commands(command_prefix = '', intents = intents)
async def on_member_join(member):
await member.send('Welcome!')
You must enable intents in your bot application as well.
Sorry this is a late comment.

Categories