!userfact command help

So I already have the code written for the command. You type !userfact followed by a fact about yourself to save the fact, and whenever someone types !userfact followed by your @ it will display that fact. But, I don’t know what to do with the code. I tried saving it as a .js file but see no way of importing it to Nightbot. So what do I do?

Hey @frostylosty1!

I’m not sure I understand, do you mean you have written a custom API on your computer and you want Nightbot to use that code?
In that case, the only option is to host the code yourself, and call it via an API URL with $(urlfetch) as Nightbot itself won’t store the data, you’ll need your own server for that.
If the code depends on an already existing API, say the Quote API, then you can host your code on a paste at Pastebin, and call it with $(urlfetch).
It’s difficult to assist you without seeing your code, or at least an explanation of how exactly you wrote it.

To be totally frank, I have no idea how to code, I just asked ChatGPT to code it for me lol. But here is the code it gave me:

let userFacts = {};

if (msg.startsWith('!userfact')) {
  let args = msg.split(' ');
  if (args.length > 1) {
    if (args[1].startsWith('@')) {
      let username = args[1].slice(1);
      if (userFacts.hasOwnProperty(username)) {
        return `${username}'s fact: ${userFacts[username]}`;
      } else {
        return `${username} has not set a fact yet.`;
      }
    } else {
      let fact = args.slice(1).join(' ');
      userFacts[user['name']] = fact;
      return `Your fact has been set to: ${fact}`;
    }
  } else {
    return 'Please provide a fact or a username.';
  }
}

It told me this, if it helps: “This code uses an object called userFacts to store the facts for each user. When someone types !userfact followed by text, it will save that text as that user’s fact. And when they type !userfact followed by a username with an @ symbol before it, it will display that person’s userfact they set, if they have already set one.”

Yes, so that code doesn’t store data durably anywhere, it wouldn’t work with Nightbot, it contains errors, and it could be rewritten so it’s easier to read.

ChatGPT, or any other AI, isn’t able to write code that works just like that (yet), you have to give it a well thought out prompt, it should have context clues to understand how to write code that integrates well with the existing code base/framework/tool(s), and you should know how to write code so you can fix its mistakes, we’re still at least years away from replacing devs completely, for now it’s just a tool to assist us.


To make a similar command with what we have, we’re going to use the Quote API I mentioned before, get your tokens by following the Manual Installation link.
Refer to the following to identify your PUBLIC_TOKEN and PRIVATE_TOKEN:

$(urlfetch https://twitch.center/customapi/quote?token=PUBLIC_TOKEN&data=$(querystring))
$(urlfetch https://twitch.center/customapi/addquote?token=PRIVATE_TOKEN&data=$(querystring))
$(urlfetch https://twitch.center/customapi/delquote?token=PRIVATE_TOKEN&data=$(querystring))

Copy them and store them somewhere safe, never share your PRIVATE_TOKEN, anyone could then manage your list, that’s why the code I’ll write won’t contain command calls to add the new commands through the chat, add the new commands through the dashboard.


We need three commands: one to consult a fact about someone, one to set a fact about oneself, and another one to delete the fact about oneself (we actually need more than three commands as the last two will require alias commands so we can add checks to avoid issues, but once set up, chatters will only use three commands).

  • !fact — get a fact about someone:

    $(eval f = `$(urlfetch https://twitch.center/customapi/quote?token=PUBLIC_TOKEN&data=$(touser)%3A)`; f.startsWith('0.') ? 'There are no fact about this chatter yet' : f;)
    

    :beginner: syntax: !fact optional_username

  • !addfact — add a fact about oneself:

    $(eval u = '$(user)'; q = `$(urlfetch json https://twitch.center/customapi/quote/list?token=PUBLIC_TOKEN)`.split(/\d+\.\s/).map(e => e.split(':')[0]); q.includes(u) ? 'you already added a !fact about yourself' : `PRIVATE_TOKEN&data=$(user)%3A%20$(querystring)`;)
    

    :warning: set this command’s alias to _addfact
    :beginner: syntax: !addfact a cool fact about myself

  • _addfact!addfact’s alias:

    $(eval a = `$(urlfetch https://twitch.center/customapi/addquote?token=$(query))`; f = decodeURIComponent(`$(query)`.replace('PRIVATE_TOKEN&data=$(user)%3A%20', '')); a.includes('Successfully added') ? `$(user) — your fact has been set to: ${f}` : `$(user) — there was an error: ${a.includes('Invalid') ? f : a}`;)
    

    :beginner: do not call this command, it’ll be automatically called by !addfact

  • !delfact — remove the fact about oneself:

    $(eval u = '$(user)'; q = `$(urlfetch json https://twitch.center/customapi/quote/list?token=PUBLIC_TOKEN)`.split(/\d+\.\s/).map(e => e.split(':')[0]); i = q.indexOf(u); i === -1 ? 'you haven\'t set a fact about yourself yet' : `PRIVATE_TOKEN&data=${i}`;)
    

    :warning: set this command’s alias to _delfact
    :beginner: syntax: !delfact

  • _delfact!delfact’s alias:

    $(eval a = `$(urlfetch https://twitch.center/customapi/delquote?token=$(query))`; f = decodeURIComponent(`$(query)`.replace('PRIVATE_TOKEN&data=', '').replace(/\d+/g, '')); a.includes('Successfully deleted') ? '$(user) — your fact has been successfully deleted' : `$(user) — there was an error: ${a.includes('Invalid') ? f : a}`;)
    

    :beginner: do not call this command, it’ll be automatically called by !delfact

:warning: Make sure to replace every instances of PUBLIC_TOKEN (3) and PRIVATE_TOKEN (4) with your tokens.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.