试图用 Discordia 和 Lua 创建一个不和谐的机器人,但踢命令不断崩溃



我正在尝试用Lua和Discordia库编写一个Discord机器人。我一直在尝试实现一种方法来检查运行命令的人是否有这样做的角色。该角色的 id 是adminid变量。机器人一直在工作,直到我实施权限检查,现在它崩溃了

Uncaught Error: .../Documents/Lua Projects/DiscordBot/deps/coro-channel.lua:62: ...rojects/DiscordBot/deps/discordia/libs/utils/Emitter.lua:105: ...cts/DiscordBot/deps/discordia/libs/containers/Member.lua:312: attempt to index local 'self' (a number value) stack traceback: [C]: in function 'assert' .../Documents/Lua Projects/DiscordBot/deps/coro-channel.lua:62: in function 'onPlain' ...s/Lua Projects/DiscordBot/deps/secure-socket/biowrap.lua:76: in function <...s/Lua Projects/DiscordBot/deps/secure-socket/biowrap.lua:61> [C]: in function 'run' [string "bundle:/init.lua"]:52: in function <[string "bundle:/init.lua"]:47> [C]: in function 'xpcall' [string "bundle:/init.lua"]:47: in function 'fn' [string "bundle:deps/require.lua"]:310: in function <[string "bundle:deps/require.lua"]:266>每当运行命令时。我不知道问题是什么,所以我问你们。如果有人可以帮助我,这是我的完整代码。

local discordia = require("discordia")
local coro = require("coro-http")
local json = require("json")
local client = discordia.Client()
adminid = 726406258730598451
local commands = {
{Command = "-ping", Description = "Replies with pong!"};
{Command = "-norris", Description = "Replies with a Chuck Norris fact!"};
{Command = "-cool [mentionedUser]", Description = "Says how cool the mentioned use is! If no one is mentioned it replies with how cool you are!"};
}
function chuckNorris(message)
coroutine.wrap(function()
local link = "https://api.chucknorris.io/jokes/random"
local result, body = coro.request("GET", link)
body = json.parse(body)
message:reply("<@!"..message.member.id.."> "..body["value"])

end)()

end
client:on("messageCreate", function(message)
local content = message.content
local member = message.member
local memberid = message.member.id
if content:lower() == "-ping" then
message:reply("pong")

end
if content:lower() == "-norris" then
chuckNorris(message)

end 
if content:lower():sub(1,#"-cool") == "-cool" then
local mentioned = message.mentionedUsers
if #mentioned == 1 then
message:reply("<@!"..mentioned[1][1].."> is "..math.random(1,100).."% cool.")

elseif #mentioned == 0 then
message:reply("<@!"..memberid.."> is "..math.random(1,100).."% cool.")

end

end
if content:lower() == "-help" then
local list = ""
for i,v in pairs(commands) do
list = list..v.Command..": "..v.Description.."n"

end
message:reply(list)

end
if content:lower():sub(1,#"-ban") == "-ban" then
local mentioned = message.mentionedUsers
if #mentioned == 1 then
message:reply("<@!"..mentioned[1][1].."> has been banned.")
member.guild:banUser(mentioned[1][1],_,_)

elseif #mentioned == 0 then
message:reply("Error: Incorrect Syntax = -ban [user]")

elseif #mentioned >= 1 then
message:reply("Sorry that operation isn't supported yet.")

end

end
if content:lower():sub(1,#"-unban") == "-unban" then
local mentioned = message.mentionedUsers
if #mentioned <= 1 then
message:reply("<@!"..mentioned[1][1].."> has been unbanned.")
member.guild:unbanUser(mentioned[1][1],_)

elseif #mentioned >= 1 then
message:reply("Sorry that operation isn't supported yet.")

end

end
if content:lower():sub(1,#"-kick") == "-kick" then
local mentioned = message.mentionedUsers
if member.hasRole(adminid) == true then
if #mentioned <= 2 then
message:reply("<@!"..mentioned[1][1].."> has been kicked for ")
member.guild:kickUser(mentioned[1][1],_)

elseif #mentioned == 0 then
message:reply("Error: Incorrect Syntax = -kick [user]")


elseif #mentioned >= 2 then
message:reply("Sorry that operation isn't supported yet.")

end
else
message:reply("You do not have permission to run that command.")
end
end

end)
client:run("Bot "..io.open("./login.txt"):read())

您使用.运算符而不是:运算符调用函数,这会导致被调用函数的第一个参数(称为self(无效。这是因为当你调用thing:function(parameter)时,lua实际上调用thing.function(thing, parameter)。错误消息指出self(这是通常用于第一个参数的变量名(被索引(如self[2]self.b(,但 self 是一个数字值 (adminid(。

要解决此问题,只需更改

if member.hasRole(adminid) == true then

if member:hasRole(adminid) == true then

此外,测试== true不是必需的,因为它只会在右侧true时返回true,这什么也不做(除非您专门测试boolean类型,但通常不是这种情况(。

如果你继续需要答案。
message.member更改为message.author
这将返回用户,以使成员执行(对于公会(message.guild:getMember(author or author.id)

最新更新