How can I solve the "name 'bot' is not defined" error? - python

Guys I'm making a discord bot but i have an error it says "name 'bot' is not defined". Can anyone help?
Thanks
import discord
from discord.ext import commands
from discord.ext.commands import has_permissions
client = commands.Bot(command_prefix = ';')
#client.event
async def on_ready():
print("{0.user} logged in as".format(client))
#bot.command(pass_context=True)
#has_permissions(administrator=True)
async def help(ctx):
await ctx.send('Help')
client.run('TOKEN')

You need to either use Client or Bot to instance your bot.
In your code you are instancing the bot as Client, but you are trying to create a command using #bot.command().
You need to use either #client.command() or #commands.command().

Related

Why is not my discord code working? When i run the progam, it gives an error saying: TypeError: 'module' object is not callable

Here is my code:
import discord
from discord.ext import commands
bot = discord.bot(command_prefix="!", help_command=None)
#bot.event
async def on_ready():
print(f"Bot logged in as {bot.user}")
bot.run("TOKEN")
The error i am getting:
File "c:\Users\user\OneDrive\Desktop\coding\discord\server bots\test.py", line 6, in <module>
bot = discord.bot(command_prefix="!", help_command=None)
TypeError: 'module' object is not callable
I haven't coded in a while and i just got back to coding. I tried a simple discord.py program but it didn't seem to work. Is there any way i could fix this?
yeah, you need to specify intents
here is the code to it works
import discord
from discord.ext import commands
intents = discord.Intents.all()
bot = commands.Bot(command_prefix="!", help_command=None, intents = intents)
#bot.event
async def on_ready():
print(f"Bot logged in as {bot.user}")
#bot.command()
async def hello(ctx):
await ctx.send(f"hi {ctx.author.mention}")
bot.run("token")
The syntax is commands.Bot() with from discord.ext import commands before (which is your case).
Also don't forget to specify the intents.

Attribute error "command" object has no attribute "role"

Im making a discord bot to role people with a simple command but I keep running into the same two issues. discord has no attribute role or member, any help is greatly appreciated.
from discord.ext import commands
from keep_alive import keep_alive
from discord.utils import get
token = "not gonna show ofc"
client = commands.Bot(command_prefix = "$")
#client.event
async def on_ready():
print("bot is ready")
#client.command()
async def test(ctx, role: discord.Role, user: discord.Member):
if ctx.author.guild_permissions.administrator:
await user.add_roles(role)
await ctx.send(f"given {role.mention} to {user.mention}")
keep_alive()
client.run(token)
Is this your full Code?
if yes you should import discord, then your code should work just fine.

Discord Bot Python - Changing Bot's nickname

I'm trying to get the bot's name to change every time it runs. I've check the discord.py docs but nothing there has been of use and none of them throw up any errors. Any ideas?
Has perms 'admin' and 'change nickname'
import discord
from discord.ext import commands
from discord.ext.commands import bot
bot = commands.Bot(command_prefix=PREFIX)
bot.remove_command('help')
#bot.event
async def on_ready():
await bot.user.edit(nick="New Nickname")
bot.run(TOKEN)
You must have the member object of your bot to change your nickname as nicknames are done in guild's. Then you must edit it.
#bot.event
async def on_ready():
for guild in bot.guilds:
await guild.me.edit(nick="new nickname")

Why errors in my cogs won't show in console in pycharm

Hello everyone i was making a bot using discord.py rewrite on pycharm but once there was a error on one of my cogs but the console didn't show any error and it didn't send any message so I had to search a lot about the error. I want to know why its like that.
My main code of the bot is:
import discord
from discord.ext import commands
client = commands.Bot(command_prefix="your prefix")
client.load_extension("extension name")
#client.event
async def on_ready():
print("bot is ready")
client.run('your token
I'm not sure why it doesn't but google will sure help!
You forgot to end the string at client.run so here's the correct code.
import discord
from discord.ext import commands
client = commands.Bot(command_prefix="your prefix")
client.load_extension("extension name")
#client.event
async def on_ready():
print("bot is ready")
client.run('your token')

Python Discord bot error

I am am making a discord bot in python.
In the output it says that I have 4 errors.
I do not understand why.
I have installed discord.py and I am using Python 3.5.
Any ideas as to what is going on?
Errors:
E0611:No name 'exe' in module 'discord'
E0602:Undefined variable 'message'
E0401:Unable to import 'discord.exe.commands'
E0602:Undefined variable 'message'
Code:
import discord
from discord.exe.commands import Bot
from discord.ext import commands
import asyncio
import time
Client = discord.Client()
client = commands.Bot(command_prefix = ";")
#client.event
async def on_ready():
print("The bot is sailing on the seven seas")
#client.event
async def on_message(maker):
if message.content == "cookie":
await client.send_message(message.channel, ":cookie:")
client.run("1NDE2--CHANGED--L24CMo"))
First error: You imported discord.exe; its called discord.ext
Second error: You assigned the message argument to the name maker in the on_message declaration. Change it to on_message(message) to get the variable

Categories