admin管理员组

文章数量:1292244

Trying to get the bot to spit out random scenario messages that also include @user midsentence.

var myArray = [
  message.author + " challenges the producers, but fails to survive a single puppet",
  "Oh no, " + message.author + " got demolished by **Mr. Smiles**",
  "After gearing up in several events " + message.author + " tried to swim and drowned"
]
var rand = myArray[Math.floor(Math.random() * myArray.length)];
channel.message(rand)

The mand is linking to this script and so far works, although instead of mentioning the user doing the mand, it prints "undefined".

I'm fairly new to javascript, but let me know if you need more information

Trying to get the bot to spit out random scenario messages that also include @user midsentence.

var myArray = [
  message.author + " challenges the producers, but fails to survive a single puppet",
  "Oh no, " + message.author + " got demolished by **Mr. Smiles**",
  "After gearing up in several events " + message.author + " tried to swim and drowned"
]
var rand = myArray[Math.floor(Math.random() * myArray.length)];
channel.message(rand)

The mand is linking to this script and so far works, although instead of mentioning the user doing the mand, it prints "undefined".

I'm fairly new to javascript, but let me know if you need more information

Share edited May 23, 2020 at 10:07 m02ph3u5 3,1617 gold badges41 silver badges57 bronze badges asked Jun 20, 2018 at 12:56 ThiilThiil 971 gold badge3 silver badges11 bronze badges 9
  • Use toString() to make a mention : discord.js/#/docs/main/stable/class/User?scrollTo=toString Though I think this will not solve your problem. I think we need a bit more of your code to be able to find the issue. – Seblor Commented Jun 20, 2018 at 13:02
  • upon changing it to message.author.toString() i recieve the following error: Error [javax.script.ScriptException]: TypeError: Cannot read property "toString" from undefined in <eval> at line number 1 – Thiil Commented Jun 20, 2018 at 13:05
  • Of course it would print that error, since message.author is undefined, message.author.toString() does not exists. That is why I edited my mend saying that we need more information – Seblor Commented Jun 20, 2018 at 13:08
  • This IS the entire code ... For the mand in discord, i did //custom setvalue NAMEOFCOMMAND pastebin./hkB85kjp It could be that by making it grab off pastebin, that the message.author can't be grabbed? – Thiil Commented Jun 20, 2018 at 13:10
  • This cannot be the entire code, as you never defined message or channel. They must be ming from somewhere, like a function definition, in which case it would be nice to have its call. – Seblor Commented Jun 20, 2018 at 13:12
 |  Show 4 more ments

2 Answers 2

Reset to default 6

message.author is an object. If you want to get his name do message.author.username If you want to mention him do: "<@" + message.author.id + ">"

Try to do this:

const userAuthor = message.author.toString()

And then when you go to code your mand:

message.channel.send(`The person, ${userAuthor}, is the coolest person.`)

本文标签: discordjsJavascript discord bot mention user midsentenceStack Overflow