update
This commit is contained in:
parent
adebf8b317
commit
462e2a232d
@ -473,6 +473,84 @@ Managing containers involves more than just creating and extending them—it als
|
||||
|
||||
The `/destroy` command in Discord-Linux initiates the process of removing a user’s container. When a user runs this command, the bot doesn’t immediately proceed with the destruction. Instead, it first retrieves the user’s container ID and then asks for confirmation before performing the actual deletion. This prevents accidental container destruction and provides users with a safety net to protect their data.
|
||||
|
||||
### Container Destruction
|
||||
|
||||
Once the user confirms that they want to proceed, the bot performs the actual destruction of the container. This is done by stopping and removing the container using Docker’s command-line interface.
|
||||
|
||||
```javascript
|
||||
cmd('docker stop ' + sshSurfID + " && docker rm " + sshSurfID).then(out => {
|
||||
console.log('Container destroyed:', out);
|
||||
});
|
||||
```
|
||||
|
||||
The bot uses the container ID (retrieved earlier) to stop and remove the Docker container associated with the user. The combination of `docker stop` and `docker rm` ensures that both the container and its data are completely removed from the system, freeing up resources.
|
||||
|
||||
### Cleaning Up Network Configuration
|
||||
|
||||
After the container is destroyed, the system also removes any network configurations associated with the container. This ensures that no leftover data, such as network routes or IP addresses, lingers after the container is removed.
|
||||
|
||||
```javascript
|
||||
try {
|
||||
fs.unlinkSync(netConfig);
|
||||
console.log("Network Config Removed!");
|
||||
} catch (err) {
|
||||
console.log("No Config to remove");
|
||||
}
|
||||
```
|
||||
|
||||
In this snippet, the bot checks if a network configuration file exists for the container. If it does, the file is deleted, ensuring that the network resources used by the container are properly cleaned up.
|
||||
|
||||
### Notifying the User
|
||||
|
||||
Once the container has been successfully destroyed, the bot sends an embedded message to the user to inform them of the completion of the process. The message is displayed in Discord using an `EmbedBuilder`, which provides a visually appealing way to show important information.
|
||||
|
||||
```javascript
|
||||
const embed = new EmbedBuilder()
|
||||
.setTitle("🤯 The container was destroyed!")
|
||||
.setDescription(`You may generate a new one if you would like using /generate at any time!`)
|
||||
.setTimestamp()
|
||||
.setFooter({ text: `Requested by ${interaction.user.username}`, iconURL: `${interaction.user.displayAvatarURL()}` });
|
||||
|
||||
await interaction.editReply({ embeds: [embed] });
|
||||
```
|
||||
|
||||
The message confirms to the user that the container has been successfully destroyed and reminds them that they can generate a new container if needed. This feedback is critical in ensuring the user knows the action has been completed.
|
||||
|
||||
### Additional Cleanup and Notifications
|
||||
|
||||
Beyond the basic destruction process, Discord-Linux goes a step further by notifying external systems and performing additional cleanup. For instance, the bot makes an HTTP request to notify another service that the user’s container has been deleted, ensuring that all traces of the container are removed from the wider network.
|
||||
|
||||
```javascript
|
||||
const request = unirest.delete(`http://non-public-endpoint/${userID}`)
|
||||
.headers({ 'password': PASSWORD });
|
||||
|
||||
request.end(function (response) {
|
||||
if (response.error) {
|
||||
console.error('Error:', response.error);
|
||||
} else {
|
||||
console.log('Response:', response.body);
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
This HTTP request ensures that all external systems are updated to reflect the removal of the container, helping maintain consistency across the network.
|
||||
|
||||
### Handling Errors
|
||||
|
||||
While the bot handles most scenarios smoothly, there’s always a chance that something could go wrong. To prevent unexpected behavior, the code includes error handling to manage cases where the container may not exist or cannot be destroyed. If the container doesn’t exist, for example, the bot notifies the user that there’s no container to destroy.
|
||||
|
||||
```javascript
|
||||
cmd('docker stop ' + sshSurfID + " && docker rm " + sshSurfID).catch(err => {
|
||||
if (err.toString().includes("such")) {
|
||||
console.log("A container does not exist to destroy");
|
||||
interaction.editReply("A container does not currently exist to destroy.");
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
This ensures that users aren’t confused if they attempt to destroy a container that no longer exists or was already removed. The bot provides clear feedback in these situations, keeping users informed at every step.
|
||||
|
||||
|
||||
### User Confirmation Process
|
||||
|
||||
To avoid accidental deletions, the bot uses a confirmation dialog to prompt the user before proceeding. This prompt comes in the form of a **Discord select menu**, offering users the choice to either proceed with destruction or cancel the action.
|
||||
@ -657,83 +735,6 @@ The platform’s ability to run commands non-interactively via Discord is incred
|
||||
|
||||
By leveraging Docker, REST logging, and thoughtful command handling, the platform delivers a flexible and secure way for users to run commands, all within a familiar interface like Discord.
|
||||
|
||||
### Container Destruction
|
||||
|
||||
Once the user confirms that they want to proceed, the bot performs the actual destruction of the container. This is done by stopping and removing the container using Docker’s command-line interface.
|
||||
|
||||
```javascript
|
||||
cmd('docker stop ' + sshSurfID + " && docker rm " + sshSurfID).then(out => {
|
||||
console.log('Container destroyed:', out);
|
||||
});
|
||||
```
|
||||
|
||||
The bot uses the container ID (retrieved earlier) to stop and remove the Docker container associated with the user. The combination of `docker stop` and `docker rm` ensures that both the container and its data are completely removed from the system, freeing up resources.
|
||||
|
||||
### Cleaning Up Network Configuration
|
||||
|
||||
After the container is destroyed, the system also removes any network configurations associated with the container. This ensures that no leftover data, such as network routes or IP addresses, lingers after the container is removed.
|
||||
|
||||
```javascript
|
||||
try {
|
||||
fs.unlinkSync(netConfig);
|
||||
console.log("Network Config Removed!");
|
||||
} catch (err) {
|
||||
console.log("No Config to remove");
|
||||
}
|
||||
```
|
||||
|
||||
In this snippet, the bot checks if a network configuration file exists for the container. If it does, the file is deleted, ensuring that the network resources used by the container are properly cleaned up.
|
||||
|
||||
### Notifying the User
|
||||
|
||||
Once the container has been successfully destroyed, the bot sends an embedded message to the user to inform them of the completion of the process. The message is displayed in Discord using an `EmbedBuilder`, which provides a visually appealing way to show important information.
|
||||
|
||||
```javascript
|
||||
const embed = new EmbedBuilder()
|
||||
.setTitle("🤯 The container was destroyed!")
|
||||
.setDescription(`You may generate a new one if you would like using /generate at any time!`)
|
||||
.setTimestamp()
|
||||
.setFooter({ text: `Requested by ${interaction.user.username}`, iconURL: `${interaction.user.displayAvatarURL()}` });
|
||||
|
||||
await interaction.editReply({ embeds: [embed] });
|
||||
```
|
||||
|
||||
The message confirms to the user that the container has been successfully destroyed and reminds them that they can generate a new container if needed. This feedback is critical in ensuring the user knows the action has been completed.
|
||||
|
||||
### Additional Cleanup and Notifications
|
||||
|
||||
Beyond the basic destruction process, Discord-Linux goes a step further by notifying external systems and performing additional cleanup. For instance, the bot makes an HTTP request to notify another service that the user’s container has been deleted, ensuring that all traces of the container are removed from the wider network.
|
||||
|
||||
```javascript
|
||||
const request = unirest.delete(`http://non-public-endpoint/${userID}`)
|
||||
.headers({ 'password': PASSWORD });
|
||||
|
||||
request.end(function (response) {
|
||||
if (response.error) {
|
||||
console.error('Error:', response.error);
|
||||
} else {
|
||||
console.log('Response:', response.body);
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
This HTTP request ensures that all external systems are updated to reflect the removal of the container, helping maintain consistency across the network.
|
||||
|
||||
### Handling Errors
|
||||
|
||||
While the bot handles most scenarios smoothly, there’s always a chance that something could go wrong. To prevent unexpected behavior, the code includes error handling to manage cases where the container may not exist or cannot be destroyed. If the container doesn’t exist, for example, the bot notifies the user that there’s no container to destroy.
|
||||
|
||||
```javascript
|
||||
cmd('docker stop ' + sshSurfID + " && docker rm " + sshSurfID).catch(err => {
|
||||
if (err.toString().includes("such")) {
|
||||
console.log("A container does not exist to destroy");
|
||||
interaction.editReply("A container does not currently exist to destroy.");
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
This ensures that users aren’t confused if they attempt to destroy a container that no longer exists or was already removed. The bot provides clear feedback in these situations, keeping users informed at every step.
|
||||
|
||||
# Editing Files with Discord-Linux
|
||||
Discord-Linux’s **edit-file** command offers users the ability to edit files within their Docker containers through Discord, making it a highly effective and user-friendly solution for container management. This functionality is facilitated through a series of integrated steps, leveraging Docker, MySQL, and Discord.js to provide seamless file editing without needing direct access to the container shell. Here's an in-depth look at how this system works, with code examples to illustrate each step.
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user