3

I've been developing a discord bot for the past month, I've been doing commands using on_message listeners, which according to alot of people online isn't very ideal, so I decided to take it to the next step and do it properly, I looked at the documentation and came up with this basic code:

import discord #import all the necessary modules
from discord import Game
from discord.ext import commands

client = discord.Client() #define discord client
bot = commands.Bot(command_prefix='!') #define command decorator

@bot.command(pass_context=True) #define the first command and set prefix to '!'
async def testt(ctx):
    await ctx.send('Hello!!')

@client.event #print that the bot is ready to make sure that it actually logged on
async def on_ready():
    print('Logged in as:')
    print(client.user.name)
    await client.change_presence(game=Game(name="in rain ¬ !jhelp"))

client.run(TOKEN) #run the client using using my bot's token

Before going through the pain of implementing all of my commands to this new way of doing things, I wanted to test it so I know that it works for sure and sadly, it didn't, I went through a bunch of posts and documentation again but I couldn't spot what I did wrong, the bot logs in fine to the proper user and I just can't seem to figure out and understand what the problem is, I am probably missing something obvious yet again

Could anyone help, that would be great, thank you!

Myronaz
  • 183
  • 1
  • 1
  • 10

1 Answers1

8

By saying client = discord.Client() you make an discord client which will be online of course (If it isn't even online tell me) and you made an discord bot with bot = commands.Bot(command_prefix='!'). The discord bot won't run, only the discord client will because you only did run the client with client.run(TOKEN). So a quick preview of what your code should be and would hopefully work:

import discord #import all the necessary modules
from discord import Game
from discord.ext import commands

bot = commands.Bot(command_prefix='!') #define command decorator

@bot.command(pass_context=True) #define the first command and set prefix to '!'
async def testt(ctx):
    await ctx.send('Hello!!')

@bot.event #print that the bot is ready to make sure that it actually logged on
async def on_ready():
    print('Logged in as:')
    print(bot.user.name)
    await bot.change_presence(game=Game(name="in rain ¬ !jhelp"))

bot.run(TOKEN) #run the client using using my bot's token
  • Unfortunately, this doesn't seem to work, the bot logs in but doesn't respond, for testing sake's it responds perfectly to a on_message listener, does this code work for you? – Myronaz Dec 30 '18 at 22:01
  • 2
    @Myronaz See [Why does on_message stop commands from working?](https://stackoverflow.com/questions/49331096/why-does-on-message-stop-commands-from-working) – Patrick Haugh Dec 30 '18 at 22:06
  • Oh I see, I still need an on_message listener for something so this was useful Thanks alot for helping me figure this out! – Myronaz Dec 30 '18 at 22:21
  • No problem, and I personally use on_message with my discord bots –  Dec 31 '18 at 13:26