My bot is no longer responding to commands - python

This was asked previously, but never answered (as in marked) and did not have the same specifics as me (here)
For context, I was working on my bot, specifically on a command that add and removes roles, I restarted the bot, and it just stopped responding to commands. I say that instead of "stopped working" because it still works. It comes online, it can put its status up, and it responds to on_message functions. Any attempt at calling a command does nothing, it doesn't respond in chat, it doesn't give an error, throwing a print statement in the command shows that it doesn't even run the command.
I have already tried reverting the roles command because it happened right after, did not fix it. I removed the command entirely and the same problem occurred. I re-installed all my packages whether they related to discord or not and updated. Nothing fixed it.
I suspect it has something to do with the way I'm running the bot. I'm using Pycord. Here is some code that may be helpful, but I really don't know what would help resolve the problem
# Top of main.py file
import discord, random, json, os
from discord.ext import commands
from keep_alive import keep_alive
from discord.utils import find
# discord.py API docs: https://discordpy.readthedocs.io/en/latest/api.html
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix='!', intents=intents)
client.remove_command('help')
# ...
# unrelated code here
# ...
# I use cogs for my commands, here is the imports for all cogs
client.load_extension('cogs.commands.fun')
client.load_extension('cogs.commands.msg')
client.load_extension('cogs.commands.mod')
client.load_extension('cogs.commands.games')
client.load_extension('cogs.commands.role')
client.load_extension('cogs.commands.misc')
client.load_extension('cogs.commands.secretCommands')
client.load_extension('cogs.commands.msgTriggers')
# ...
# unrelated code here
# ...
# Bottom of main.py file
keep_alive()
client.run(os.getenv('token'))
# Top of one of my cogs (fun commands cog)
import discord, json, randfacts, random
from discord.ext import commands
from random import randint
from cogs import assets
class Fun(commands.Cog):
def __init__(self, client):
self.client = client
# ...
# unrelated code here
# ...
# Bottom of one of my cogs (fun commands cog)
def setup(client):
client.add_cog(Fun(client))

Related

intents not working on discord.py commands extension

I'm trying to use intents on my discord bots so I can get all members. I looked through a few posts and read that I'm supposed to do
from discord.ext import commands
intents = discord.Intents(messages=True, members=True)
bot = commands.Bot(command_prefix=',', intents=intents)
However when I try it, it says NameError: name 'discord' is not defined. I don't know how to solve it and why works for other people.
I also have "Server Members Intent" ticked.
I've tried putting import discord at the top but it return None if I print the guild or member list (it only shows the info for the bot itself, the other users it just says "None").
This is because you have imported only the commands extension, you have to import discord fully too, simply add this to the top of your file
import discord
...

Closing Discord bot connection without terminating command line (discord.py)

Purpose:
To add more commands without interrupting other commands
I have been looking around to find a way to roll out updates without interrupting the flow of my bot, since it has some asyncio functions that execute a while after the function has been called
I have tried:
await client.logout()
Above will logout the bot, but also closes the command line. I found this on the discord.py docs.
I am running Windows 10, with Python version 3.9
Any help would be appreciated. Thanks in advance.
If you don't want to kill the current process and instead just want hot-reloading of different functionality, you might want to look into discord.py's extensions feature. Using extensions and cogs will allow you to enable/disable/reload certain features in your bot without stopping it (which should keep tasks running). It's also the built-in method for hot-reloading.
Extensions and cogs are typically used together (though they don't have to be). You can create files for each group of similar commands you want to reload together.
The following code samples should be integrated into your setup. You'll probably also want to add error handling and input checks, but it should give you an idea of what's going on. For a detailed explanation or method options, check out the docs.
# info.py
# ...
# this is just an example
class Info(commands.Cog):
def __init__(self, bot):
self.bot = bot
#commands.command()
async def about(self, ctx):
await ctx.send("something here")
# ...
# extensions need a setup function
def setup(bot):
bot.add_cog(Info(bot))
# main.py
# ...
bot = commands.Bot(
# ...
)
bot.load_extension("info")
bot.run(token)
# reload command
#bot.command()
async def reload(ctx, extension):
# probably want to add a check to make sure
# only you can reload things.
# also handle the input
bot.reload_extension(extension)
to use, you might do something like `prefix!reload info`
Create a new python file in the same directory with the name startup.py for example. Inside this file do the following:
import os
import time
time.sleep(5)
os.system('python YOUR_BOTS_FILE_NAME.py')
Then in the file where your bot's code is add a new command that we are going to call restart for example:
import discord
from discord.ext import commands
import os
#client.command()
#commands.is_owner()
async def restart(ctx):
os.system('python startup.py')
exit()
In the startup.py file, os waits 5 seconds for your bot's file to turn off and then turns it on. The restart command in your bot's file starts the startup file then shuts itself down.
#commands.is_owner()
Makes sure the author of the message is you so people don't restart your bot.
I am developing a bot myself and I have made a shutdown command myself which shuts down the bot without using terminal.
First I would add the code and then explain it.
Code:
myid = <my discord account ID>
#MyBot.command()
async def shutdown(ctx):
if ctx.author.id == myid:
shutdown_embed = discord.Embed(title='Bot Update', description='I am now shutting down. See you later. BYE! :slight_smile:', color=0x8ee6dd)
await ctx.channel.send(embed=shutdown_embed)
await MyBot.logout()
if ctx.author.id != myid:
errorperm_embed = discord.Embed(title='Access Denied!', description='This command is `OWNER` only. You are not allowed to use this. Try not to execute it another time.', color=0xFF0000)
errorperm_embed.set_footer(text=ctx.author)
await ctx.channel.send(embed=errorperm_embed, delete_after=10.0)
I have not added any has_permissions as I don't need it when I using my discord ID to restrict its usage.
Explanation:
I have defined a variable, myid which is equal to my discord account ID.
Check here on how to get user ID:
https://support.discord.com/hc/en-us/articles/206346498-Where-can-I-find-my-User-Server-Message-ID-
I have added a condition that if the ID of the user who used this command is equal to myid or if it is not. If it is equal to my account' ID then it would shutdown the bot otherwise it would show an error to the user.
I have simply used await MyBot.logout() which logs you out and disconnect.
You can place your code in a while True loop.
while True:
client = commands.Bot(command_prefix='!')
async def restart(ctx):
await client.logout()
client.run('token')
You could just replace the current process with the same process, starting anew. You have to flush buffers and close file-pointers beforehand, but that's easy to do:
import os
import sys
from typing import List
def restart(filepointers: List):
# this cleanup part is optional, don't need it if your bot is ephemeral
# flush output buffers
sys.stdout.flush()
sys.stderr.flush()
# flush and close filepointers
for fp in filepointers:
os.fsync(fp)
fp.close()
# replace current process
os.execl(*sys.argv)
Then just call this function with your bot as you would(from the same file).
If you want to update your code you must restart the program.
import os
path = "your .py file path"
#client.command(name="restart")
async def restart_command(ctx):
await client.close()
os.system("python " + path)

Discrod.py rewrite #bot.command not responding no matter what i do

ok, first time ever asking a question so i apologize if i do something wrong. i am trying to use the #bot.command feature and it wont return anything no matter what i do.
from discord.ext import commands
from config import *
from discord import *
import discord
import time
import random
client = discord.Client()
bot = commands.Bot(command_prefix='+')
#bot.command()
async def yes(ctx):
await ctx.send('yes')
print("worked")
bot.add_command(yes)
client.run("token")
bot.run("token")
so in theory when someone types "+yes" is discord it should respond with "yes" as well as print "worked" in the terminal. instead i just get nothing. i know the bot works because i have '''other on_message''' commands but i have to use the offical '''#bot.command''' for this because it will eventually be part of interactivity.
You need to supply the command to the command, That's how I usually do it. Unsure of your method but perhaps try it this way:
client = discord.Client()
bot = commands.Bot(command_prefix='+')
#bot.command(name="yes")
async def yes(ctx):
await ctx.send('yes')
print("worked")
# bot.add_command(yes) # How will it know to map function yes to the command yes?
client.run("token")
bot.run("token)

Why is my discord bot code not running anything?

Okay so I wanted to make a discord bot for my first python project. However, despite my seemingly correct code, the bot doesn't run:
I used this basic code to simply get the bot online but nothing happens. I haven't seen anyone else have this problem either.
import discord
from discord.ext import commands
client = commands.Bot(command_prefix = '.')
#client.event
async def on_ready():
print("Bot is ready")
client.run("Token") # i removed the token for security
and this is the result of the program
Am I doing something wrong?
I am using this version of the discord botting API: https://github.com/Rapptz/discord.py
As #jwjhdev said, client.run is not supposed to be tabbed under on_ready. If you delete the tab, then everything should work perfectly.

Discord.py Python Bot the bot goes offline in discord after approximately 5 minutes. but continues to do its job

Below is my code, I made a bot using Discord.Py, Its basis is to take a screenshot, then post the screenshot to discord, it is supposed to repeat this process every 5 minutes continuously. this works within my script, however after a bit the bot then goes offline in discord, and is no longer under its role, or under online members, and is shown offline. Is their a way to force the bot to stay online?
import pyautogui
import pyscreeze
import time
import discord
from discord.ext import tasks
from discord.ext.commands import Bot
from discord.ext import commands
class MyClient(discord.Client) :
async def on_ready(self):
print('online')
game = discord.Game("Watching Logs")
await client.change_presence(status=discord.Status.online, activity=game)
while True:
time.sleep(300 - time.time() % 300)
im1 = pyscreeze.screenshot()
im2 = pyscreeze.screenshot(r'C:\Users\Tyler\Desktop\discord-screenshot-bot-master\371logs.png')
print('Screenshot Taken')
time.sleep(5)
channel = client.get_channel(780053606753239050)
await channel.send('371 Logs')
await channel.send(file=discord.File(r'C:\Users\Tyler\Desktop\discord-screenshot-bot-master\371logs.png'))
print('Screenshot Posted to Discord')
client = MyClient()
client.run('token')
Are you running the bot on a computer you own, or is it being hosted? Some hosts might consider a sleeping Python program to be "inactive". Also, can you see the stdout it is printing? It might be crashing you not know it. Also, you might want to check out the logging module. If you want it, here is a tutorial.
(This looks pretty similar to this page...)
EDIT:
Discord's python module has some pretty weird error catching, as it is intended for use with logging.
Here is a snippet of code from my own Discord bot:
import logging
import discord
logger = logging.getLogger("discord")
logger.setLevel(logging.INFO) # Do not allow DEBUG messages through
handler = logging.FileHandler(filename="bot.log", encoding="utf-8", mode="w")
handler.setFormatter(logging.Formatter("{asctime}: {levelname}: {name}: {message}", style="{"))
logger.addHandler(handler)
If you want more messsages (for example, when debugging, as you are now), comment out logger.setLevel(logging.INFO) # Do not allow DEBUG messages through.

Categories