-
Notifications
You must be signed in to change notification settings - Fork 2
/
merchant.py
119 lines (105 loc) · 4.95 KB
/
merchant.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
import database
import game
import json
import discord
import logging
from discord.ext.commands import Bot
from discord.ext import tasks, commands
logging.basicConfig(handlers=[logging.FileHandler(filename="logs/merchant.log",
encoding='utf-8', mode='a+')],
format="%(asctime)s %(name)s:%(levelname)s:%(message)s",
datefmt="%F %A %T",
level=logging.INFO)
with open("/home/pi/Atom/TBG/game_data/bot_token.json","r") as f:
file = json.load(f)
token = file["Merchant"]
f.close()
db = database.Database()
class merchant:
def __init__(self,token):
self.token = token
self.client = Bot(command_prefix=".")
self.shop = game.shop()
self.client.remove_command("help")
self.prepare_client()
def run(self):
self.client.run(self.token)
def get_discord_emoji(self,emoji_id):
return self.client.get_emoji(int(emoji_id))
def prepare_client(self):
@tasks.loop(hours=2.0)
async def rotate_items():
self.shop.generate_items()
print("shop rotating")
@self.client.event
async def on_ready():
print("ready to trade")
rotate_items.start()
@self.client.event
async def on_command_error(ctx, error):
if isinstance(error, commands.CommandNotFound):
return
raise error
@self.client.command()
async def shop(ctx):
ShopEmbed = discord.Embed(colour=discord.Colour.green())
ShopEmbed.set_author(name="MERCHANT'S SHOP")
for item_name in self.shop.return_items():
ShopEmbed.add_field(name=f"{self.get_discord_emoji(db.get_graphic(item_name)[1])} **{item_name}**",value=f"`COST` {int(db.get_item(game.hash_string(item_name))[2]*0.95)}")
await ctx.message.channel.send(embed=ShopEmbed)
@self.client.command()
async def buy(ctx):
message_contents = ctx.message.content.split(" ")
user = ctx.message.author
amount = 1
if message_contents[-1].isdigit():
item_name = " ".join(message_contents[1:-1])
amount = int(message_contents[-1])
else:
item_name = " ".join(message_contents[1:])
if item_name in database.forbidden_items:
await ctx.message.channel.send(f"you can't buy {item_name} here")
return
if db.get_inventory(ctx.message.author.id)[1]+amount > 20:
await ctx.message.channel.send("you can't have over 20 items")
return
for abbreviation in database.abbreviations:
item_name = item_name.replace(abbreviation[0],abbreviation[1])
if db.get_item(game.hash_string(item_name)) is None:
await ctx.message.channel.send(f"{item_name} is not a valid item")
return
price = db.get_item(game.hash_string(item_name))[2]
if item_name in self.shop.return_items():
price = price*0.95
if game.update_money(user.id,-price*amount):
db.add_item(user.id,game.hash_string(item_name),amount)
await ctx.message.channel.send(f"{user.mention} bought {str(amount)} X {item_name}")
logging.info(f"{user.id} bought {str(amount)} X {item_name}")
return
await ctx.message.channel.send("you don't have enough money, my friend")
@self.client.command()
async def sell(ctx):
user = ctx.message.author
user_inv = db.get_inventory(user.id)[0]
message_contents = ctx.message.content.split(" ")
amount = 1
if message_contents[-1].isdigit():
item_name = " ".join(message_contents[1:-1])
amount = int(message_contents[-1])
else:
item_name = " ".join(message_contents[1:])
for abbreviation in database.abbreviations:
item_name = item_name.replace(abbreviation[0],abbreviation[1])
if item_name not in user_inv.keys():
await ctx.message.channel.send(f"you don't have any {item_name}")
return
if user_inv[item_name] >= amount:
game.update_money(user.id,int(db.get_item(game.hash_string(item_name))[2]*amount/4))
await ctx.message.channel.send(f"{user.mention} sold {str(amount)} X {item_name} for {int(db.get_item(game.hash_string(item_name))[2]*amount/4)}{self.get_discord_emoji(822330641559191573)}")
db.add_item(user.id,game.hash_string(item_name),-amount)
logging.info(f"{str(user.id)} has removed {str(amount)} X {item_name}")
return
await message.channel.send(f"you don't have enough {item_name}, my friend")
if __name__ == "__main__":
MerchantBot = merchant(token)
MerchantBot.run()