Discord command bot doesn't responde (python) - python

i have a problem with my discord bot: when i type on discord $ping he doesn't response me pong and i don't know why, i just check the bot have the admin role so he can write, i'm using VsCode and he didn't give me nothing error.
Here is the code
import discord
from discord.ext import commands
import requests
import json
import random
client = discord.Client()
bot = commands.Bot(command_prefix='$')
#bot.command()
async def ping(ctx):
await ctx.channel.send("pong")
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
client.run("XXXXXXXXXXXXXXXXXXXXXXX")

The problem is, that you are defining the command with bot.command, but you only do client.run. To fix this, either choose a client, or a bot, but not both, like this if you choose bot for example:
import discord
from discord.ext import commands
import json
import random
bot = commands.Bot(command_prefix='$')
#bot.command()
async def ping(ctx):
await ctx.channel.send("pong")
#bot.event
async def on_ready():
print('We have logged in as {0.user}'.format(bot))
bot.run(Token)
Also don't use requests, since it's blocking.

Related

Discord bot won't go online

I've just started learn python and my discord bot won't go online. it just said
" Process exit with exit code 0". And there's no error with the code.
here's my code. enter image description here
Add await client.process_commands(ctx) and #client.event don't need parentheses(). Use the code given below:
#client.event
async def on_message(ctx):
if ctx.author == client.user:
return
await client.process_commands(ctx)
Use this entire code if it still not working:
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix="!")
#bot.event
async def on_ready():
print('We are logged in!')
#bot.event
async def on_message(message):
if message.author==bot.user:
return
await bot.process_commands(message)
#bot.command()
async def ping(message) :
await message.channel.send("Pong!!")
bot.run("TOKEN")
You should try this instead
import os
import discord
from discord.ext import commands
discord_token = "Your Token"
client = discord.Client()
bot = commands.Bot(command_prefix="s!")
#bot.event
async def on_ready():
print("running")
bot.run(discord_token)
#bot.command()
async def on_ready():
print("running")
bot.run(discord_token)
and the output should be running
You should try this instead
import os
import discord
from discord.ext import commands
discord_token = "Your Token"
client = discord.Client()
bot = commands.Bot(client_prefix="!")
#client.command()
async def on_ready():
print("running")
bot.run(discord_token)
The code you gave is wrong, this is correct:
import os
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix="!") # you don't need an extra discord.client Bot is enough
token = os.getenv("TOKEN")
#you should not run the program here you should run it at last
#bot.event #no need for brackets
async def on_ready():
print("running")
#bot.event
async def on_message(ctx):
if ctx.author == client.user:
return
#bot.command(name="span",description="YOUR DESCRIPTION HERE") # it is command and not commands
async def span_(ctx,amount:int,*,message):
for i in range(amount):
await ctx.send(message)
bot.run(token) #you should run the bot now only and this will work perfectly!
You can find the the documentation for discord.py here.

Discord bot to send text messages with image from built in command (Python; Can redo in another language)

I've been working on trying to build a discord bot for a server that you could for example do "!publish [text + upload]" this would then send whatever you've typed (plus the uploaded image) from the bot in the current channel. I had had sqiggly code I was doing in Python off a new tutorials but so far nothing has stuck.
What I've got below is an attempt at having it send an image from the bot on command as a start however it hasn't even functioned this far. If anyone would be able to help me fix this up to get the desired result that would be great, I'm happy to switch up languages if that's easier. So far I've got it repeating text from command (See: $hello the bot will send hello! back. I'll be keeping that command for further use but haven't been able to get anything with an image extension). Have tried both file images and url; tried all the options on discord documentation without success.
import discord
import os
from replit import db
from discord.ext import commands
from discord.ext.commands import bot
import asyncio
import datetime as dt
client = discord.Client()
bot = commands.Bot(command_prefix='!')
#bot.event
async def on_ready():
print('Connected!')
#client.event
async def on_ready():
print('Hello world {0.user}'
.format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('$hello'):
await message.channel.send('hello!')
#bot.command(pass_context=True)
async def test(ctx):
await ctx.send('Working!', file=discord.File('image.png'))
client.run(os.environ['TOKEN'])
First tutorial I followed had me using replit to code this, store data and keep it cloud-base operational even when the computer wasn't active. The bot is functioning at this time just using the $hello command and anything similar. Nothing with images.
First of all you should only use either client or Bot not both, bot is the better option since it has commands and events. here is your code organized using commands.Bot(command_prefix='!')
import discord
import os
from replit import db
from discord.ext import commands
import asyncio
import datetime as dt
bot = commands.Bot(command_prefix='!')
#bot.event
async def on_ready():
print(f'Connected! {bot.user}')
#bot.event
async def on_message(message):
if message.author == bot.user:
return
#code here
# !hello
#bot.command()
async def hello(ctx):
await ctx.send('Hey!')
# !test
#bot.command()
async def test(ctx):
await ctx.send('Working!', file=discord.File('image.png'))
bot.run(os.environ['TOKEN'])
You have defined two instances and only ran client. That is why !test did not work

Adding cooldowns to discord.py

I've been researching methods to add cooldowns to only specific commands in my bot. I am unable to find anything that works, and I'm unsure how #commands.cooldown(rate=1, per=5, bucket=commands.BucketType.user) would be implemented into this code.
import discord
import random
from pathlib import Path
from discord.ext import commands
from discord.ext.commands.cooldowns import BucketType
client = discord.Client()
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('#help'):
*command goes here*
if message.content.startswith('#gamble'):
*command goes here*
client.run('TOKEN')
I suggest you using commands.Bot as it has all the functionality that discord.Client has + a full command system, your code rewrited would look like this:
import discord
from discord.ext import commands
# You should enable some intents
intents = discord.Intents.default()
bot = commands.Bot(command_prefix="#", intents=intents)
bot.remove_command("help") # Removing the default help command
#bot.event
async def on_ready():
print(f"Logged in as {bot.user}")
#bot.command()
async def help(ctx): # Commands take a `commands.Context` instance as the first argument
# ...
#bot.command()
#commands.cooldown(1, 6.0, commands.BucketType.user)
async def gamble(ctx):
# ...
bot.run("TOKEN")
Take a look at the introduction

Discord.py set status (client)

Hello,
How i set status to the bot?
import discord
import os
import asyncio
from keep_alive import keep_alive
from discord.ext import commands
client = discord.Client()
(I dont want change to bot.event because i have RolePlay bot and with bot.event and bot is spamming messages. Help me how to set status with client.event.)
It should be something like this if you want to use client:
import discord
client = discord.Client()
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
await client.change_presence(activity=discord.Game(name="a game")) # Here we change it
#client.event
async def on_message(message):
# Your code
client.run('your token here')

my commands dont work in my discord server

im trying to make a bot for my discord server, but all my commands dont work.
im using windows and pycharm to test and use the bot. i tried many different types but nothing works. im using python 3.7
import discord
from discord.ext import commands
import asyncio
from discord.ext.commands import Bot
Client = discord.Client()
client = commands.Bot(command_prefix='.')
#client.event
async def on_ready():
print("bot is active")
#client.command()
async def ping(ctx):
await ctx.send('pong')
await print("pong")
it doesn't crash or give an error, it just doesn't do anything in the command
Try this:
import discord
from discord.ext import commands
TOKEN = YOUR_TOKEN_GOES_HERE
client = commands.Bot(command_prefix = '.')
#client.event
async def on_ready():
print("Powering up the bot")
#client.event
async def on_message(message):
print("A new message!")
await client.process_commands(message)
#client.command()
async def ping(ctx):
await ctx.channel.send("Pong!")

Categories