changed a few things
This commit is contained in:
@@ -10,14 +10,10 @@ from random import shuffle
|
|||||||
from os import environ as env
|
from os import environ as env
|
||||||
|
|
||||||
# Channel ids
|
# Channel ids
|
||||||
CUSTOM_CHANNELS = set([
|
|
||||||
int(env['CHANNEL_ID'])
|
|
||||||
])
|
|
||||||
|
|
||||||
TOKEN = env['TOKEN']
|
TOKEN = env['TOKEN']
|
||||||
MINUTES = 30
|
MINUTES = 30
|
||||||
ROLE = int(env['ROLE'])
|
ROLE = int(env['ROLE'])
|
||||||
CHANNELS = set([])
|
CHANNEL = None
|
||||||
|
|
||||||
def decode(value):
|
def decode(value):
|
||||||
return b64decode(value).decode('utf-8')
|
return b64decode(value).decode('utf-8')
|
||||||
@@ -33,9 +29,8 @@ bot = commands.Bot(command_prefix="!", intents=intents)
|
|||||||
@bot.event
|
@bot.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
print(f"Logged in as {bot.user}")
|
print(f"Logged in as {bot.user}")
|
||||||
for cc in CUSTOM_CHANNELS:
|
CHANNEL = bot.get_channel(int(env['CHANNEL_ID']))
|
||||||
print(f'Custom channel: {cc}')
|
|
||||||
CHANNELS.add(bot.get_channel(cc))
|
|
||||||
check_inactivity.start()
|
check_inactivity.start()
|
||||||
|
|
||||||
active_trivia = {} # Format: {message_id: {"correct_answer": answer, "count": 0}}
|
active_trivia = {} # Format: {message_id: {"correct_answer": answer, "count": 0}}
|
||||||
@@ -45,36 +40,12 @@ async def check_inactivity():
|
|||||||
if ROLE is not None:
|
if ROLE is not None:
|
||||||
current_time = datetime.now(timezone.utc)
|
current_time = datetime.now(timezone.utc)
|
||||||
print(f"{current_time.strftime('%H:%M:%S')} - Running inactivity check")
|
print(f"{current_time.strftime('%H:%M:%S')} - Running inactivity check")
|
||||||
current_question = None
|
print(f"{datetime.now(timezone.utc).strftime('%H:%M:%S')} - Checking channel {CHANNEL.name}")
|
||||||
for channel in CHANNELS:
|
async for message in CHANNEL.history(limit=1):
|
||||||
print(f"{datetime.now(timezone.utc).strftime('%H:%M:%S')} - Checking channel {channel.name}")
|
|
||||||
async for message in channel.history(limit=1):
|
|
||||||
# seconds_since = (current_time - message.created_at).total_seconds()
|
|
||||||
# print(f"{datetime.now(timezone.utc).strftime('%H:%M:%S')} - Time since last message: {seconds_since} seconds")
|
|
||||||
minutes_since = (current_time - message.created_at).total_seconds() / 60
|
minutes_since = (current_time - message.created_at).total_seconds() / 60
|
||||||
print(f"{datetime.now(timezone.utc).strftime('%H:%M:%S')} - Time since last message: {minutes_since} minutes")
|
print(f"{datetime.now(timezone.utc).strftime('%H:%M:%S')} - Time since last message: {minutes_since} minutes")
|
||||||
if minutes_since > MINUTES:
|
if minutes_since > MINUTES:
|
||||||
print(f"{datetime.now(timezone.utc).strftime('%H:%M:%S')} - Channel is inactive, sending message")
|
|
||||||
if current_question is None:
|
|
||||||
current_question = requests.get(OPENTDB_URL).json()['results'][0]
|
|
||||||
|
|
||||||
decoded_data = {k: decode(v) if isinstance(v, str) else [decode(i) for i in v] for k, v in current_question.items()}
|
|
||||||
answers = list(decoded_data['incorrect_answers']) + [decoded_data['correct_answer']]
|
|
||||||
shuffle(answers)
|
|
||||||
|
|
||||||
role_mention = f"<@&{ROLE}>"
|
|
||||||
embedVar = discord.Embed(title=decoded_data['question'], description="", color=0x00ff00)
|
|
||||||
embedVar.add_field(name="Difficulty", value=str(decoded_data['difficulty']).capitalize(), inline=False)
|
|
||||||
embedVar.add_field(name="Answers", value='**'+('\n'.join(['- '+str(a) for a in answers]))+'**', inline=False)
|
|
||||||
msg = await channel.send(role_mention, embed=embedVar)
|
|
||||||
|
|
||||||
# Store this question in our tracking dict
|
|
||||||
active_trivia[msg.id] = {
|
|
||||||
"correct_answer": str(decoded_data['correct_answer']).lower(),
|
|
||||||
"count": 0,
|
|
||||||
"max_replies": 10
|
|
||||||
}
|
|
||||||
print(f"{datetime.now(timezone.utc).strftime('%H:%M:%S')} - Added trivia question ID {msg.id}")
|
|
||||||
|
|
||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
@@ -104,4 +75,26 @@ async def on_message(message):
|
|||||||
del active_trivia[message.reference.message_id]
|
del active_trivia[message.reference.message_id]
|
||||||
print(f"{datetime.now(timezone.utc).strftime('%H:%M:%S')} - Removed trivia question ID {message.reference.message_id} after {trivia_data['max_replies']} replies")
|
print(f"{datetime.now(timezone.utc).strftime('%H:%M:%S')} - Removed trivia question ID {message.reference.message_id} after {trivia_data['max_replies']} replies")
|
||||||
|
|
||||||
|
async def send_trivia():
|
||||||
|
print(f"{datetime.now(timezone.utc).strftime('%H:%M:%S')} - Channel is inactive, sending message")
|
||||||
|
current_question = requests.get(OPENTDB_URL).json()['results'][0]
|
||||||
|
|
||||||
|
decoded_data = {k: decode(v) if isinstance(v, str) else [decode(i) for i in v] for k, v in current_question.items()}
|
||||||
|
answers = list(decoded_data['incorrect_answers']) + [decoded_data['correct_answer']]
|
||||||
|
shuffle(answers)
|
||||||
|
|
||||||
|
role_mention = f"<@&{ROLE}>"
|
||||||
|
embedVar = discord.Embed(title=decoded_data['question'], description="", color=0x00ff00)
|
||||||
|
embedVar.add_field(name="Difficulty", value=str(decoded_data['difficulty']).capitalize(), inline=False)
|
||||||
|
embedVar.add_field(name="Answers", value='**'+('\n'.join(['- '+str(a) for a in answers]))+'**', inline=False)
|
||||||
|
msg = await CHANNEL.send(role_mention, embed=embedVar)
|
||||||
|
|
||||||
|
# Store this question in our tracking dict
|
||||||
|
active_trivia[msg.id] = {
|
||||||
|
"correct_answer": str(decoded_data['correct_answer']).lower(),
|
||||||
|
"count": 0,
|
||||||
|
"max_replies": 40
|
||||||
|
}
|
||||||
|
print(f"{datetime.now(timezone.utc).strftime('%H:%M:%S')} - Added trivia question ID {msg.id}")
|
||||||
|
|
||||||
bot.run(TOKEN)
|
bot.run(TOKEN)
|
||||||
|
|||||||
Reference in New Issue
Block a user