discord bot bot not defined - python

import discord
from discord.ext import commands
import requests
import os
TOKEN = os.getenv("TOKEN")
#bot.event
async def on_message(message):
if message.content == "hello":
await message.channel.send("world")
bot.run(TOKEN)
when I start the bot in python I get the error that the bot is not defined
#bot.event
NameError: name 'bot' is not defined

you need to construct a bot first so like this
import discord
from discord.ext import commands
import requests
import os
bot = commands.Bot(command_prefix="!", intents=discord.Intents.all())
TOKEN = os.getenv("TOKEN")
#bot.event
async def on_message(message):
if message.content == "hello":
await message.channel.send("world")
bot.run(TOKEN)

You haven't made the bot yet. You are running bot.run() even though you don't have bot declared as a variable. To fix this make a discord.ext bot or a discord client.
import discord
from discord.ext import commands
import requests
import os
TOKEN = os.getenv("TOKEN")
bot = commands.Bot(command_prefix="your prefix here", intents=discord.Intents.default())
# If you want more intents, do discord.Intents.all()
# or call them manually with intents.message_content = True
#bot.event
async def on_message(message):
if message.content == "hello":
await message.channel.send("world")
bot.run(TOKEN)

Related

Discord bot not responding for commands

I try to make a discord bot on python but he not responding for any command.
How i can fix it?
import requests
import random
from discord.ext import commands
intents = discord.Intents.all()
Away = commands.Bot(command_prefix="$", intents=intents)
#Away.event
async def on_message(ctx):
print(ctx.content)
#Away.command
async def say(ctx):
await ctx.send(ctx.content)
Away.run("token")```

How can I kick all Discord members without a role?

I've setup a custom bot in Discord with presence and members, added it the server with admin permissions. These are the codes I tried running, but with no avail:
import discord
from discord.ext import commands
from discord.ext.commands import has_permissions
intents = discord.Intents.all()
bot = commands.Bot(command_prefix='.', intents=intents)
#bot.command()
#has_permissions(administrator=True)
async def kicknorole(ctx: commands.Context):
members = ctx.guild.members
for member in members:
if len(member.roles) == 1:
await member.kick()
print(member.name)
await ctx.reply('Done!')
bot.run('TOKEN')
and
import discord
from discord.ext import commands
from discord.ext.commands import has_permissions
intents = discord.Intents.all()
bot = commands.Bot(command_prefix='.', intents=intents)
#bot.command()
#has_permissions(administrator=True)
async def kicknorole(ctx: commands.Context):
members = ctx.guild.members
for member in members:
if len(member.roles) == 0:
await member.kick()
print(member.name)
await ctx.reply('Done!')
bot.run('token')
These codes run, but no error shows up and the number of members doesn't go down.
This was my final try:
import discord
from discord.ext import commands
from discord.ext.commands import has_permissions
from discord import Intents
intents = Intents.default()
intents.members = True
bot = commands.Bot(command_prefix='!', intents=intents)
#bot.command()
async def noroleskick(ctx):
# iterate through guild members and kick members with only 1 (#everyone) role
# increase kicked counter +1 for each kicked member
guild_members = ctx.guild.members
kicked = 0
for member in guild_members:
if len(member.roles) == 1:
await member.kick()
kicked += 1
bot.run('tokengoeshere')
I'd greatly appreciate help!

How do I loop a background task in discord.py and why does my code not work?

How can I send a message every x time with a discord bot with discord.py?
Here is my code:
import discord
from discord.ext import commands
from discord.ext import tasks
import os
TOKEN = 'xxxxxxxxxxx'
bot = commands.Bot(command_prefix='!',help_command=None,activity=discord.Game(name="!help"))
#tasks.loop(seconds=5)
async def ping(channel):
print("is the function being called")
channel = bot.get_channel(877115461014282320)
await channel.send('embed=embed')
bot.run(TOKEN)
Edit: I know I shouldn't leak my key but I edited it after 10 seconds and removed a letter why could someone still access it?
You have to start task using ping.start() in event on_ready or in some command.
import discord
from discord.ext import commands
from discord.ext import tasks
import os
TOKEN = os.getenv('DISCORD_TOKEN')
bot = commands.Bot(command_prefix='!', help_command=None, activity=discord.Game(name="!help"))
#tasks.loop(seconds=5)
async def ping():
print("is the function being called")
#channel = bot.get_channel(877115461014282320)
#await channel.send('embed=embed')
#bot.event
async def on_ready():
print('on ready')
ping.start()
bot.run(TOKEN)
You have also ping.stop(), ping.cancel(), etc. See doc: tasks

No audio from discord bot. discord.py/ffmpeg

I'm creating a simple music bot and I cant figure out why there is no audio from the bot. I've tried kicking and rejoining, changing code and more. I just cant figure this out. There is no errors from the command line and the bot has a little green circle around it as if there was audio from it. However it goes away if you rejoin the VC.
Here is the code:
import discord
from discord.utils import get
from discord.ext import commands
from discord import FFmpegPCMAudio
from discord.voice_client import VoiceClient
import youtube_dl
import os
TOKEN = "Token Here"
bot = commands.Bot(command_prefix='s')
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix=',', intents = intents)
#bot.event
async def on_ready():
channel = discord.utils.get(bot.get_all_channels(), id=794444804607574026)
await channel.connect()
#bot.command(name='play')
async def play(ctx, url):
await message.content.startswith('play')
player = await voice_client.create_ytdl_player(url)
player = await voice.create_ytdl_player("https://youtu.be/K4DyBUG242c")
player = await vc.create_ytdl_player()
player.start()
bot.run(TOKEN)
If you want to, feel free to improve it. I got this code from a YouTube tutorial so I don't completely understand the audio part.
Try this. When you run the command the message will never start with play, the bot prefix is ",".
Also, you add intents to client. You should set it to bot instead and delete client, you never use it in the code.
import discord
from discord.utils import get
from discord.ext import commands
from discord import FFmpegPCMAudio
from discord.voice_client import VoiceClient
import youtube_dl
import os
TOKEN = "Token Here"
intents = discord.Intents.default()
intents.members = True
bot = commands.Bot(command_prefix=',', intents = intents)
#bot.event
async def on_ready():
channel = discord.utils.get(bot.get_all_channels(), id=794444804607574026)
await channel.connect()
#bot.command(name='play')
async def play(ctx, url):
# await message.content.startswith('play')
player = await voice_client.create_ytdl_player(url)
player = await voice.create_ytdl_player("https://youtu.be/K4DyBUG242c")
player = await vc.create_ytdl_player()
player.start()
bot.run(TOKEN)

Discord bot that returns guild owner's username discord.py

Hello guys im trying to write a code that gives me the discord server owner but its giving Me 'None'
import discord
client = discord.Client()
TOKEN = 'token'
#client.event
async def on_message(message):
if message.content.find("getowner") != -1:
await message.channel.send(str(message.guild.owner))
client.run(TOKEN)
Can someone please help me with this bot thanks!!
I want to get the discord servers owner by typing getowner in a text channel.
I recommend using it as a command rather than doing on_message, you can do this:
from discord.ext import commands
token = "Token"
client = commands.Bot(command_prefix="!") #Use any prefix
#client.command(pass_context=True)
async def getOwner(ctx):
#await ctx.channel.send(str(ctx.guild.owner.display_name))
await ctx.channel.send(str(ctx.guild.owner))
client.run(TOKEN)
But if you don't want to use a command, you could use regular expression and just keep it as:
import discord
import re
client = discord.Client()
TOKEN = 'token'
#client.event
async def on_message(ctx):
if re.match("getowner", ctx.content):
await ctx.channel.send(str(ctx.guild.owner))
client.run(TOKEN)
EDIT: Also try using intents with your bot code.
import discord
import re
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)
TOKEN = "token"
#client.event
async def on_message(ctx):
if re.match("getowner", ctx.content):
await ctx.channel.send(str(ctx.guild.owner))
client.run(TOKEN)
Same changes can be done with the command version.
It's very simple;
from discord.ext import commands
#client.command(name="getowner")
async def getowner(ctx):
await ctx.send(str(ctx.guild.owner))

Categories