Compare commits

..

2 Commits

Author SHA1 Message Date
171bc65bf4 activitytracker is ready? 2021-09-29 14:56:36 -07:00
9dd975cf89 ashfuguisdfhglisdhfg wip 2021-09-29 13:38:26 -07:00
4 changed files with 99 additions and 2 deletions

95
activitytracker.js Normal file
View File

@ -0,0 +1,95 @@
var client = require("./client");
var config = require("./config");
var app = require("./www");
var DataStore = require("./datastore");
var ds = new DataStore("activity");
app.get("/detect/:userid", (req, res) => {
res.sendFile(process.cwd() + "/track-image.png");
if (req.headers["user-agent"].includes("Discordbot")) return;
onActivity(req.params.userid);
});
client.on("messageCreate", m => m.guildId == config.guild && onActivity(m.author.id));
client.on("interactionCreate", i => i.guildId == config.guild && onActivity(i.user.id));
client.on("typingStart", t => t.guild?.id == config.guild && onActivity(t.user.id));
client.on("guildMemberAdd", m => m.guild.id == config.guild && onActivity(m.user.id));
client.on("messageReactionAdd", (r, user) => r.message?.guild?.id == config.guild && onActivity(user.id));
client.on("messageReactionRemove", (r, user) => r.message?.guild?.id == config.guild && onActivity(user.id));
client.on("messageUpdate", (oldMessage, newMessage) => (oldMessage.editedAt != newMessage.editedAt) && onActivity(newMessage.author.id));
async function onActivity(user_id) {
if (!user_id) return;
ds.set(user_id, Date.now());
if (ds.get(user_id + "deactivated")) {
reactivateMember(client.guilds.resolve(config.guild).members.resolve(user_id));
}
}
module.exports.interval = setInterval(() => {
var guild = client.guilds.resolve(config.guild);
if (!guild) return;
guild.members.cache.filter(m => !m.user.bot).forEach(member => {
let lastActivityTime = ds.get(member.user.id);
if (!lastActivityTime) return;
if (Date.now() - lastActivityTime > 1000*60*60*24) { // if last activity > 24 hours ago
if (!ds.get(member.id + "deactivated"))
deactivateMember(member);
}
});
}, 60*60*1000); // every hour i guess
async function deactivateMember(member) {
//if (ds.get(member.id + "deactivated")) return "member is already deactivated";
await member.roles.add(config.inactive_role);
ds.set(member.id + "deactivated");
var magic_channel = client.channels.resolve(ds.get(member.id + "magicchannelid"));
if (!magic_channel) {
magic_channel = await member.guild.channels.create("inactive", {
permissionOverwrites: [
{
id: this.guild.roles.everyone,
deny: "VIEW_CHANNEL"
},
{
id: client.user,
allow: "VIEW_CHANNEL"
},
{
id: this.user,
allow: "VIEW_CHANNEL"
}
]
});
ds.put(member.id + "magicchannelid", magic_channel.id);
} else {
magic_channel.permissionOverwrites.edit(member, {"VIEW_CHANNEL": true});
}
var magic_channel_message = await magic_channel.messages.fetch(ds.get(member.id + "magicchannelmessage"));
if (!magic_channel_message) {
magic_channel_message = await magic_channel.send(`${config.base_uri}/detect/${this.user.id}?${Math.random()}`);
ds.put(member.id + "magicchannelmessage", magic_channel_message.id)
} else {
await magic_channel_message.edit(`${config.base_uri}/detect/${this.user.id}?${Math.random()}`);
}
}
async function reactivateMember(member) {
//if (!ds.get(member.id + "deactivated")) return "member is not deactivated";
await member.roles.remove(config.inactive_role);
ds.del(member.id + "deactivated");
var magic_channel = client.channels.resolve(ds.get(member.id + "magicchannelid"));
if (magic_channel) magic_channel.permissionOverwrites.edit(member, {"VIEW_CHANNEL": false});
}
module.exports.deactivateMember = deactivateMember;
module.exports.reactivateMember = reactivateMember;

View File

@ -7,6 +7,7 @@ module.exports = {
admin_role: "776899554603565116", admin_role: "776899554603565116",
human_role: "672956630962274306", human_role: "672956630962274306",
bot_role: "673671040010027034", bot_role: "673671040010027034",
inactive_role: "892869309603389500",
eval_undefined_emoji: "707729833601531935", eval_undefined_emoji: "707729833601531935",
mi_emoji: "887931046086185060", mi_emoji: "887931046086185060",
ki_emoji: "887935846710394910", ki_emoji: "887935846710394910",

View File

@ -13,3 +13,4 @@ require('./漢字ゲーム');
require('./translate2'); require('./translate2');
require('./world-clock'); require('./world-clock');
require('./buttonthing'); require('./buttonthing');
require("./activitytracker");

View File

@ -42,7 +42,7 @@ module.exports = class MagicChannel extends EventEmitter{
}).then(channel => { }).then(channel => {
this.channel = channel; this.channel = channel;
ds.put(this.user.id + "channel", channel.id); ds.put(this.user.id + "channel", channel.id);
channel.send(`${config.base_uri}/${this.user.id}`).then(message => { channel.send(`${config.base_uri}/${this.user.id}?${Math.random()}`).then(message => {
ds.put(this.user.id + "message", message.id); ds.put(this.user.id + "message", message.id);
}); });
}); });