-1

I am currently using python to code my first discord bot. While trying to write a command, I saw that I was unable to do this. This code:

import os
import random
from discord.ext import commands
from dotenv import load_dotenv

load_dotenv()
TOKEN = 'NzQxMjA2OTQzMDc4ODA5NjEy.Xy0Mwg.Shkr9hHvkn-y4l5ye1yTHYM3rQo'
client = discord.Client()
    
@client.event
async def on_member_join(member):
    await member.create_dm()
    await member.dm_channel.send(f'Hi {member.name}, welcome to my Discord server!')

pp = commands.Bot(command_prefix='!')

messages = ["hello", "hi", "how are you?"]
@pp.command(name = "swear")
async def swear(ctx):
    await ctx.send(rand.choice(messages))

client.run(TOKEN)

is not outputting anything in the discord application when I type !swear. The bot is online, and working normally for other codes such as:

@client.event
async def on_member_join(member):
    await member.create_dm()
    await member.dm_channel.send(f'Hi {member.name}, welcome to my Discord server!')

Here is the full code:

import discord
import os
import random
from discord.ext import commands
from dotenv import load_dotenv

load_dotenv()
TOKEN = (##my token spelled out)
client = discord.Client()

@client.event
async def on_ready():
    print('im 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('Hi there!')
        
    elif message.content.startswith("Is Ethan's mum gay?"):
        await message.channel.send("Yep, 100%!")
    
    elif message.content.startswith("What are you doing?"):
        await message.channel.send(f"Step {message.author}")
        
    elif 'happy birthday' in message.content.lower():
        await message.channel.send('Happy Birthday! 🎈🎉')
        
    
    
@client.event
async def on_member_join(member):
    await member.create_dm()
    await member.dm_channel.send(f'Hi {member.name}, welcome to my Discord server!')

pp = commands.Bot(command_prefix='!')

messages = ["hello", "hi", "how are you?"]
@pp.command(name = "swear")
async def swear(ctx):
    await ctx.send(rand.choice(messages))

client.run(TOKEN)

Does anyone know how I can fix my problem?

4
  • 1
    By pasting your token here, you have given everyone the password for your bot. Go refresh it right now! (Ps the token is supposed to go in a separate file.) Commented Aug 7, 2020 at 10:52
  • Do you know how to put it into a seperate file? I'm not sure what you are talking about... Commented Aug 7, 2020 at 11:02
  • Pretty sure that’s what dotenv is for stackoverflow.com/a/41547163/6083675. But you need to prioritize resetting your token. Commented Aug 7, 2020 at 11:33
  • as for me Client() and Bot() are two different methods to create bot - so you create two bots at the same time - and you can't use them together because you would need client.run(TOKEN) and pp.run(TOKEN) to run both at the same time. You should rather use only Bot() and @pp.event instead of @client.event Commented Aug 7, 2020 at 11:44

1 Answer 1

1

Client() and Bot() are two methods to create bot and using both at the same time is wrong.

Using Client() and Bot() you create two bots but they would need client.run(TOKEN) and pp.run(TOKEN) to run together - but it makes problem to start them at the same time and it can make conflict which one should get user message.

Bot() is extended version of Client() so you need only Bot() and use

  • @pp.event instead of @client.event
  • pp.run(TOKEN) instead of client.run(TOKEN)
  • pp.user instead of client.user

EDIT:

And you have to define pp before all functions. Like this

import random
from discord.ext import commands

TOKEN = 'TOKEN'

pp = commands.Bot(command_prefix='!')

@pp.event
async def on_ready():
    print('im in as {}'.format(pp.user))

@pp.event
async def on_message(message):
    if message.author == pp.user:
        return
 
    if message.content.startswith('hello'):
        await message.channel.send('Hi there!')
        
    elif message.content.startswith("Is Ethan's mum gay?"):
        await message.channel.send("Yep, 100%!")
    
    elif message.content.startswith("What are you doing?"):
        await message.channel.send(f"Step {message.author}")
        
    elif 'happy birthday' in message.content.lower():
        await message.channel.send('Happy Birthday! 🎈🎉')
    
@pp.event
async def on_member_join(member):
    await member.create_dm()
    await member.dm_channel.send(f'Hi {member.name}, welcome to my Discord server!')

messages = ["hello", "hi", "how are you?"]

@pp.command(name = "swear")
async def swear(ctx):
    await ctx.send(rand.choice(messages))

pp.run(TOKEN)
Sign up to request clarification or add additional context in comments.

2 Comments

When I use pp.event, the code responds with NameError: name 'pp' is not defined .
you have to define pp = commands.Bot(command_prefix='!') before all @pp.even - it is so obious that I didn't mentioned it .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.