Ask coding questions

← Back to all posts
Not defined
FunnySusMan420 (2)

So im making a discord bot but on row 41 its saying that the name message is not defined. How will i fix this and do i have any other wrong code?

Comments
hotnewtop
RYANTADIPARTHI (6001)

Solution

you actually have multiple errors. Here's the right code.

import discord
import random

client = discord.Client()


numbers_lol = [
  "**You got 25 Bamiel Bucks!**",
  "**You got 25 Bamiel Bucks and 1 Sus Banner!** ",
  "**You got 25 Bamiel Bucks and 1 Buff Banner!**",
  "**You got 25 Bamiel Bucks and 1 Epic Catfruit!**",
  "**You got 25 Bamiel Bucks and 1 Gold Catfruit!**",
  "**You got 30 Bamiel Bucks!**",
  "**You got 30 Bamiel Bucks and 1 Sus Banner!**",
  "**You got 30 Bamiel Bucks and 1 Buff Banner!**",
  "**You got 30 Bamiel Bucks and 1 Epic Catfruit!**",
  "**You got 30 Bamiel Bucks and 1 Gold Catfruit!**",
  "**You got 35 Bamiel Bucks!**",
  "**You got 35 Bamiel Bucks and 1 Sus Banner!**",
  "**You got 35 Bamiel Bucks and 1 Buff Banner!**",
  "**You got 35 Bamiel Bucks and 1 Epic Catfruit!**",
  "**You got 35 Bamiel Bucks and 1 Gold Catfruit!**",
  "**You got 40 Bamiel Bucks!**",
  "**You got 40 Bamiel Bucks and 1 Sus Banner!**",
  "**You got 40 Bamiel Bucks and 1 Buff Bannera!**",
  "**You got 40 Bamiel Bucks and 1 Epic Catfruit!**",
  "**You got 40 Bamiel Bucks and 1 Gold Catfruit!**",
  "**You got nothing,idiot**",
  "**The gods bless you,and you get 100 Bamiel Bucks!**"
  ]




@client.event
  async def on_ready():
    print('We have logged in as{0.user}').format(client)

@client.event
  def on_message(message):
    if message.content.startswith('!Bamiel'):
       async message.channel.send("numbers_lol")
       numbers_lol = [1, 2, 3]
       print(random.choice(numbers_lol) #any one of "1", "2" or "3"
    
client.run("ODEwNjExNzg1NzA2ODk3NDEw.YCmLDg.TsvaiHsC2ocZ6AKsCzbcs92f9zg")

like that.
**That should work**
DrHalloween (14)

I don't really know much about bots but this is what i've come up with (idk if it works though):

import discord
import random
from discord.ext.commands import Bot
from discord.ext import commands

client = discord.Client()

import discord
from discord.ext import commands

bot = discord.ext.commands.Bot(command_prefix = "your_prefix");

bot.run("your_token")

numbers_lol = [
"You got 25 Bamiel Bucks!",
"You got 25 Bamiel Bucks and 1 Sus Banner! ",
"You got 25 Bamiel Bucks and 1 Buff Banner!",
"You got 25 Bamiel Bucks and 1 Epic Catfruit!",
"You got 25 Bamiel Bucks and 1 Gold Catfruit!",
"You got 30 Bamiel Bucks!",
"You got 30 Bamiel Bucks and 1 Sus Banner!",
"You got 30 Bamiel Bucks and 1 Buff Banner!",
"You got 30 Bamiel Bucks and 1 Epic Catfruit!",
"You got 30 Bamiel Bucks and 1 Gold Catfruit!",
"You got 35 Bamiel Bucks!",
"You got 35 Bamiel Bucks and 1 Sus Banner!",
"You got 35 Bamiel Bucks and 1 Buff Banner!",
"You got 35 Bamiel Bucks and 1 Epic Catfruit!",
"You got 35 Bamiel Bucks and 1 Gold Catfruit!",
"You got 40 Bamiel Bucks!",
"You got 40 Bamiel Bucks and 1 Sus Banner!",
"You got 40 Bamiel Bucks and 1 Buff Bannera!",
"You got 40 Bamiel Bucks and 1 Epic Catfruit!",
"You got 40 Bamiel Bucks and 1 Gold Catfruit!",
"You got nothing,idiot",
"The gods bless you,and you get 100 Bamiel Bucks!"
]

@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))

await bot.send_message.channel.send("numbers_lol")

client.run("ODEwNjExNzg1NzA2ODk3NDEw.YCmLDg.TsvaiHsC2ocZ6AKsCzbcs92f9zg")

FunnySusMan420 (2)

@DrHalloween when i do that on row 42 it says indented block.do you know how to fix that?

DrHalloween (14)

@FunnySusMan420 What do you mean by indented block? Like, "expected an indented block" or "unexpected indented block" or something else?

FunnySusMan420 (2)

@DrHalloween expected an indented block

DrHalloween (14)

@FunnySusMan420 Oh that's simple to fix i think, i get that all the time. I would go to the beginning of the line and just type a space and i think it should work (sorry if that sounds a bit messy)

FunnySusMan420 (2)

@DrHalloween now on row 43 it says: SyntaxError: 'await' outside function.

DrHalloween (14)

@FunnySusMan420 What exactly are you trying to make the bot do?

DrHalloween (14)

@FunnySusMan420 (It's currently 11pm here rn so i'm gonna go sleep, i'll see if i can fix the code tomorrow)

DrHalloween (14)

@FunnySusMan420 So i've been fiddling around for a bit and i can't find the command so i call forth the gods cough@19winterspcough

Also if generally othing works, i have noticed the code in general is pretty buggy, so if you can't fix that error, this tutorial is pretty good: https://realpython.com/how-to-make-a-discord-bot-python/#how-to-make-a-discord-bot-in-python

19wintersp (1120)

@DrHalloween @FunnySusMan420 Here are the things which I find wrong at first:

  1. You're missing a colon after your if statement on line 43
  2. You are using await (main.py:44) in a function not marked as async
  3. Your Discord API token is written directly into your code; move this to a ".env" file
  4. Not an error, but you've imported random and aren't using it; did you want to send a random item of numbers_lol on line 44?
FunnySusMan420 (2)

@19wintersp thanks for all of this and to answer number 4,yes i did want to send a random item from numbers_lol on line 44

19wintersp (1120)

@FunnySusMan420 Since you have the random module already, you can use the choice function: it picks a random* item from a list:

import random

my_list = [1, 2, 3]

print(random.choice(my_list)) #any one of "1", "2" or "3"
DrHalloween (14)

@19wintersp How are you so good with programming? You know like, every language, every mistake, how?

19wintersp (1120)

@DrHalloween I'm still at an overall beginner level to be honest, but practice (and a lot of StackOverflow searching) really helps.

DrHalloween (14)

@19wintersp Ah, I would've assumed you took a programming course of some sort.

19wintersp (1120)

@DrHalloween I've taken one or two, but most of what I know now is either from experience, other code or the manual guide/reference. I learnt Python (my first language) from a book, actually.

19wintersp (1120)

@DrHalloween The best books are often the "guide to <language>" on the official website. That particular book was an introduction to programming; I've forgotten the name.

robowolf (434)

You have to make a new client event

@client.event
def on_message(message):
    if message.content.startswith('hi'):
       await message.channel.send('hi')

You would have to do something like that.

robowolf (434)

@robowolf Also, hide your token. If everyone can see it than anyone can 'hack' your bot. Create a .env file. Then in that file put TOKEN = 'token'. Then in your python file do

import os
token =os.getenv('TOKEN')
#stuff
client.run(token)