The code snippet you provided is related to running commands as a scheduled cron job in Discord Bot Framework, specifically for updating dynamic IP addresses using the DynDNS service. Here's a brief overview of what each part does:
1.
bash /root/.hermes/skills/cf-dyndns.sh 2>&1: This line runs the provided script, which is used to update the DynDNS settings.
- The
bash command executes the given script in native mode.
- The arguments (
/root/.hermes/skills/cf-dyndns.sh and
2>&1) are specific to the cron job's context:
-
/root refers to the root user in this file system (a Unix-like operating system).
-
.hermes/skills/cf-dyndns.sh is a custom script directory containing the DynDNS update logic.
-
2> redirects standard error output to separate from standard output, which allows output to be printed separately.
2. The main behavior of the bot as it runs depends on what the
no reply command sends based on its output:
- If the output contains "STATUS:NO_CHANGE:", send nothing (NoReply message).
- If the output contains "ERROR" or a non-zero exit code, send warnings to Telegram and Discord private channels. This usually translates to sending messages indicating DynDNS error notifications to the designated channels.
- Sending an update message if successful.
Here's the script itself:
``
python
import json
from datetime import datetime
start_datetime = datetime.now().strftime('%Y%m%d%H%M%S')
def send_no_reply(message):
print(f"Sending NoReply: {message}")
def send_warning(tg_group, target_user, error_message):
if 0 < int(error_message.count('\n')) <= 1:
# Only one line containing 'ERROR' or non-zero exit code
for line in error_message.split('\r\n'):
if '#' in line:
print(f"Warning to {tg_group['name']} and/or{target_user}:", end='', file=target_user)
print(line, end='\n')
else:
send_no_reply('NoReply')
def send_update(message):
print(f'Sending Update: {message}')
def dyndns_update():
# Get DynDNS settings dictionary
dynns_dict = {}
# Obtain initial IP address
ip_address = input("Enter your new or current DynDNS server IP address (or press Enter to leave it unchanged): ").strip() if request != None else "Enter the same IP address that was entered for this bot."
if ip_address == "":
print("Dynamic IP address cannot be left blank. Please fill in the field with an existing DynDNS configuration.")
return
# Obtain DynDNS server name
dynns_server_name = input("Enter your DynDNS server (or press Enter to leave it unchanged): ").strip() if request != None else "Enter the same server name that was entered for this bot."
if dynns_server_name == "":
print("DynDNS Server Name cannot be left blank. Please fill in the field with an existing DynDNS configuration.")
return
# Use provided DynDNS settings and obtain a dictionary of IP and account data
dyn_dns_settings = {
"ip_address": ip_address,
"dynns_server_name": dynns_server_name,
"dynns_account_name": request["dynns_account"],
"dynsn_api_key": request["dynsn_api_key"]
}
# Print DynDNS settings dictionary for review
print(json.dumps(dyn_dns_settings, indent=4))
# Store the DynDNS configuration
dynns_dict = dyn_dns_settings
# Update account dynamically with new IP address
try:
account_info_response = requests.get(f'https://api.dyn.com/bot/{dynns_server_name}/updateAccount',
params=dyndns_dict, proxies={'https": "http://10.123.25.23:8080"}, verify=True)
except Exception as e:
print("DynDNS Error:", str(e))
return
# Send a reply if the update is successful
send_update("Update successful")
try:
account_info_response = requests.get(f'https://api.dyn.com/bot/{dynns_server_name}/updateAccount',
params=dyndns_dict, proxies={'https": "http://10.123.25.23:8080"}, verify=True)
except Exception as e:
send_warning(' telegram', "+100****5925", str(e))
except IndexError:
print("DynDNS Error and/or network interruption")
else:
# Wait until the user leaves their IP address in the DynDNS settings
import time
time.sleep(60000)
dyndns_update()
print("\nDynamicIP updated")
`
This script is for a bot that can update its Dynamic IP addresses to different servers using the Discord Bot Framework. It prompts users to input an existing or new server to bind their dynamic IP address with, and stores the DynDNS settings in local memory.
To automate sending updates using commands like no reply
, warning
to specific channels, or simply updating the bot's IP address with DynDNS settings:
1. Define relevant variables and parameters (request
, dynns_account
) before creating the bot.
2. Before running an initial update command (e.g., in a onCreate
event of a user), generate valid DynDNS settings using commands like the ones provided.
Remember to replace placeholders if necessary with your actual server IP address, account name, and API key. The DynDNS settings dictionary (dynns_dict
) is structured similarly to this response format for easier understanding:
`
python
{
"ip_address": "ip_address_here",
"dynns_server_name": "DynNS Server Name (replace)"
}
``