-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
145 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import nextcord | ||
from nextcord.ext import commands, tasks | ||
import utils.settings as s | ||
import requests | ||
import re | ||
import logging | ||
import os | ||
import asyncio | ||
|
||
log_directory = s.chatlog_path | ||
webhook_url = s.chatlog_webhookurl | ||
|
||
class ChatFeedCog(commands.Cog): | ||
def __init__(self, bot): | ||
self.bot = bot | ||
self.log_directory = log_directory | ||
self.webhook_url = webhook_url | ||
self.first_check_done = False | ||
self.last_processed_line = None | ||
self.current_log_file = None | ||
self.check_logs.start() | ||
self.blocked_phrases = ["/adminpassword", "/creativemenu", "/"] | ||
|
||
def cog_unload(self): | ||
self.check_logs.cancel() | ||
|
||
@tasks.loop(seconds=8) | ||
async def check_logs(self): | ||
try: | ||
files = sorted( | ||
[ | ||
f for f in os.listdir(self.log_directory) | ||
if f.endswith(".txt") or f.endswith(".log") | ||
], | ||
key=lambda x: os.stat(os.path.join(self.log_directory, x)).st_mtime, | ||
reverse=True | ||
) | ||
if not files: | ||
return | ||
|
||
newest_file = os.path.join(self.log_directory, files[0]) | ||
if self.current_log_file != newest_file: | ||
self.current_log_file = newest_file | ||
self.last_processed_line = None | ||
self.first_check_done = False | ||
|
||
with open(self.current_log_file, "r", encoding="utf-8", errors="ignore") as file: | ||
content = file.read() | ||
lines = content.splitlines() | ||
|
||
if not self.first_check_done: | ||
if lines: | ||
self.last_processed_line = lines[-1] | ||
self.first_check_done = True | ||
return | ||
|
||
new_lines_start = False | ||
for line in lines: | ||
if line == self.last_processed_line: | ||
new_lines_start = True | ||
continue | ||
if new_lines_start or self.last_processed_line is None: | ||
if "[Chat::" in line: | ||
await self.process_and_send(line) | ||
|
||
if lines: | ||
self.last_processed_line = lines[-1] | ||
except: | ||
pass | ||
|
||
async def process_and_send(self, line): | ||
try: | ||
match = re.search(r"\[Chat::(?:Global|Local|Guild)\]\['([^']+)'.*\]: (.*)", line) | ||
if match: | ||
username, message = match.groups() | ||
if any(bp in message for bp in self.blocked_phrases): | ||
return | ||
requests.post(self.webhook_url, json={"username": username, "content": message}) | ||
await asyncio.sleep(1) | ||
except: | ||
pass | ||
|
||
@check_logs.before_loop | ||
async def before_check_logs(self): | ||
await self.bot.wait_until_ready() | ||
|
||
def setup(bot): | ||
if not os.getenv("CHATLOG_PATH") or not os.getenv("CHATLOG_WEBHOOKURL"): | ||
logging.error("Chatlog path or webhook URL not set.") | ||
return | ||
bot.add_cog(ChatFeedCog(bot)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import nextcord | ||
from nextcord.ext import commands | ||
import os | ||
from utils.database import get_server_details | ||
from utils.rconutility import RconUtility | ||
import utils.settings as s | ||
import logging | ||
|
||
sftp_channel_id = s.chatlog_channel | ||
server_name = s.chatlog_servername | ||
|
||
class ChatRelayCog(commands.Cog): | ||
def __init__(self, bot): | ||
self.bot = bot | ||
self.sftp_channel_id = sftp_channel_id | ||
self.server_name = server_name | ||
self.rcon_util = RconUtility() | ||
|
||
@commands.Cog.listener() | ||
async def on_message(self, message: nextcord.Message): | ||
if message.author.bot: | ||
return | ||
|
||
if message.channel.id != self.sftp_channel_id: | ||
return | ||
|
||
if not message.content: | ||
return | ||
|
||
server_details = await get_server_details(self.server_name) | ||
if not server_details: | ||
return | ||
|
||
broadcast_message = f"[Discord] {message.content}" | ||
server_info = { | ||
"name": self.server_name, | ||
"host": server_details[0], | ||
"port": server_details[1], | ||
"password": server_details[2], | ||
} | ||
|
||
await self.rcon_util.rcon_command(server_info, f"Broadcast {broadcast_message}") | ||
|
||
def setup(bot): | ||
if not os.getenv("CHATLOG_CHANNEL"): | ||
logging.error("Chat log channel env variable not set. Chat feed will not be loaded.") | ||
return | ||
bot.add_cog(ChatRelayCog(bot)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters