Python discord bot closes/crashes immediately after opening - python

I'm trying to make a discord bot with discord.py for my discord server, I have extremely basic code that should get the bot online but instead just opens and then crashes.
import discord
#client.event
asnyc def on_ready():
print 'bot read`enter code here`y'
client.run('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx')
I do have the token normally I just didn't want to leak. I appreciate all the help.

I found a nice tutorial to create a discord bot in python. Therefore you can create a connection with the following code:
import os
import discord
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
client = discord.Client()
#client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
client.run(TOKEN)
And I would suggest to rename the file from 'discord' to 'bot' or something else, so there are no conflicts with the import.

Related

Discord Python Bot only reacts on DM message

Dear Community I tried my best but I cant find out why the bot only reacts on dm messages. Yesterday evening the bot worked fin and reacted to messages in a Discord but now he only reacts to dm´s. This code is just a short example of the on_message function I´m using but I cant find an alternative to on_message.
from timeit import repeat
import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
from datetime import datetime
import time
import sqlite3
from discord.message import Message
bot = commands.Bot(command_prefix='!')
PREFIX = '!'
#bot.event
async def on_message(message):
if message.content.startswith(PREFIX):
if message.content == (PREFIX + "test"):
await message.channel.send("test")
#bot.event
async def on_ready():
print("GuMo!")
bot.run("Token")```
I think that this probably has to do with the "intents" permissions that are being added to discord bots. Try
bot = commands.Bot(command_prefix='!', intents=discord.Intents.all())
and also give the bot the message content intent on the developer portal https://discord.com/developers/applications
If you install this:
pip install -U git+https://github.com/Rapptz/discord.py
Your bot will only react to messages send as DM.

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)

Discord.py bot status not showing

So, when i was trying to add a status to my discord python bot, it wouldn't show up on discord, when running the code no errors would show up, it's just that the status wouldn't show itself.
#Connection to discord
#client.event
async def on_ready():
print({client.user}, 'has connected to Discord!')
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name='~help'))
If anyone knows a fix to this please help.
import discord
import os
import config
import sys
client = discord.Client()
#client.event
async def on_ready():
print({client.user}, 'has connected to Discord!')
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name='~help'))
client.run(config.token) #imports token from the config.py
I tried your code in my test bot and it was working totally fine. I have attached the screenshots below and the code above. Not sure why it wouldn't work for you. [This is my entire code and has nothing extra attached in between or at the end].
Note: import config has my token (so it isn't a necessary import)

python discord bot- just can't get it online (just a simple question)

I was trying to make my bot go online but it never worked I just used these 3 lines of code:
import discord
client = discord.Client()
client.run('token')
Maybe this will help you: Creating a Discord Connection
It looks like you need to load environment variables with the os.getenv() function
You have to create a connection to discord
# bot.py
import os
import discord
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
client = discord.Client()
#client.event
async def on_ready():
print(f'{client.user} has connected to Discord!')
client.run(TOKEN)
Here, you’ve created a Client and implemented its on_ready() event handler, which handles the event when the Client has established a connection to Discord and it has finished preparing the data that Discord has sent, such as login state, guild and channel data, and more.

Why errors in my cogs won't show in console in pycharm

Hello everyone i was making a bot using discord.py rewrite on pycharm but once there was a error on one of my cogs but the console didn't show any error and it didn't send any message so I had to search a lot about the error. I want to know why its like that.
My main code of the bot is:
import discord
from discord.ext import commands
client = commands.Bot(command_prefix="your prefix")
client.load_extension("extension name")
#client.event
async def on_ready():
print("bot is ready")
client.run('your token
I'm not sure why it doesn't but google will sure help!
You forgot to end the string at client.run so here's the correct code.
import discord
from discord.ext import commands
client = commands.Bot(command_prefix="your prefix")
client.load_extension("extension name")
#client.event
async def on_ready():
print("bot is ready")
client.run('your token')

Categories