This repository has been archived by the owner on Dec 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
137 lines (110 loc) · 5.16 KB
/
bot.py
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# bot.py
# python3 -m pip install discord.py discord-py-interactions gspread oauth2client
import discord
import re
from discord_slash import SlashCommand, SlashContext
from discord_slash.utils.manage_commands import create_permission
from discord_slash.utils.manage_components import create_select, create_select_option, create_actionrow, ComponentContext
from discord_slash.model import SlashCommandPermissionType
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import configparser
COLOR_RED = 0xE74C3C
COLOR_GREEN = 0x00ff00
COLOR_LIGHT_GREY = 0xBCC0C0
COLOR_DARK_GOLD = 0xC27C0E
COLOR_DEFAULT = 0x000000
slash = None
class Bot(discord.Client):
sheet = None
guild_ids = []
def __init__(self):
global slash
config = configparser.ConfigParser()
config.read('config.ini')
self.userIdCollaborateur = int(config['Role']['Collaborateur'])
self.token = config['Discord']['Token']
self.guild_ids = []
tempList = config['Discord']['GuildID'].split(',')
for tempid in tempList:
self.guild_ids.append((int(tempid)))
client = gspread.service_account(filename = 'weazel-news-331709-b0d89286ef38.json', scopes = gspread.auth.READONLY_SCOPES)
self.sheet = client.open_by_key("1cJh0ZlXKX5WcaYfVzGlKs09nwa2D3aoKZqQMx02s7Q4")
intents = discord.Intents.all()
self.client = discord.Client(intents=intents)
slash = SlashCommand(self.client, sync_commands=True)
self.on_ready = self.client.event(self.on_ready)
self.on_component = self.client.event(self.on_component)
async def on_component(self, ctx: ComponentContext):
embed=discord.Embed(title=ctx.selected_options[0].split(';')[0],
description="Prix véhicule HT : " + ctx.selected_options[0].split(';')[1] + "$",
color=0xFF5733)
amount = round(int(ctx.selected_options[0].split(';')[1])*2/100 if int(ctx.selected_options[0].split(';')[1])>50000 else int(ctx.selected_options[0].split(';')[1])*1/100)
description="Prix véhicule HT : " + ctx.selected_options[0] + "$",
embed.add_field(name="Flash", value=str(amount+1000) + "$", inline=True)
embed.add_field(name="Classified", value=str(amount+600) + "$", inline=True)
await ctx.edit_origin(description = description, embed=embed)
async def on_ready(self):
print(str(self.client.user) + " has connected to Discord")
print("Bot ID is " + str(self.client.user.id))
await self.client.wait_until_ready()
print(str(self.client.user) + " is now ready!")
def run(self):
print("Starting bot ...")
self.client.run(self.token)
bot = Bot()
@slash.slash(
name="prix",
description="Prix des prestations pour un véhicule",
default_permission = False,
permissions={
bot.guild_ids[0]: [
create_permission(bot.userIdCollaborateur, SlashCommandPermissionType.ROLE, True)]
},
options = [{
"name": "modele",
"description": "Modèle du Véhicule",
"type": 3,
"required": True
}],
guild_ids=bot.guild_ids)
async def _prix(ctx: SlashContext, modele: str):
await ctx.defer(hidden=True)
amount_re = re.compile(modele, re.IGNORECASE)
cell = bot.sheet.worksheet("Vehicules").find(amount_re)
if not cell:
await ctx.send(content="Véhicule non trouvé !",hidden=True)
return
cell_list = bot.sheet.worksheet("Vehicules").findall(amount_re)
if(len(cell_list)==1):
amount = int(bot.sheet.worksheet("Vehicules").cell(cell_list[0].row, cell_list[0].col+1).value)
modele = bot.sheet.worksheet("Vehicules").cell(cell_list[0].row, cell_list[0].col).value
embed=discord.Embed(title=modele,
description="Prix véhicule HT : " + str(amount) + "$",
color=0xFF5733)
if(amount > 50000):
amount = int(amount * 2/100)
else:
amount = int(amount * 1/100)
embed.add_field(name="Flash", value=str(amount + 1000) + "$", inline=True)
embed.add_field(name="Classified", value=str(amount + 600) + "$", inline=True)
await ctx.send(embed=embed, hidden=True)
else:
embed=discord.Embed(title=modele,
description="Prix véhicule HT",
color=0xFF5733)
options = []
for data in cell_list:
options.append(create_select_option(
bot.sheet.worksheet("Vehicules").cell(data.row, 1).value,
value = bot.sheet.worksheet("Vehicules").cell(data.row, 1).value + ";" + bot.sheet.worksheet("Vehicules").cell(data.row, 2).value
))
select = create_select(
options=options,
placeholder="Choisir le véhicule",
min_values=1,
max_values=1
)
action_row = create_actionrow(select)
await ctx.send(embed=embed, hidden=True, components=[action_row])
bot.run()