Discordia bot DM sending breaks my script

656 Views Asked by At

I am trying to make a bot for my server on discord using Discordia, but when I try to use member:send(str), all I get is Uncaught Error: C:/luvit/deps/coro-channel.lua:62: C:/luvit/deps/discordia/libs/containers/User.lua:91: attempt to call method 'getPrivateChannel' (a nil value) it does send the dm, but then the code breaks and the bot doesn't run anymore, how can I fix this?

member is a variable that gets the member from message

local member = message.member

2

There are 2 best solutions below

3
Allister On

I am able to recreate the issue, albeit with a different error that I suspect is due to me not having all your code to debug with. The bot will send the DM and post a message, then crash due to error. You have this code:

if message.content:lower() == prefix..'createdm' then
      message.channel:send("<@!"..memberid.."> ".. "Sent!")
      message.member:send("test") --message.member is able to inherit the send method from message.author
end

I don't know how you find memberid, so I defined it like this: memberid = message.member:__hash() It is called every time a message is sent, and it sends a message. This means the bot is going to call this and read its own messages. While the bot will not continue into the if statement because its message is not <prefix>createdm, it will still try to get memberid and evaluate the if statement. I do not know how bot users differ from human users in their account metadata, but I strongly suspect there is some difference and this is your issue.

I believe adding this to your code, at the beginning of your message callback (before your do anything else to evaluate a message) will fix your problem.

if message.author == client.user then
   return
end

client here is whatever you assigned discordia.Client() to.

0
aStonePenguin On
if message.content:lower() == prefix..'createdm' then
      message.channel:send("<@!"..memberid.."> ".. "Sent!") -- member.id !!!
      message.member:send("test") --message.member is able to inherit the send method from message.author
end