Skip to content

Commit 1cdbd36

Browse files
committed
Bug fix: moderator notification received server ID versus the server record directly.
Changes to be committed: modified: Companion modified: LauncherCompanion
1 parent 7c5c863 commit 1cdbd36

File tree

2 files changed

+87
-14
lines changed

2 files changed

+87
-14
lines changed

Companion

Lines changed: 83 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,39 @@
1818
# for now, it works well and is quite fast.
1919

2020
# ***** IMPORTANT:
21-
# This program uses ADMINISTRATOR priviledges.
21+
# This program MAY still require ADMINISTRATOR priviledges. Work is being done to
22+
# curb that.
23+
#
24+
# The following permissions are NOW required:
25+
# create_instant_invite, kick_members, ban_members,
26+
# manage_channels, manage_guild, add_reactions, view_audit_log,
27+
# view_channel, manage_roles, manage_permissions, manage_webhooks,
28+
# manage_emojis_and_stickers, send_messages, send_tts_messages,
29+
# manage_messages, embed_links, attach_files, read_message_history,
30+
# mention_everyone, manage_threads, use_embedded_activities,
31+
# send_messages_in_threads, use_application_commands, manage_events
32+
33+
# According to Discord documentation,
34+
# https://discord.com/community/permissions-on-discord-discord
35+
#
36+
# Without the Administrator permission:
37+
#
38+
# The role cannot bypass channel-specific permissions unless explicitly granted
39+
# in each channel.
40+
#
41+
# Cannot access permissions higher than its own role in the hierarchy. (This
42+
# appears to no longer work).
2243

2344
# Forum and thread support is a strange approach. Threads are treated like
2445
# channels, with a few extra bits. Webhooks, slowdown mode, edits, even the way
25-
# message are sent into the thread is effected. Activitieshave to be tested and
46+
# message are sent into the thread is effected. Activities have to be tested and
2647
# is it is a thread, most of the time, you have to pull the parent channel.
2748
# Webhooks and slowdown mode, in particular, do NOT have separate
2849
# functionalities of a channel, even though discord treats them like a channel.
2950

3051
### *** At some point, I'm going to really have to rewrite this. The forum/thread
3152
### detection code is a stinking hot mess and scattered everywhere.
3253

33-
# Areas where a classifier can improve functionality (at a higher cost):
34-
# Phone number detection would have a significantly lower lecel of false
35-
# positives.
36-
#
37-
# Technical messages versus non-technical for varying the temperature of
38-
# the conversation.
39-
4054
# Emotion scoring (classifier)
4155
#
4256
# Emotional scoring (major scale): 10 (love) to -10 (hatred) sets the
@@ -122,6 +136,7 @@
122136
# "THREAT", "SEXUALLY_EXPLICIT", "FLIRTATION", "PERSONAL ATTACK",
123137
# "INFLAMMATORY", "OBSCENE", "BULLYING"
124138

139+
# In consideration/progres
125140
# Anti nuke
126141
# Ticket management
127142
# Anti nudity verification for images.
@@ -175,7 +190,7 @@ from googleapiclient.discovery import build
175190

176191
# Active version
177192

178-
Version="0.0.0.0.1250"
193+
Version="0.0.0.0.1265"
179194

180195
# The running name of the program. Must be global and NEVER changing.
181196

@@ -350,6 +365,7 @@ def function_trapper(failed_result=None):
350365

351366
# Cheap mkdir
352367

368+
@function_trapper(None)
353369
def mkdir(fn):
354370
if not os.path.exists(fn):
355371
os.makedirs(fn,exist_ok=True)
@@ -373,13 +389,15 @@ def ReadFile(fn,binary=False):
373389

374390
# Append a single line to an existing file
375391

392+
@function_trapper(None)
376393
def AppendFile(fname,text):
377394
fh=open(fname,'a+')
378395
fh.write(text)
379396
fh.close()
380397

381398
# Write file to disk
382399

400+
@function_trapper(None)
383401
def WriteFile(fn,data):
384402
cf=open(fn,'w')
385403
cf.write(data)
@@ -822,7 +840,6 @@ def WriteLog(gid,uid,channel,text):
822840
AppendFile(fn,s)
823841
except Exception as err:
824842
print(f'LOG Broke: {err}')
825-
pass
826843
LoggingLock.release()
827844

828845
# Log errors
@@ -1734,7 +1751,10 @@ def GetOpenRouter(apikey,messages,model,freqpenalty,temperature,timeout):
17341751
timeout=timeout
17351752
)
17361753
clientAI.close()
1737-
response=completion.choices[0].message.content.strip()
1754+
try:
1755+
response=completion.choices[0].message.content.strip()
1756+
except Exception as err:
1757+
return None
17381758
RawLog(f"OpenRouter/{model}: {str(completion)}")
17391759
return response
17401760

@@ -2440,6 +2460,13 @@ async def VerifyCompanionAdministration(guild):
24402460
if orole not in guild.owner.roles:
24412461
await guild.owner.add_roles(orole)
24422462

2463+
# Push it to the highest position
2464+
# await guild.edit_role_positions(positions={orole: len(guild.roles)-1})
2465+
# else:
2466+
# Push it to the highest position
2467+
# if orole!=guild.roles[-1]:
2468+
# await guild.edit_role_positions(positions={orole: len(guild.roles)-1})
2469+
24432470
# Check for the 'companion-administration' channel
24442471
channel=discord.utils.get(guild.text_channels, name='companion-administration')
24452472

@@ -2466,6 +2493,36 @@ async def VerifyCompanionAdministration(guild):
24662493
channel.edit(overwrites=overwrites)
24672494
return channel
24682495

2496+
# Verify all required permissions
2497+
2498+
@function_trapper
2499+
def VerifyGuildPermissions(guild):
2500+
# List of non-voice-related permissions
2501+
non_voice_permissions = [
2502+
"create_instant_invite", "kick_members", "ban_members", "administrator",
2503+
"manage_channels", "manage_guild", "add_reactions", "view_audit_log",
2504+
"view_channel", "manage_roles", "manage_permissions", "manage_webhooks",
2505+
"manage_emojis_and_stickers", "send_messages", "send_tts_messages",
2506+
"manage_messages", "embed_links", "attach_files", "read_message_history",
2507+
"mention_everyone", "manage_threads", "use_embedded_activities",
2508+
"send_messages_in_threads", "use_application_commands", "manage_events"
2509+
]
2510+
2511+
# Get the bot's current guild permissions
2512+
bot_permissions = guild.me.guild_permissions
2513+
2514+
# Find missing permissions
2515+
missing_permissions = [perm for perm in non_voice_permissions if not getattr(bot_permissions, perm)]
2516+
2517+
# Print missing permissions if any
2518+
if missing_permissions:
2519+
print(f"{guild.id} Missing permissions:")
2520+
for perm in missing_permissions:
2521+
print(f"- {perm}")
2522+
return False # Not all permissions are enabled
2523+
2524+
return True # All permissions are enabled
2525+
24692526
# This function checks the security setup of a Discord server to ensure
24702527
# it’s well-protected and highlights any weak points. It verifies if an
24712528
# "owner" role exists, has the highest permissions, and is assigned to the
@@ -2490,6 +2547,8 @@ async def SecurityAudit(guild=None,message=None,ShowResults=False,auto=False):
24902547
# Create the CompanionAdministration area (if not exist) and get the channel
24912548
admin=await VerifyCompanionAdministration(guild)
24922549

2550+
PermChk=VerifyGuildPermissions(guild)
2551+
24932552
# Start with the automatic setting. Results will be forced if audit fails
24942553
FailedAudit=False
24952554
AuditOwnerRole=False
@@ -2549,6 +2608,8 @@ async def SecurityAudit(guild=None,message=None,ShowResults=False,auto=False):
25492608
if ShowResults or FailedAudit:
25502609
await admin.send(f"Guild: {guild.name} ({guild.id})")
25512610
await admin.send(f"Owner: {guild.owner} ({guild.owner.id})")
2611+
if not PermChk:
2612+
await admin.send(f"This guild does NOT have the proper permissions set to function.")
25522613
if not AuditOwnerRole:
25532614
await admin.send(f"The `owner` role does not exist. A dedicated guild owner role is vital for securing control, ensuring only trusted hands hold top permissions. This role acts as the ultimate safeguard against unauthorized changes, breaches, and ensures accountability in managing server integrity.")
25542615
if not IsHighestPos:
@@ -2615,7 +2676,12 @@ async def ImposterAmoungUs(guild, member):
26152676
return True
26162677
return False
26172678

2618-
# Check wwhitelist
2679+
# This function acts as a bypass for regular system measures, granting special
2680+
# permissions to **trusted** users, roles, or channels listed in a whitelist. It
2681+
# checks if a user’s ID, their role name, or the channel name matches an entry in
2682+
# the bot’s predefined whitelist. If a match is found, it allows the action without
2683+
# enforcing standard restrictions. This ensures trusted individuals or channels can
2684+
# operate freely, even when other controls are in place.
26192685

26202686
def CheckWhitelist(bot,whitelist,message=None,user=None):
26212687
if message==None and author==None:
@@ -3328,7 +3394,7 @@ async def on_typing(channel, user, when):
33283394
# Determine slow mode duration
33293395
if typing_count>2 * expected_users: # Over threshold
33303396
bot=GetCompanionPersona(server.id,None,Welcome=True)
3331-
await ModeratorNotify(bot,server.id,f"Under potential raid attack, {joins} joins, {typing_count} users>{expected_users} users")
3397+
await ModeratorNotify(bot,server,f"Under potential raid attack, {joins} joins, {typing_count} users>{expected_users} users")
33323398
slow_mode_duration=SlowModeMultiplier * typing_count*10*joins
33333399
elif typing_count>expected_users:
33343400
slow_mode_duration=SlowModeMultiplier * typing_count*joins
@@ -3734,6 +3800,9 @@ async def on_ready():
37343800

37353801
# Print some fluff
37363802
for guild in client.guilds:
3803+
if not VerifyGuildPermissions(guild):
3804+
print(f"{guild.id} does NOT have the proper permissions to function.")
3805+
37373806
# Add the guild to the circular guild queue, if not already in it.
37383807
if guild.id not in GuildQueue and GuildQueueLock.acquire(timeout=GuildQueueTimeout):
37393808
GuildQueue.append(guild)

LauncherCompanion

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
# Crontab entry ( without hash mark #
44
# @reboot ( /home/Companion/LauncherCompanion & ) > /dev/null 2>&1
55

6+
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
7+
8+
#source /home/RAPMD/bin/activate
9+
610
export OLLAMA_HOST=http://127.0.0.1:11434
711

812
BaseDir="/home/Companion"

0 commit comments

Comments
 (0)