mu_cat

mu_cat

github

Recording a script writing experience with zero foundation in GPT.

Praised haha

image-20230412152758657

Partial screenshots#

image-20230412152741284

image-20230413233141284

Some experience in asking GPT to write code (zero-based)#

1. Patience#

Don't give up just because you keep getting errors or can't get it to run. Really, ask slowly, think carefully, and there will be results!

2. Be clear about what you want to do#

Don't find it troublesome, try to describe your needs clearly, what language to use, where to use it, and what you want to achieve,

For example, I saw a ready-made script for detecting IP and restarting the robot, but it is not suitable for broadband dialing environment, so this is what I finally want, IP detection and restart (dialing version)

And, the feature of GPT is that the more you give it, the more accurate it gives you, so I gave it the original script. If you don't need to run in a specific environment of the robot, just ignore this point completely. I have already asked some basic knowledge about IP detection and restart before, and have written code that can run in the terminal. This depends on personal needs.

image-20230413233744033

This way, GPT gave me a result that is close to 90% of the answer, and the rest is the process of fixing bugs. Just copy and paste the running error to it.

3. Don't lose your own thinking#

Don't just keep copying and pasting the errors to GPT when there are errors. Many times, its logic itself is problematic. At this time, you need to find the problem and find a new breakthrough point by yourself. For example, here I encountered a problem of mistakenly deleting the detected IP. The original code is as follows:

/**
 * @name ip变动重启bncr以及docker
 * @origin muzi
 * @version 1.0.5
 * @description ip变动重启for双拨
 * @rule ^ip$
 * @priority 1000
 * @admin true
 * @public false
 * @disable false
 * @cron 0 *\/5 * * * *
 */
const axios = require('axios');
const AmTool = require('./mod/AmTool');
const sysDB = new BncrDB('system');
const { exec } = require('child_process');
async function getPublicIp() {
  try {
    const response = await axios.get('https://ip.useragentinfo.com/json', { timeout: 10000 }); // 设置 10 秒超时
    const data = response.data;
    return data.ip;
  } catch (error) {
    console.error('获取公共IP地址时发生错误:', error);
    return null;
  }
}
function restartContainer(containerNameOrId) {
  exec(`docker restart ${containerNameOrId}`, (error, stdout, stderr) => {
    if (error) {
      console.error(`Error restarting container: ${error}`);
    } else {
      console.log(`Container restarted: ${stdout}`);
    }
  });
}
module.exports = async s => {
  const v4DB = (await sysDB.get('publicIpv4')) || [];
  const lastCheckedIp = (await sysDB.get('lastCheckedIp')) || '';
  const nowV4ip = await getPublicIp();
  
  if (nowV4ip === null) {
    // 获取 IP 失败,不执行后续操作
    return;
  }
  let logs = `上次ip:${(lastCheckedIp && AmTool.Masking(lastCheckedIp, 5, 6)) || '空'}\n`;
  logs += `本次ip:${(nowV4ip && AmTool.Masking(nowV4ip, 5, 6)) || '空'}\n`;
  let open = false;

  if (!v4DB.includes(nowV4ip)) {
    if (v4DB.length >= 2) {
      logs += '进行bncr与docker重启...';
      open = true;
      // 删除旧的 IP 地址
      v4DB.shift();
      // 重启指定的 Docker 容器,旨在解决ws反向链接假死
      restartContainer('go-cqhttp');
    }
    // 保存新的 IP 地址
    v4DB.push(nowV4ip);
    await sysDB.set('publicIpv4', v4DB);
    await sysDB.set('lastCheckedIp', nowV4ip);
  }

  await s.reply(logs);
  open && (s.getFrom() === 'cron' ? sysMethod.inline('重启') : s.inlineSugar('重启'));
};

Here is a bug:

One IP change causes two restarts. In a dual-dialing environment, there are two IPs. If the script has already obtained two IPs through IP detection, a and b, with a in front and b in the back, when the IP changes, for example, b becomes c, the script will execute the database data becomes b, c, and perform a restart. However, c is derived from the change from b, so when the script detects the unchanged a again, it will be judged as an IP change and execute a restart, and the database will become normal c, a.

So I asked it:

image-20230413234553770

This answer confused me. What is this, something extra? It doesn't matter, I don't understand this if loop, so I continue to ask:

image-20230413234700816

After thinking, this really doesn't work, I feel it won't work, I can't figure it out. So I questioned it!

image-20230413234749703

The root cause of the bug has not been solved here, it is here, I have an epiphany, the reason for the bug is the mistakenly deleted IP, the IP was mistakenly deleted, so it will be judged as a new IP again and cause the code to be true and run. At this time, I organized my language and told it my thoughts, and asked it to translate it into code. In this case, it is 100% valid!

image-20230413235038874

Finally, it still insists! Clearly, I solved the problem, but there is no apology!

Finally#

Natural programming, 100% achievable, now it's just a lack of mature interaction, fast forward to brain-computer!

I wish everyone can achieve what they want to achieve.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.