Discord bot not responding for commands - python

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")```

Related

discord bot bot not defined

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)

Discord.py Timed message is not executing

So, I have created a command that executes a reoccurring message. I have it set to 10 seconds in the code for testing purposes, but I will have the message loop every 4 hours. I don't get any errors when running the code, but the command is not actually executing. The message never sends and I am confused as to why and can't seem to figure it out.
import discord
import discord.utils
import asyncio
import http.client
import aiohttp
import EZPaginator
import platform
import yaml
import os
from yaml import load, dump
from discord.ext import commands
from discord.ext import tasks
intents = discord.Intents.default()
client = commands.Bot(command_prefix = '$', intents=intents)
client.remove_command('help')
#client.event
async def on_ready():
print('ZOF BOT IS ONLINE!')
# Reoccuring Message Reminder
#tasks.loop(seconds=10)
async def send_message(ctx):
channel = discord.utils.get(ctx.guild.channels, name='reminders')
remind = discord.Embed(title='**:rotating_light:ATTENTION ZOF:', description='Do not forget to donate to Fortress, Alliance tech, and Ruins!', color=0xFFFFFF)
print('Reoccurring Message Sent.')
await channel.send(embed=remind) ```
You need to start you task function for example in on_ready event:
#client.event
async def on_ready():
if not send_message.is_running():
send_message.start()

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 command bot doesn't responde (python)

i have a problem with my discord bot: when i type on discord $ping he doesn't response me pong and i don't know why, i just check the bot have the admin role so he can write, i'm using VsCode and he didn't give me nothing error.
Here is the code
import discord
from discord.ext import commands
import requests
import json
import random
client = discord.Client()
bot = commands.Bot(command_prefix='$')
#bot.command()
async def ping(ctx):
await ctx.channel.send("pong")
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
client.run("XXXXXXXXXXXXXXXXXXXXXXX")
The problem is, that you are defining the command with bot.command, but you only do client.run. To fix this, either choose a client, or a bot, but not both, like this if you choose bot for example:
import discord
from discord.ext import commands
import json
import random
bot = commands.Bot(command_prefix='$')
#bot.command()
async def ping(ctx):
await ctx.channel.send("pong")
#bot.event
async def on_ready():
print('We have logged in as {0.user}'.format(bot))
bot.run(Token)
Also don't use requests, since it's blocking.

Categories