2022-06-09 01:59:15 +00:00
|
|
|
import datetime as dt
|
|
|
|
import hikari
|
|
|
|
import lightbulb
|
|
|
|
from lightbulb.ext import tasks
|
|
|
|
|
|
|
|
import config as c
|
|
|
|
|
|
|
|
|
|
|
|
plugin = lightbulb.Plugin("activity")
|
|
|
|
|
|
|
|
|
|
|
|
# Check every minute if inactive
|
|
|
|
@tasks.task(s=60)
|
|
|
|
async def check_activity():
|
2022-07-05 22:19:59 +00:00
|
|
|
for author_id, timestamp in c.db.items():
|
2022-08-16 22:36:37 +00:00
|
|
|
# If time between now and timestamp >= duration
|
2022-06-09 01:59:15 +00:00
|
|
|
if dt.datetime.now(dt.timezone.utc) - timestamp >= dt.timedelta(seconds=c.config["duration"]):
|
2022-07-24 03:08:59 +00:00
|
|
|
try:
|
2022-08-16 18:57:50 +00:00
|
|
|
# Acquire member object
|
2022-07-24 03:08:59 +00:00
|
|
|
member = plugin.bot.cache.get_member(
|
|
|
|
c.config["guild"], author_id
|
|
|
|
) or await plugin.bot.rest.fetch_member(c.config["guild"], author_id)
|
|
|
|
|
2022-08-16 18:59:27 +00:00
|
|
|
# Delete member from db if it has excluded role
|
|
|
|
if c.config["exclude"] and c.config["exclude"] in member.role_ids:
|
|
|
|
del c.db[author_id]
|
|
|
|
continue
|
|
|
|
|
2022-08-16 18:57:50 +00:00
|
|
|
# Enforce activity roles
|
2022-07-24 03:08:59 +00:00
|
|
|
if c.config["active"] and c.config["active"] in member.role_ids:
|
|
|
|
await member.remove_role(c.config["active"])
|
|
|
|
if c.config["inactive"] and c.config["inactive"] not in member.role_ids:
|
|
|
|
await member.add_role(c.config["inactive"])
|
2022-08-16 18:57:50 +00:00
|
|
|
# Delete member from db if not found
|
2022-07-24 03:08:59 +00:00
|
|
|
except hikari.NotFoundError:
|
|
|
|
del c.db[author_id]
|
2022-06-09 01:59:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Listener for bot ready
|
|
|
|
@plugin.listener(hikari.StartedEvent)
|
|
|
|
async def on_ready(event):
|
|
|
|
check_activity.start()
|
|
|
|
|
|
|
|
|
|
|
|
# Listener for guild messages
|
|
|
|
@plugin.listener(hikari.GuildMessageCreateEvent)
|
|
|
|
async def on_message(event):
|
2022-08-16 18:57:50 +00:00
|
|
|
# Exclude bots, unselected guild, and excluded role
|
2022-06-14 23:21:49 +00:00
|
|
|
if event.is_bot or event.guild_id != c.config["guild"] or c.config["exclude"] in event.member.role_ids:
|
2022-06-09 01:59:15 +00:00
|
|
|
return
|
|
|
|
|
2022-08-16 18:57:50 +00:00
|
|
|
# Insert current timestamp into db
|
2022-07-05 22:19:59 +00:00
|
|
|
c.db[event.author_id] = dt.datetime.now(dt.timezone.utc) # or event.message.timestamp
|
2022-06-09 01:59:15 +00:00
|
|
|
|
2022-08-16 18:57:50 +00:00
|
|
|
# Toggle activity roles
|
2022-06-09 01:59:15 +00:00
|
|
|
if c.config["active"] and c.config["active"] not in event.member.role_ids:
|
|
|
|
await event.member.add_role(c.config["active"])
|
|
|
|
if c.config["inactive"] and c.config["inactive"] in event.member.role_ids:
|
|
|
|
await event.member.remove_role(c.config["inactive"])
|
|
|
|
|
|
|
|
|
2022-06-09 03:22:19 +00:00
|
|
|
# Listener for voice state
|
|
|
|
@plugin.listener(hikari.VoiceStateUpdateEvent)
|
|
|
|
async def on_voice(event):
|
2022-08-16 18:57:50 +00:00
|
|
|
# Exclude bots, unselected guild, and excluded role
|
2022-06-14 23:21:49 +00:00
|
|
|
if (
|
|
|
|
event.state.member.is_bot
|
|
|
|
or event.guild_id != c.config["guild"]
|
|
|
|
or c.config["exclude"] in event.state.member.role_ids
|
|
|
|
):
|
2022-06-09 03:22:19 +00:00
|
|
|
return
|
|
|
|
|
2022-08-16 18:57:50 +00:00
|
|
|
# Insert current timestamp into db
|
|
|
|
c.db[event.state.user_id] = dt.datetime.now(dt.timezone.utc) # or event.message.timestamp
|
2022-06-09 03:22:19 +00:00
|
|
|
|
2022-08-16 18:57:50 +00:00
|
|
|
# Toggle activity roles
|
2022-06-09 03:22:19 +00:00
|
|
|
if c.config["active"] and c.config["active"] not in event.state.member.role_ids:
|
|
|
|
await event.state.member.add_role(c.config["active"])
|
|
|
|
if c.config["inactive"] and c.config["inactive"] in event.state.member.role_ids:
|
|
|
|
await event.state.member.remove_role(c.config["inactive"])
|
|
|
|
|
|
|
|
|
2022-06-09 01:59:15 +00:00
|
|
|
def load(bot):
|
|
|
|
bot.add_plugin(plugin)
|
|
|
|
|
|
|
|
|
|
|
|
def unload(bot):
|
|
|
|
bot.remove_plugin(plugin)
|