-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbot.py
More file actions
237 lines (189 loc) · 10.1 KB
/
bot.py
File metadata and controls
237 lines (189 loc) · 10.1 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import discord
import os
import requests
from discord.ext import commands
from dotenv import load_dotenv
from datetime import datetime, timedelta
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
TEBEX_SECRET = os.getenv('TEBEX_SECRET')
ADMIN_ROLE_IDS = [int(role_id) for role_id in os.getenv('ADMIN_ROLE_IDS').split(',')]
bot = commands.Bot(command_prefix='/', intents=discord.Intents.all())
def is_admin(ctx):
return any(role.id in ADMIN_ROLE_IDS for role in ctx.author.roles)
@bot.slash_command(name='verify', description='Get payment info from Transaction ID')
@commands.check(is_admin)
async def kakunin(ctx, transaction_id: discord.Option(str, "Transaction ID")):
url = f'https://plugin.tebex.io/payments/{transaction_id}'
key = {'X-Tebex-Secret': TEBEX_SECRET}
response = requests.get(url, headers=key)
if response.status_code == 200:
data = response.json()
# Convert the date to UTC
date_str = data['date']
date_utc = datetime.strptime(date_str, "%Y-%m-%dT%H:%M:%S%z")
date_utc_str = date_utc.strftime("%Y-%m-%d %H:%M:%S")
embed = discord.Embed(
title=f"🔍 Information for {transaction_id}",
description="Here are the details of the transaction:",
color=discord.Color.blue()
)
embed.add_field(name="💰 Price", value=data['amount'], inline=True)
status = data['status']
if status.lower() == 'complete':
status_text = f"```🟢 {status}```"
else:
status_text = f"```🔴 {status}```"
embed.add_field(name="📊 Status", value=status_text, inline=True)
embed.add_field(name="📅 Date (UTC)", value=date_utc_str, inline=False)
player_name = data['player']['name']
embed.add_field(name="👤 Tebex Username", value=player_name, inline=False)
package_names = ', '.join([package['name'] for package in data['packages']])
embed.add_field(name="🎁 Package Name(s)", value=package_names, inline=False)
embed.set_footer(
text="Powered By NickyBoy",
icon_url="https://i.imgur.com/QfmDKS6.png"
)
await ctx.respond(embed=embed)
else:
await ctx.respond('Failed to retrieve payment information.')
@bot.slash_command(name='products', description='list of produscts on the store')
@commands.check(is_admin)
async def products(ctx):
url = 'https://plugin.tebex.io/packages'
key = {'X-Tebex-Secret': TEBEX_SECRET}
response = requests.get(url, headers=key)
if response.status_code == 200:
packages = response.json()
embeds = []
current_embed = None
for index, package in enumerate(packages, start=1):
if index % 25 == 1:
if current_embed:
embeds.append(current_embed)
current_embed = discord.Embed(title='Products', color=0XE16941, description='Here are the list of products on the store')
package_name = package['name']
package_price = package['price']
package_category = package['category']['name']
package_id = package['id']
package_info = f"Price: {package_price}, ID: {package_id}, Category: {package_category}"
current_embed.add_field(name=package_name, value=package_info, inline=False)
if current_embed:
embeds.append(current_embed)
if embeds:
for embed in embeds:
await ctx.respond(embed=embed)
else:
await ctx.respond('No products found.')
else:
await ctx.respond('Failed to retrieve product information.')
@bot.slash_command(name='search', description='Get information from Tebex username')
@commands.check(is_admin)
async def search(ctx, tebex_id: discord.Option(str, "Tebex username")):
url = f'https://plugin.tebex.io/user/{tebex_id}'
key = {'X-Tebex-Secret': TEBEX_SECRET}
response = requests.get(url, headers=key)
if response.status_code == 200:
data = response.json()
embed = discord.Embed(title=f'🔍Player Information for {tebex_id}')
embed.add_field(name='👤Username', value=data['player']['username'])
embed.add_field(name='🔨Ban Count', value=data['banCount'])
embed.add_field(name='💳Chargeback Rate', value=data['chargebackRate'])
total_purchases = '\n'.join([f"{currency}: {amount}" for currency, amount in data['purchaseTotals'].items()])
embed.add_field(name='💵Total Purchases', value=total_purchases)
# Recent payment histories
payments = data['payments'][:5] # Limit to the 5 most recent payments
payment_info = ""
for payment in payments:
txn_id = payment.get('txn_id', 'N/A')
timestamp = payment.get('time', 0)
price = payment.get('price', 'N/A')
currency = payment.get('currency', 'N/A')
status = payment.get('status', 'N/A')
# Convert the Unix timestamp to a datetime object in UTC
dt = datetime.utcfromtimestamp(timestamp)
# Format the datetime as a string in UTC
utc_time = dt.strftime("%Y-%m-%d %H:%M:%S")
payment_info += f"Transaction ID: {txn_id}\nPrice: {price} {currency}\nStatus: {status}\nDate (UTC): {utc_time}\n\n"
if payment_info:
embed.add_field(name='Recent Payments', value=payment_info, inline=False)
else:
embed.add_field(name='Recent Payments', value='No recent payments found', inline=False)
embed.set_footer(text="Powered By NickyBoy", icon_url="https://i.imgur.com/QfmDKS6.png")
await ctx.respond(embed=embed)
else:
await ctx.respond('Failed to retrieve player information.')
@bot.slash_command(name='updateproduct', description='Update product information')
@commands.check(is_admin)
async def updateproduct(ctx, package_id: discord.Option(int, "product ID"), enabled: discord.Option(bool, "enabled / disabled"), name: discord.Option(str, "new product name"), price: discord.Option(float, "new price")):
url = f'https://plugin.tebex.io/package/{package_id}'
key = {'X-Tebex-Secret': TEBEX_SECRET}
data = {
'disabled': not enabled,
'name': name,
'price': price
}
response = requests.put(url, headers=key, json=data)
if response.status_code == 204:
status = 'enabled' if enabled else 'disabled'
await ctx.respond(f'Package {package_id} has been updated. Status: {status}, Name: {name}, Price: {price}')
else:
await ctx.respond('Failed to update the package.')
@bot.slash_command(name='createurl', description='Create payment URL')
@commands.check(is_admin)
async def createurl(ctx, package_id: discord.Option(str, "product ID"), tebex_id: discord.Option(str, "Tebex username")):
url = 'https://plugin.tebex.io/checkout'
key = {'X-Tebex-Secret': TEBEX_SECRET}
data = {
'package_id': package_id,
'username': tebex_id
}
response = requests.post(url, headers=key, json=data)
if response.status_code == 201:
checkout_data = response.json()
checkout_url = checkout_data['url']
expires_at = checkout_data['expires']
# Convert ISO format to readable UTC format
expires_at_dt = datetime.strptime(expires_at, "%Y-%m-%dT%H:%M:%SZ")
readable_expires_at = expires_at_dt.strftime("%Y-%m-%d %H:%M")
embed = discord.Embed(title='Checkout URL Created', color=discord.Color.green())
embed.add_field(name='URL', value=checkout_url)
embed.add_field(name='Expires At', value=readable_expires_at)
embed.set_footer(text="Powered By NickyBoy", icon_url="https://i.imgur.com/QfmDKS6.png")
await ctx.respond(embed=embed)
else:
await ctx.respond('Failed to create the checkout URL.')
@bot.slash_command(name='recentpayments', description='Recent payment information')
@commands.check(is_admin)
async def recentpayments(ctx):
url = 'https://plugin.tebex.io/payments?paged=1'
key = {'X-Tebex-Secret': TEBEX_SECRET}
response = requests.get(url, headers=key)
if response.status_code == 200:
data = response.json()
payments = data['data'][:25] # Truncate the list to the first 25 payments
embed = discord.Embed(title='Recent Payments', color=discord.Color.blue())
payment_info = ""
for index, payment in enumerate(payments, start=1):
transaction_id = payment.get('id', 'N/A') # Use 'N/A' as default if 'id' is not found
timestamp = payment.get('date', 0) # Use 0 as default if 'date' is not found
amount = payment.get('amount', 'N/A') # Use 'N/A' as default if 'amount' is not found
currency = payment.get('currency', {}).get('iso_4217', 'N/A') # Use 'N/A' as default if 'currency' or 'iso_4217' is not found
status = payment.get('status', 'N/A') # Use 'N/A' as default if 'status' is not found
player_name = payment.get('player', {}).get('name', 'N/A') # Use 'N/A' as default if 'player' or 'name' is not found
# Convert the timestamp to a datetime object
dt = datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S%z")
# Add 9 hours to convert from UTC to JST
jst_dt = dt + timedelta(hours=-4)
# Format the datetime as a string in JST
jst_time = jst_dt.strftime("%Y-%m-%d %H:%M:%S")
package_names = ', '.join([package.get('name', 'N/A') for package in payment.get('packages', [])]) # Use 'N/A' as default if 'packages' or 'name' is not found
payment_info += f"**Transaction ID: {transaction_id}**\nPlayer: {player_name}\nAmount: {amount} {currency}\nPackage(s): {package_names}\nStatus: {status}\nDate (JST): {jst_time}\n\n"
if index % 5 == 0 or index == len(payments):
embed.add_field(name=f"Payments {index-4} to {index}", value=payment_info, inline=False)
payment_info = ""
embed.set_footer(text="Powered By NickyBoy", icon_url="https://i.imgur.com/QfmDKS6.png")
await ctx.respond(embed=embed)
else:
await ctx.respond('Failed to retrieve recent payments.')
bot.run(TOKEN)