我试图用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())
答案 0 :(得分:1)
您正在使用.
运算符而不是:
运算符调用函数,这将导致被调用函数的第一个参数(称为self
)无效。这是因为当您呼叫thing:function(parameter)
时,lua实际上会呼叫thing.function(thing, parameter)
。错误消息指出self
(通常是第一个参数的变量名)已被索引(例如self[2]
或self.b
),但self是一个数字值({{ 1}})。
要解决此问题,只需更改
adminid
到
if member.hasRole(adminid) == true then
此外,不需要测试if member:hasRole(adminid) == true then
,因为它只会在== true
的右侧返回true
,这将不执行任何操作(除非您专门测试true
类型,但通常不是这种情况。
答案 1 :(得分:0)
如果您继续需要答案。
将 message.member
更改为 message.author
这将返回用户,让成员执行(对于公会)message.guild:getMember(author or author.id)