ChiMC
ChiMC

Reputation: 15

Discord bot doesn't work while online, bot running

I was starting to code a discord bot in Python. But now I struggle with something pretty big: The bot doesn't send the messages

import discord
from discord.ext import commands

client = commands.Bot(command_prefix='!')
general_channel = client.get_channel(798136768120881165)
join_channel = client.get_channel(798194832442130444)

@client.event
async def on_member_join(ctx, member):
    await ctx.join_channel.send(f"Welcome to my server! <@userid>")

@client.event
async def on_message(context):
    if message.content == "What is this?":
        
        WhatEmbed = discord.Embed(title="This is a TestBot", description="It's my first bot ever made!", color=0x00ff00)

    await context.message.channel.send(embed=WhatEmbed)

@client.command(aliases=['help'])
async def help(message):

    helpEmbed = discord.Embed(title="Help", description=None, colour=discord.Colour.gray())

    await message.channel.send(helpEmbed=helpEmbed)

The bot is running and it is online

Upvotes: 0

Views: 101

Answers (1)

Łukasz Kwieciński
Łukasz Kwieciński

Reputation: 15689

You messed up your code a bit.

  • on_message doesn't take the Context manager, it takes message
  • The help command doesn't take message as the argument, it takes Context
  • on_member_join only takes a single argument, member

You should also add client.process_commands at the end of the on_message event and enable some intents.

import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.members = True

client = commands.Bot(command_prefix='!', intents=intents)

@client.event
async def on_ready():
    await client.wait_until_ready()
    print(f"Bot is online")


@client.event
async def on_member_join(member):
    join_channel = client.get_channel(798194832442130444)
    await join_channel.send(f"Welcome to my server! <@userid>") # if you want to mention the member use `member.mention`


@client.event
async def on_message(message):
    if message.content == "What is this?":
        WhatEmbed = discord.Embed(title="This is a TestBot", description="It's my first bot ever made!", color=0x00ff00)
        await message.channel.send(embed=WhatEmbed)

    await client.process_commands(message)


@client.command(aliases=['help'])
async def help(ctx):
    helpEmbed = discord.Embed(title="Help", description=None, colour=discord.Colour.gray())
    await ctx.send(helpEmbed=helpEmbed)

I enabled intents.members cause you have the on_member_join event, remember to also enable them in the developer portal

Reference:

Upvotes: 2

Related Questions