Search code examples
python-3.xdiscord.pydice

How would I append an integer to a single line from my bot?


I have no idea how to word this question, but I am trying to put all information from the roll into one response from my bot.

import discord
import random

DND_1d6 = [1, 2, 3, 4, 5, 6]

@client.event
async def on_message(message):
    if message.content.startswith(";roll 1d6"):
        response = random.choice(DND_1d6)
        await message.channel.send(response)

    if message.content.startswith(";roll 2d6"):
        response = random.choice(DND_1d6), random.choice(DND_1d6)
        response_added = random.choice(DND_1d6) + random.choice(DND_1d6)
# how would i use these two variables together in one line?
        await message.channel.send()

client.run(client_id)

So for example, if a user types ";roll 2d6" I want to have the bot type out the first and second roll separately, "2, 6" and have the bot add the two numbers together "8" all in one nice line. This is simply a quality of life thing to not spam the chat. How could I go about this? The final result I am looking for would be something like this "You rolled x and y for a total of z."


Solution

  • You can construct a string using the results you get and send that to the channel.

    Also note that response = random.choice(DND_1d6), random.choice(DND_1d6) creates a tuple that contains the two rolls, for example (2,6). You do not need to roll again as you are doing in response = random.choice(DND_1d6), random.choice(DND_1d6) as these will give you different numbers (they are not linked to the previous rolls).

    import discord
    import random
    
    DND_1d6 = [1, 2, 3, 4, 5, 6]
    
    @client.event
    async def on_message(message):
        if message.content.startswith(";roll 1d6"):
            response = random.choice(DND_1d6)
            await message.channel.send(response)
    
        if message.content.startswith(";roll 2d6"):
            response = random.choice(DND_1d6), random.choice(DND_1d6)
            response_str = 'You rolled {0} and {1} for a total of {2}'.format(response[0], response[1], sum(response))
            await message.channel.send(response_str )
    
    client.run(client_id)