Discord Bot Command is not Showing - python

So I'm making a basic bot command that responds with what the player said, like doing !test code will make the bot respond with 'code'. For some reason, nothing happens when the command is run. I even put a print inside of it to see if it was actually being run, and it wasn't. Here's my code:
import discord
from discord.ext import commands
client = discord.Client()
bot = commands.Bot(command_prefix="!")
#client.event
async def on_ready():
print('Logged in as {0.user}'.format(client))
print("-"*16)
game = discord.Game("Discord")
await client.change_presence(status=discord.Status.online, activity=game)
#bot.command()
async def test(ctx, arg):
await ctx.send(str(arg))
client.run('token here')
Any help is appreciated.

try this out:
import discord
from discord.ext import commands
client = commands.Bot(command_prefix="!")
#client.event
async def on_ready():
print('Logged in as {0.user}'.format(client))
print("-"*16)
game = discord.Game("Discord")
await client.change_presence(status=discord.Status.online, activity=game)
#client.command()
async def test(ctx, *, arg):
await ctx.send(str(arg))
client.run('token here')
heres what you got wrong:
client = discord.Client()
bot = commands.Bot(command_prefix="!")
You had 2 separate handlers for the bot, if you use commands you only need the bot = commands.Bot(command_prefix="!") line, in this case you had the bot handler for commands but you were running client

The code below runs as expected when tested using Python3.7 ...
import discord
from discord.ext import commands
client = commands.Bot(command_prefix="!")
#client.event
async def on_ready():
print('Logged in as {0.user}'.format(client))
print("-"*16)
game = discord.Game("Discord")
await client.change_presence(status=discord.Status.online, activity=game)
#client.command()
async def test(ctx, *arg):
await ctx.send(str(arg))
client.run('token here')
Your code as posted has an await statement outside of a function
......
print("-"*16)
game = discord.Game("Discord")
await client.change_presence(status=discord.Status.online, activity=game)
.......
Also change
#bot.command()
async def test(ctx, arg):
TO:
#bot.command()
async def test(ctx, *arg):
For an explanation as to why you pass *arg and not arg:
args-and-kwargs

You can't run client and bot together if want bot to run the last line of code must be bot.run('your token') if you want client to run use client.run('your token')

Related

Why ctx.send(arg) doesn't send arg in Discord?

I just started making my own Discord bot, but even this simple test code won't work
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
#client.event
async def on_ready():
print(f'We have logged in as {client.user}')
bot = commands.Bot(command_prefix='?',intents=intents)
#bot.command()
async def test(ctx, arg):
await ctx.send(arg)
Both
message.content.startswith('$hello'):
and
await message.channel.send('Hello!')
work as expected, I have tried to change the command_prefix to other symbols, with no luck
I think your issue might be related to having both a discord.Client and a discord.Bot class instantiated together. You only need one or the other - not both. It's not shown but I'm taking a hunch that you're doing client.run(TOKEN) and therefore, the command isn't getting registered.
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='?',intents=intents)
#bot.event
async def on_ready():
print(f'We have logged in as {bot.user}')
#bot.command()
async def test(ctx, arg):
await ctx.send(arg)
bot.run(TOKEN)
This ran perfectly for me.

Discord bot not responding (python)

import discord
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('$hello'):
await message.channel.send('Hello!')
client.run('token is here')
I'm trying to create a discord bot and I'm trying to use this simple code to get started but whenever I type the command ($hello) in a server that the bot is in it doesn't respond. Any solutions?
I'm assuming you're new to discord.py which is fine, though writing code like this is a little counter-intuitive. Instead of registering it as an event, why not just register it as an actual command?
This is how it should be done:
from discord.ext import commands
client = commands.Bot(command_prefix='$') # This can always be changed
#client.event()
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#client.command()
async def hello():
await ctx.send('Hello')
client.run('token is here')

A Python discord bot not responding to a command

I tried making a discord bot and stumbled across an issue, the bot does not respond to commands and I have no clue what's wrong.
Here's my code
import discord
from discord.ext import commands
class MyClient(discord.Client):
async def on_ready(self):
print('Logged on as {0}!'.format(self.user))
await client.change_presence(activity=discord.Game(name="Python"))
async def on_message(self, message):
print('Message from {0.author}: {0.content}'.format(message))
await bot.process_commands(message)
client = MyClient()
client.run("Token Here")
bot = commands.Bot(command_prefix='!')
#bot.command()
async def test(ctx):
await ctx.send("Sup")
You are currently mixing a discord.Client with a commands.bot.
Discord.py provides multiples ways to creates bots that are not compatibles each other.
Moreover, client.run is blocking, and should be at the end of your script.!
import discord
from discord.ext import commands
class MyClient(commands.Bot):
async def on_ready(self):
print('Logged on as {0}!'.format(self.user))
await client.change_presence(activity=discord.Game(name="Python"))
async def on_message(self, message):
print('Message from {0.author}: {0.content}'.format(message))
await self.process_commands(message)
client = MyClient(command_prefix='!')
#client.command()
async def test(ctx):
await ctx.send("Sup")
client.run("...")
Note that you are not obligated to subclass the commands.Bot class given by the library.
Here is an other exemple that don't use subclassing:
import discord
from discord.ext import commands
client = commands.Bot(command_prefix='!')
#client.event
async def on_ready():
print('Logged on as {0}!'.format(client.user))
await client.change_presence(activity=discord.Game(name="Python"))
#client.event
async def on_message(message):
print('Message from {0.author}: {0.content}'.format(message))
await client.process_commands(message)
#client.command()
async def test(ctx):
await ctx.send("Sup")
client.run("...")
Using the second approach might be beneficial if you just started to learn dpy, and can work when you dont have a lot of commands.
However, for bigger projects, the first approach will help to organise your bot by using cogs for commands and events.

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 Joining Message is not working python

Hello i try a lot commands to fix it but the bot is doing nothing
Here is my Code. The File are bot.py
import discord
import os
from discord.ext import commands
class MyClient(discord.Client):
async def on_ready(self):
print('Logged on as {0}!'.format(self.user))
async def on_message(self, message):
print('Message from {0.author}: {0.content}'.format(message))
client = MyClient()
#client.event
async def on_ready():
print(f'{client.user.name} Test!')
###############
bot = commands.Bot(command_prefix="!")
#bot.event
async def on_guild_join(guild):
if guild.system_channel:
await guild.system_channel.send("Test")
##client.event
#async def on_member_join(member):
# await ctx.author.send("Welcome!")
##*#client.event
#async def on_member_join(member):
# await member.create_dm()
# await member.send(
# f'Hallo{member.name}, Willkommen'
# )
Why is it not working i try a lot Commands/models whatever.
I use Notepad++ to programming. Give it a better Programm to programming with Python? Maybe what for Beginners?
You cant use both discord.Client and commands.Bot, choose one of them.
commands.Bot is basically the same as discord.Client but with the extra commands feature
-> Docs
# so first remove the client part in your code
# and define the Bot | you can name it 'bot' or 'client'
# bot = commands.Bot(command_prefix="!")
# is the same as
# client = commands.Bot(command_prefix="!")
# also make sure to enable Intents
intents = discord.Intents.default()
intents.member = True
bot = commands.Bot(command_prefix="!", intents=intents)
#now you can add events
#bot.event
async def on_guild_join(guild):
if guild.system_channel:
await guild.system_channel.send("Test")
#bot.event
async def on_member_join(member):
await member.send("welcome!")
# and commands
#bot.command()
async def slap(ctx, member: discord.Member):
await ctx.send(f"{ctx.author} gave {member} a slap")
# at the very end run your bot
bot.run('BOT TOKEN')

Categories