-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathobject.py
More file actions
100 lines (91 loc) · 4.23 KB
/
object.py
File metadata and controls
100 lines (91 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import discord
from redbot.core import Config, checks, commands
import aiohttp
from bs4 import BeautifulSoup
class Object(commands.Cog):
"""Object cog"""
def __init__(self, bot):
self.bot = bot
self.config = Config.get_conf(self, identifier=707350954969792584)
default_guild = {"channels": [], "bot_channel": None}
self.config.register_guild(**default_guild)
self.url = "https://dev.prineside.com/en/gtasa_samp_model_id/model/"
self.image = "https://files.prineside.com/gtasa_samp_model_id/white/{}_w.jpg"
@commands.command()
async def object(self, ctx, id: str):
"""Get the object info"""
# If not allowed channel return
channels = await self.config.guild(ctx.guild).channels()
if ctx.channel.id not in channels:
bot_channel_id = await self.config.guild(ctx.guild).bot_channel()
bot_channel_mention = None
if bot_channel_id:
bot_channel = ctx.guild.get_channel(bot_channel_id)
if bot_channel:
bot_channel_mention = bot_channel.mention
if bot_channel_mention:
await ctx.send(f"{ctx.author.mention} This channel is not allowed to use the `object` command. Please use {bot_channel_mention} instead.")
else:
await ctx.send(f"{ctx.author.mention} This channel is not allowed to use the `object` command.")
await ctx.message.delete()
return
try:
id = int(id)
except ValueError:
await ctx.send(f"{ctx.author.mention} Please provide a valid object ID (number).")
return
async with ctx.typing():
# Get object info from webpage
url = self.url + str(id)
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
data = await response.text()
match = "[{}] - object of SA-MP and GTA San Andreas".format(id)
# check if object exists
if match not in data:
return await ctx.send("Object not found")
# scrape data
soup = BeautifulSoup(data, "html.parser")
# find the table by id "mp-model-info"
table = soup.find("table", {"id": "mp-model-info"})
# get first 8 rows
rows = table.findAll("tr")[:8]
# add embed
embed = discord.Embed()
# loop through rows
for row in rows:
# find children td
cols = row.findAll("td")
# get text
index = cols[0].text.strip()
# if next is input get value
if cols[1].find("input"):
value = cols[1].find("input").get("value")
else:
value = cols[1].text.strip()
# add to embed
embed.add_field(name=index, value=value, inline=True)
# get image
image = self.image.format(id)
embed.set_thumbnail(url=image)
embed.set_footer(text="Requested by: " + ctx.author.name, icon_url=ctx.author.avatar_url)
await ctx.send(embed=embed)
@checks.admin_or_permissions(manage_roles=True)
@commands.group()
async def objectset(self, ctx):
pass
@objectset.command()
async def channel(self, ctx):
channels = await self.config.guild(ctx.guild).channels()
if ctx.channel.id in channels:
channels.remove(ctx.channel.id)
await self.config.guild(ctx.guild).channels.set(channels)
await ctx.channel.send("Object command is no longer allowed in this channel.")
else:
channels.append(ctx.channel.id)
await self.config.guild(ctx.guild).channels.set(channels)
await ctx.channel.send("Object command is now allowed in this channel.")
@objectset.command(description="Set the bot channel to mention when command is used in disallowed channel")
async def botchannel(self, ctx, channel: discord.TextChannel):
await self.config.guild(ctx.guild).bot_channel.set(channel.id)
await ctx.send(f"Bot channel to mention set to {channel.mention}.")