Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python “投注”;XP";在命令discord.py中_Python_Python 3.x_Discord_Discord.py - Fatal编程技术网

Python “投注”;XP";在命令discord.py中

Python “投注”;XP";在命令discord.py中,python,python-3.x,discord,discord.py,Python,Python 3.x,Discord,Discord.py,我不知道如何让用户在命令中下注X个数量的“XP”(我使用美元)。我在下面发布了一个简单的coinflip命令,其中包含我认为应该具备的逻辑,但我不能100%确定我是否在正确的轨道上。我想知道,当用户随机下注一笔钱时,我怎么能给他们打电话叫“get_$”。我猜我需要创建一些东西,比如betamount=enter authors bet amount,但我在如何处理他们可能放入的随机金额方面还是空白,而不是硬编码一个强制用户使用的固定金额 client = discord.Client() tr

我不知道如何让用户在命令中下注X个数量的“XP”(我使用美元)。我在下面发布了一个简单的coinflip命令,其中包含我认为应该具备的逻辑,但我不能100%确定我是否在正确的轨道上。我想知道,当用户随机下注一笔钱时,我怎么能给他们打电话叫“get_$”。我猜我需要创建一些东西,比如
betamount=enter authors bet amount
,但我在如何处理他们可能放入的随机金额方面还是空白,而不是硬编码一个强制用户使用的固定金额

client = discord.Client()

try:
    with open("cash.json") as fp:
        cash = json.load(fp)
except Exception:
    cash = {}

def save_cash():
    with open("cash.json", "w+") as fp:
        json.dump(cash, fp, sort_keys=True, indent=4)

def get_dollars(user: discord.User):
    id = user.id
    if id in cash:
        return cash[id].get("dollars", 0)
    return 0

@client.event
async def on_message(message):
    betamount = ???
    if message.content.lower().startswith('!coinflip'):
        if get_dollars(message.author) < 0:
            await client.send_message(message.channel, "{} you don't have enough money to bet.".format(message.author.mention))
        else:
            choice = random.randint(0,1)
            if choice == 0
                await client.add_reaction(message, '⚫')
                await client.send_message(message.channel, "The coin handed on heads!)
                if 'heads' in message.content:
                    await client.send_message(message.channel, "You've won ${}".format(betamount))
                    add_dollars(message.author, betamount)
                else:
                    if 'tails' in message.content:
                        await client.send_message(message.channel, "You've lost ${}".format(betamount))
                        remove_dollars(message.author, betamount)
            elif choice == 1:
                await client.add_reaction(message, '⚪')
                await client.send_message(message.channel, "The coin handed on tails!")
                if 'tails' in message.content:
                    await client.send_message(message.channel, "You've won ${}".format(betamount))
                    add_dollars(message.author, betamount)
                else:
                    if 'heads' in message.content:
                        await client.send_message(message.channel, "You've lost ${}".format(betamount))
                        remove_dollars(message.author, betamount)
client=discord.client()
尝试:
以open(“cash.json”)作为fp:
cash=json.load(fp)
除例外情况外:
现金={}
def save_cash():
以open(“cash.json”、“w+”)作为fp:
dump(cash、fp、sort_keys=True、indent=4)
def获取美元(用户:discord.user):
id=user.id
如果身份证是现金:
返回现金[id]。获取(“美元”,0)
返回0
@客户端事件
异步def on_消息(消息):
betamount=???
如果message.content.lower().startswith(“!coinflip”):
如果获取美元(message.author)<0:
等待客户。发送消息(message.channel,“{}您没有足够的钱下注。”.format(message.author.notice))
其他:
choice=random.randint(0,1)
如果选择==0
等待客户。添加反应(消息,'⚫')
等待客户。发送_消息(message.channel,“硬币在头上交!”
如果message.content中有“heads”:
等待客户端。发送消息(message.channel,“youhavewon${}”。格式(betamount))
添加美元(message.author,betamount)
其他:
如果message.content中有“tails”:
wait client.send_message(message.channel,“You's lost${}”。格式(betamount))
删除美元(message.author,betamount)
elif选项==1:
等待客户。添加反应(消息,'⚪')
等待客户。发送消息(message.channel,“硬币放在尾巴上!”)
如果message.content中有“tails”:
等待客户端。发送消息(message.channel,“youhavewon${}”。格式(betamount))
添加美元(message.author,betamount)
其他:
如果message.content中有“heads”:
wait client.send_message(message.channel,“You's lost${}”。格式(betamount))
删除美元(message.author,betamount)

如果您使用的是像
!coinflip(heads/tails)(amount)
这样的命令,那么您可以使用
x=message.content.split(“”
)将消息拆分为一个列表。使用此命令,您可以执行类似于
output=x[1]
betamount=x[2]
的操作


我还建议您将消息中的'tails'更改为
。内容:
如果结果=='tails'
。否则用户可以执行类似于
!coinflip heads(amount)tails
的操作,每次都会奖励他们现金。

如果您使用的命令是
!coinflip(heads/tails)(amount)
然后您可以使用
x=message.content.split(“”
将消息拆分为一个列表。使用此选项,您可以执行类似
output=x[1]
betamount=x[2]
的操作


我还建议您将消息中的“tails”更改为
。内容:
如果结果=='tails'
。否则用户可以执行类似于
!coinflip heads(amount)tails
的操作,每次都会奖励他们现金。

如果您使用带有参数的命令,(尤其是当这些参数变成像
discord.Member
这样的类型时),我强烈建议切换到使用
discord.ext.commands
扩展。它使编写命令变得更加简单,这意味着您可以将代码从
on_message
事件中移出。下面是使用
commands
时命令的外观:

from discord.ext import commands

bot = commands.Bot(command_prefix='!')

@bot.command(pass_context=True)
async def coinflip(ctx, guess: str, amount: float):
    guesses = ('heads', 'tails')
    guess = guess.lower()
    if guess not in guesses:
        await bot.say("Invalid guess.")
        return
    author = ctx.message.author
    balance = get_dollars(author)
    if balance < amount:
        await bot.say(f"You don't have that much money.  Your balance is ${balance:.2f}")
        return
    result = random.sample(guesses)
    if result == guess:
        await bot.say("You won!")
        add_dollars(author, amount)
    else:
        await bot.say("You lost!")
        remove_dollars(author, amount)

bot.run("TOKEN")
来自discord.ext导入命令
bot=commands.bot(命令前缀='!')
@命令(pass\u context=True)
异步def coinflip(ctx,guess:str,amount:float):
猜测=(‘正面’、‘反面’)
guess=guess.lower()
如果猜测不在猜测中:
等待机器人。说(“无效猜测”)
返回
author=ctx.message.author
余额=获取美元(作者)
如果余额<金额:
等待bot。说(f“你没有那么多钱。你的余额是${balance:.2f}”)
返回
结果=随机样本(猜测)
如果结果==猜测:
等待机器人。说:“你赢了!”
加上美元(作者、金额)
其他:
等待机器人。说:“你输了!”
删除单位(作者、金额)
bot.run(“令牌”)

还要记住,如果您开始使用命令,您必须将带有参数的命令添加到您的
on_消息
事件的底部(尤其是当这些参数变成
discord.Member等类型时),我强烈建议切换到使用
discord.ext.commands
扩展。它使编写命令变得更加简单,这意味着您可以将代码从
on_message
事件中移出。下面是使用
commands
时命令的外观:

from discord.ext import commands

bot = commands.Bot(command_prefix='!')

@bot.command(pass_context=True)
async def coinflip(ctx, guess: str, amount: float):
    guesses = ('heads', 'tails')
    guess = guess.lower()
    if guess not in guesses:
        await bot.say("Invalid guess.")
        return
    author = ctx.message.author
    balance = get_dollars(author)
    if balance < amount:
        await bot.say(f"You don't have that much money.  Your balance is ${balance:.2f}")
        return
    result = random.sample(guesses)
    if result == guess:
        await bot.say("You won!")
        add_dollars(author, amount)
    else:
        await bot.say("You lost!")
        remove_dollars(author, amount)

bot.run("TOKEN")
来自discord.ext导入命令
bot=commands.bot(命令前缀='!')
@命令(pass\u context=True)
异步def coinflip(ctx,guess:str,amount:float):
猜测=(‘正面’、‘反面’)
guess=guess.lower()
如果猜测不在猜测中:
等待机器人。说(“无效猜测”)
返回
author=ctx.message.author
余额=获取美元(作者)
如果余额<金额:
等待bot。说(f“你没有那么多钱。你的余额是${balance:.2f}”)
返回
结果=随机样本(猜测)
如果结果==猜测:
等待机器人。说:“你赢了!”
加丁娃娃