sirfish/bot.lua
2024-08-26 18:30:44 +01:00

144 lines
3.8 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

local discordia = require('discordia')
local client = discordia.Client {
gatewayIntents = 36863,
}
local http = require("simple-http")
local json = require('json')
local prefix = "*"
math.randomseed(os.time())
--- https://stackoverflow.com/a/22843701
string.startswith = function(self, str)
return self:find('^' .. str) ~= nil
end
function load(filename)
local f = io.open(filename, "r")
if not f then return false end
local token = f:read("*a")
f:close()
return token:gsub("\n","")
end
function write(filename, inp)
local f = io.open(filename, "w")
if not f then return false end
local token = f:write(inp)
f:close()
return true
end
function getnick(str, guild)
if guild ~= nil then
local b = str:gsub("<@",""):gsub(">","")
local user = guild:getMember(b)
if user.nickname == nil then
return user.name
else
return user.nickname
end
else
local b = str:gsub("<@",""):gsub(">","")
local user = client:getUser(b)
return user.name
end
end
function getinfo(guild)
if guild ~= nil then
local membercount = 0
local botcount = 0
for member in guild.members:iter() do
if member.user.bot then
botcount = botcount + 1
else
membercount = membercount + 1
end
end
return {membercount, botcount}
else
return false
end
end
function getchannels(guild)
if guild ~= nil then
local members = load("data/"..guild.id..".m")
local bots = load("data/"..guild.id..".b")
if members == false then return false end
if bots == false then return false end
local mchannel = guild:getChannel(members)
local bchannel = guild:getChannel(bots)
print(mchannel)
print(bchannel)
if mchannel == nil then return false end
if bchannel == nil then return false end
return {mchannel, bchannel}
else
return false
end
end
client:on('ready', function()
print('Logged in as '.. client.user.username)
end)
client:on('memberJoin', function(member)
local guildinfo = getinfo(member.guild)
local guildchns = getchannels(member.guild)
if guildchns ~= false and guildinfo ~= false then
guildchns[1]:setName("Members: "..guildinfo[1])
guildchns[2]:setName("🤖Bots: "..guildinfo[2])
end
end)
client:on('memberLeave', function(member)
local guildinfo = getinfo(member.guild)
local guildchns = getchannels(member.guild)
if guildchns ~= false and guildinfo ~= false then
guildchns[1]:setName("Members: "..guildinfo[1])
guildchns[2]:setName("🤖Bots: "..guildinfo[2])
end
end)
client:on('messageCreate', function(message)
if message.content == prefix .. 'ping' and message.author.id == client.owner.id then
message.channel:send({embed = {title = "Ping?",description = "Pong!",color = 0x00FFFF}})
end
if message.content == prefix .. 'shutdown' then
if message.author.id == "867901290336223242" then
message.channel:send("aight byte")
client:stop()
end
end
if message.content == prefix .. 'update' then
local guildinfo = getinfo(message.guild)
local guildchns = getchannels(message.guild)
if guildchns ~= false and guildinfo ~= false then
guildchns[1]:setName("Members: "..guildinfo[1])
guildchns[2]:setName("🤖Bots: "..guildinfo[2])
else
message.channel:send("update unsuccessful")
end
end
if message.content:startswith(prefix .. "botchn") then
if message.author.id == "867901290336223242" or message.author.id == message.guild.owner.user.id then
local chn = message.content:gsub(prefix.."botchn ","")
write("data/"..message.guild.id..".b",chn)
message.channel:send("bot stat channel set")
end
end
if message.content:startswith(prefix .. "memchn") then
if message.author.id == "867901290336223242" or message.author.id == message.guild.owner.user.id then
local chn = message.content:gsub(prefix.."memchn ","")
write("data/"..message.guild.id..".m",chn)
message.channel:send("member stat channel set")
end
end
end)
client:run('Bot ' .. load("token.txt.noinclude"))