[英]How could I delete a .js file with a Discord.js command?
我想要做的是,如果我有一個名為test.js
的文件,我希望能夠運行 Discord 命令,例如!deletefile
,它將刪除test.js
文件。 這可能嗎? 如果沒有,是否可以做類似的事情但能夠編輯文件而不是刪除它?
如果要使用鍵盤,可以使用箭頭鍵關注文件,然后按 CMD + Delete(或分別按 CTRL)。 這樣做,您也可以按 Enter 開始重命名文件。
我建議查看 VS Code 中的鍵盤快捷鍵:
macOS: https://code.visualstudio.com/shortcuts/keyboard-shortcuts-macos.pdf
windows: https://code.visualstudio.com/shortcuts/keyboard-shortcuts-windows.pdf
linux: https://code.visualstudio.com/shortcuts/keyboard-shortcuts-linux.pdf
Vscode is just a text editor, if you press ctr` you can open the terminal and check what shell do you have, if you installed git then you probably have bash which is a linux shell. 您可以投入少量時間並學習 bash,您可以做的不僅僅是刪除文件。 刪除文件的命令是:
rm filename
您似乎想使用 Discord 機器人刪除文件。
您可以使用內置的 Node.js 模塊, fs
用於此。 它的.unlink
方法將刪除一個文件。 我認為通過 Discord 機器人刪除文件不是一個好主意,但以下應該可以工作:
const fs = require('fs').promises;
const path = require('path');
const { Client } = require('discord.js');
const client = new Client();
client.on('message', async (message) => {
if (message.author.bot || !message.content.startsWith('!deletefile')) return;
try {
// change the path to your file
await fs.unlink(path.join(__dirname, '../logs/1.test'));
message.channel.send('File is deleted. 🎉');
} catch (error) {
console.log(error);
message.channel.send('⚠️ Oops, there was an error. File is not deleted.');
}
});
是的,您還可以編輯 Node.js 中的文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.