os-release-bot/bot.py

114 lines
4.0 KiB
Python
Raw Normal View History

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-21 20:44:50 +01:00
import sys, time, json, datetime
2023-01-13 19:34:04 +01:00
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-21 20:44:50 +01:00
def uptime():
return str(datetime.timedelta(seconds=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')
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
2023-01-12 18:12:58 +01:00
user = interaction.user
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)
await interaction.response.send_message(content=f'Added {role}.')
2023-01-13 21:34:49 +01:00
print("Added role.")
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
@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):
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))
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='/etc/os-release bot by jocadbz')
2023-01-21 20:44:50 +01:00
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"])
2023-01-12 18:12:58 +01:00
url_view = discord.ui.View()
url_view.add_item(discord.ui.Button(label='View source...', style=discord.ButtonStyle.url, url=config["bot source url"]))
2023-01-12 18:12:58 +01:00
await interaction.response.send_message(embed=embed, view=url_view)
print("Status queried.")
2023-01-12 18:12:58 +01:00
2023-01-13 17:47:58 +01:00
client.run(sys.argv[1])