2023-01-12 18:12:58 +01:00
# Code written by Jocadbz(madureirajoaquim95@gmail.com)
# Code revised by BodgeMaster
# Code Licensed under GPL 3.0
2023-01-13 19:34:04 +01:00
import sys , time , json
2023-01-12 18:12:58 +01:00
import discord
from discord import app_commands
import typing
from discord . ext import commands
import discord . utils
2023-01-13 17:47:23 +01:00
BOOT_TIME = time . time ( )
2023-01-12 18:12:58 +01:00
2023-01-13 19:34:24 +01:00
config_file = open ( " config.json " , " r " )
config = json . loads ( config_file . read ( ) ) ;
config_file . close ( )
2023-01-12 18:12:58 +01:00
def seconds_elapsed ( ) :
2023-01-13 20:31:53 +01:00
return int ( time . time ( ) - BOOT_TIME )
2023-01-12 18:12:58 +01:00
class MyClient ( discord . Client ) :
def __init__ ( self , * , intents : discord . Intents ) :
super ( ) . __init__ ( intents = intents )
self . tree = app_commands . CommandTree ( self )
async def setup_hook ( self ) :
await self . tree . sync ( )
intents = discord . Intents . default ( )
client = MyClient ( intents = intents )
bot = commands . Bot ( command_prefix = ' lb! ' , intents = intents )
@client.event
async def on_ready ( ) :
print ( f ' Logged in as { client . user } (ID: { client . user . id } ) ' )
print ( ' ------ ' )
2023-01-13 21:34:49 +01:00
@client.tree.command ( name = " join_role " )
2023-01-12 18:12:58 +01:00
@app_commands.describe ( distro = ' Assign yourself a role ' )
2023-01-13 20:34:50 +01:00
async def distro ( interaction : discord . Interaction , distro : eval ( " typing.Literal[ \" " + " \" , \" " . join ( config [ " roles " ] . keys ( ) ) + " \" ] " ) ) :
2023-01-13 20:31:53 +01:00
if not interaction . guild . id == config [ " guild " ] :
print ( " Role assignment attempted from unknown guild. " )
return
2023-01-12 18:12:58 +01:00
user = interaction . user
2023-01-13 20:34:50 +01:00
role = discord . utils . get ( interaction . guild . roles , id = config [ " roles " ] [ distro ] )
try :
2023-01-12 18:12:58 +01:00
await user . add_roles ( role )
2023-01-13 20:34:50 +01:00
await interaction . response . send_message ( content = f ' Added { role } . ' )
2023-01-13 21:34:49 +01:00
print ( " Added role. " )
2023-01-13 20:34:50 +01:00
except :
await interaction . response . send_message ( content = ' Something went wrong. ' )
print ( " Failed to add role. Permissions set correctly? " )
2023-01-12 18:12:58 +01:00
2023-01-13 21:34:49 +01:00
@client.tree.command ( name = " leave_role " )
@app_commands.describe ( distro = ' Unassign yourself a role ' )
async def distro ( interaction : discord . Interaction , distro : eval ( " typing.Literal[ \" " + " \" , \" " . join ( config [ " roles " ] . keys ( ) ) + " \" ] " ) ) :
if not interaction . guild . id == config [ " guild " ] :
print ( " Role assignment attempted from unknown guild. " )
return
user = interaction . user
role = discord . utils . get ( interaction . guild . roles , id = config [ " roles " ] [ distro ] )
try :
await user . remove_roles ( role )
await interaction . response . send_message ( content = f ' Left { role } . ' )
print ( " Removed role. " )
except :
await interaction . response . send_message ( content = ' Something went wrong. ' )
print ( " Failed to remove role. Permissions set correctly? " )
2023-01-12 18:12:58 +01:00
# This context menu command only works on messages
2023-01-13 20:31:53 +01:00
@client.tree.context_menu ( name = ' Report to Moderators ' )
2023-01-12 18:12:58 +01:00
async def report_message ( interaction : discord . Interaction , message : discord . Message ) :
2023-01-13 20:31:53 +01:00
if not interaction . guild . id == config [ " guild " ] :
return
2023-01-12 18:12:58 +01:00
await interaction . response . send_message (
2023-01-13 20:50:57 +01:00
f ' Thanks for reporting. ' , ephemeral = True
2023-01-12 18:12:58 +01:00
)
2023-01-13 20:50:57 +01:00
log_channel = interaction . guild . get_channel ( config [ " report channel " ] )
2023-01-12 18:12:58 +01:00
embed = discord . Embed ( title = ' Reported Message ' )
if message . content :
embed . description = message . content
embed . set_author ( name = message . author . display_name , icon_url = message . author . display_avatar . url )
embed . timestamp = message . created_at
url_view = discord . ui . View ( )
url_view . add_item ( discord . ui . Button ( label = ' Go to Message ' , style = discord . ButtonStyle . url , url = message . jump_url ) )
2023-01-20 02:03:35 +01:00
embed . add_field ( name = " Reported by: " , value = interaction . user , inline = False )
2023-01-12 18:12:58 +01:00
await log_channel . send ( embed = embed , view = url_view )
@client.tree.command ( name = " status " )
async def status ( interaction : discord . Interaction ) :
embed = discord . Embed ( title = ' System Stats ' )
2023-01-20 02:15:19 +01:00
embed . add_field ( name = " Uptime: " , value = seconds_elapsed ( ) , inline = True )
embed . add_field ( name = " System: " , value = sys . platform , inline = True )
embed . add_field ( name = " Version: " , value = " v1.2.0 " , inline = True )
2023-01-12 18:12:58 +01:00
url_view = discord . ui . View ( )
2023-01-20 02:15:19 +01:00
url_view . add_item ( discord . ui . Button ( label = ' See the source code ' , style = discord . ButtonStyle . url , url = " https://lostcave.ddnss.de/git/jocadbz/os-release-bot " ) )
url_view . add_item ( discord . ui . Button ( label = ' Report a issue ' , style = discord . ButtonStyle . url , url = " https://lostcave.ddnss.de/git/jocadbz/os-release-bot/issues " ) )
2023-01-12 18:12:58 +01:00
await interaction . response . send_message ( embed = embed , view = url_view )
2023-01-13 20:31:53 +01:00
print ( " Status queried. " )
2023-01-12 18:12:58 +01:00
2023-01-13 17:47:58 +01:00
client . run ( sys . argv [ 1 ] )