os-release-bot/bot.py

114 lines
4.0 KiB
Python

# Code written by Jocadbz(madureirajoaquim95@gmail.com)
# Code revised by BodgeMaster
# Code Licensed under GPL 3.0
import sys, time, json, datetime
import discord
from discord import app_commands
import typing
from discord.ext import commands
import discord.utils
BOOT_TIME = time.time()
config_file = open("config.json", "r")
config = json.loads(config_file.read());
config_file.close()
def uptime():
return str(datetime.timedelta(seconds=int(time.time() - BOOT_TIME)))
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('------')
@client.tree.command(name="join_role")
@app_commands.describe(distro='Assign 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.add_roles(role)
await interaction.response.send_message(content=f'Added {role}.')
print("Added role.")
except:
await interaction.response.send_message(content='Something went wrong.')
print("Failed to add role. Permissions set correctly?")
@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?")
# This context menu command only works on messages
@client.tree.context_menu(name='Report to Moderators')
async def report_message(interaction: discord.Interaction, message: discord.Message):
if not interaction.guild.id==config["guild"]:
return
await interaction.response.send_message(
f'Thanks for reporting.', ephemeral=True
)
log_channel = interaction.guild.get_channel(config["report channel"])
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))
embed.add_field(name="Reported by:", value=interaction.user, inline=False)
await log_channel.send(embed=embed, view=url_view)
@client.tree.command(name="status")
async def status(interaction: discord.Interaction):
embed = discord.Embed(title='/etc/os-release bot by jocadbz')
embed.add_field(name="Uptime:", value=uptime(), inline=True)
embed.add_field(name="Version:", value=config["bot version"], inline=True)
embed.set_footer(text=config["bot issues"])
url_view = discord.ui.View()
url_view.add_item(discord.ui.Button(label='View source...', style=discord.ButtonStyle.url, url=config["bot source url"]))
await interaction.response.send_message(embed=embed, view=url_view)
print("Status queried.")
client.run(sys.argv[1])