How to auto restart discord bot using python - python

Is there any way of making my discord bot auto-restart. What i mean is say if my wifi shut off and my bot went offline the code would detect the bot went offline it would attempt to restart the bot- if it failed it would wait 5 mins then try again. NOTE: The discord bot source code is running on python 3.6.5 on a raspberry pi 3

I think it can be done by creating seccond file:
import os
while True:
os.system("yourfilename.py")
time.sleep(300)
this will run your bot 5 minutes after going offline every time that will happen

Related

Discord Client error when running discord bot with python

So I was first running my code on replit.com and I decided to switch over to running it localy on my computer. I got it running on Python IDLE 3.10.1 and then I decided to run it on VS Code with the same interpreter and then I got an error:
TypeError: Client.__init__() missing 1 required keyword-only argument: 'intents'
On this line of code:
client = discord.Client()
And this line of code was working both on replit and on IDLE and after running it on VS code it stopped running on IDLE aswell. And after I fixed the error with this I found on stack overflow:
client = discord.Client(intents=discord.Intents.default())
The bot seems to be running in the console, but in reality it isn't running because it isn't responding to my commands.
I also get these weird messages before the bot "starts":
TLDR; Running discord bot on replit, switched to Python IDLE 3.10.1 kept working, switched to VS Code error appeared, fixed error, bot not working anymore on IDLE and VS Code.
I fixed it like this:
Code:
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
And from the discord dev portal you need to enable whatever priviledges you want, in my case message priviledges

Discord bot is running but not responding to commands after using VS Code

So I was first running my code on replit.com and I decided to switch over to running it localy on my computer. I got it running on Python IDLE 3.10.1 and then I decided to run it on VS Code with the same interpreter and then I got an error:
TypeError: Client.__init__() missing 1 required keyword-only argument: 'intents'
On this line of code:
client = discord.Client()
And this line of code was working both on replit and on IDLE and after running it on VS code it stopped running on IDLE aswell. And after I fixed the error with this I found on stack overflow:
client = discord.Client(intents=discord.Intents.default())
The bot seems to be running in the console, but in reality it isn't running because it isn't responding to my commands.
I also get these weird messages before the bot "starts":
TLDR; Running discord bot on replit, switched to Python IDLE 3.10.1 kept working, switched to VS Code error appeared, fixed error, bot not working anymore on IDLE and VS Code.
Try this :)
from discord.ext import commands
botDescription="Bot"
intents = discord.Intents.all()
client = commands.Bot(command_prefix="?", help_command=None, description=botDescription, intents=intents)
The Bot it self need the right to handle intents. Go to discord.com/developers/applications > Bot > SERVER MEMBERS INTENT > True

Python ffmpeg not working on heroku, no error but it just kind off freezes at that line

When I run the bot offline it just works but when i run it on the heroku server it doesn't. it prints the time right at the first time, then the bot connects to the channel and then it just freezes, i get no errors in the heroku logs, the bot just stays online. I've added the ffmpeg buildpack into the buildpacks in heroku and added ffmpeg to the requirements.txt. the time is also correct (i defined the time earlier using an other package) so it can asses the sound file but it just sort of freezes at the ffmep line and doesn't play the sound, and then doesn't print the time and doesn't disconnect.
voice = await channel.connect()
print(time)
voice.play(discord.FFmpegPCMAudio(f'sounds/{arg}.mp3'))
print(time)
sleep(time)
await voice.disconnect()
I had the same issue. The problem is you also need to install libopus. To do this:
Enter your Heroku page.
Go to Settings.
Click Add Buildpack in Buildpacks.
Type https://github.com/codeinteger6/heroku-buildpack-libopus.git in Enter Buildpack URL section.
Deploy your app again.
I hope this will work.

Python to send a command through another process, wait for process to terminate, and then reboot system?

I'm running a Minecraft server for my friends and I off of a Raspberry Pi. Aside from lag (Due to my internet, not the hardware) the server runs smoothly, however, my Pi wants to shut off every so often (If the server console crashes, or if screen decides to close that session, etc...). What I want to do is create a python script that runs at Midnight and Noon every day to issue a few commands through the server console and to the pi itself.
The server console is accessed through screen -r Minecraft so I can close an SSH session and leave the server running. If the server is not running, it can be started with ./minecraft.sh.
Heres essentially what I want to do: (Sorry, I know very little python aside from what I need to navigate the pi console) (I am more familiar with C# so I'm sorry if my code more resembles that instead of Python)
Note: Commands issued into the server console screen -r minecraft do not require quotes around non-command text I.E. Say Words will send Words to game chat, but Say "Words" will send "Words" to game chat.
//SafeShutDown.py
//This code is in no way executable. I just typed it out as an outline. A lot is wrong with it I'm assuming.
Loop A;
If dateTime.Now == 12:00 or dateTime.Now == 24:00 {
screen -r Minecraft
say Server saving... //In minecraft console. Announces to players
autosave all //In minecraft console. Plugin to save the world
say Server is restarting in 1 minute. Please wait //In Minecraft console
wait 60s //In python
stop //Command to send within the process "Minecraft" to shutdown server
wait for process "Screen -r Minecraft" to terminate //This is what I need help with
reboot
Else { Go To A;}
// Other scripts here that starts at boot to run ./minecraft.sh and ./SafeShutDown.py from /home/Minecraft.
I have tried getting scripts to run at boot to no avail. Minecraft.sh is supposed to run at boot so if it crashes will still run (Minecraft.sh starts server under screen -r Minecraft so I can get to it later)
Edit: Spelling, C# explanation, better code.
Edit: Descriptors of Minecraft commands, added announcements and save state.

How do I make my discord bot stay online

I recently made a music bot in my discord server but the problem I have is that I have to turn on the bot manually etc. and it goes offline when I turn off my computer. How can I make the bot stay online at all times, even when I'm offline?
You need to run the python script on a server, e.g. an EWS linux instance.
To do that you would need to install python on the server and place the script in the home directory and just run it via a screen.
Use a Raspberry Pi with an internet connection. Install all necessary components and then run the script in its terminal. Then, you can switch off the monitor and the bot will continue running. I recommend that solution to run the bot 24/7. If you don't have a Raspberry Pi, buy one (they're quite cheap), or buy a server to run the code on.

Categories

Resources