Python Discord ChatBot:制作情绪计数器

时间:2018-07-27 17:31:48

标签: python discord discord.py

我想为不和谐的ChatBot设置心情计数器。此变量应影响对用户的响应。到目前为止,我用python编写了两本词典,一本称为响应,一本称为侮辱。响应非常简单,如果用户发送了词典中的消息,机器人将进行回复。每次侮辱机器人时,侮辱词典都会从变化的心情中减去设定的数量。我写了一条if语句,试图稍微减少情绪,但是每当我实际侮辱bot时,bot都不会对我说什么。请帮忙。请记住,这还不是全部代码,我愿意就我遇到的问题提供更深入的解释。

@client.event
async def on_message(message) :
    content = message.content.upper()
    mood = 2
    if content in responses:
        if mood <= 0:
            await client.send_message(message.channel, responses[content]["RANG"])
        if mood == 1:
            await client.send_message(message.channel, responses[content]["ANG"])
        if mood == 2:
            await client.send_message(message.channel, responses[content]["NEU"])
        if mood == 3:
            await client.send_message(message.channel, responses[content]["HAP"])
        if mood == 4:
            await client.send_message(message.channel, responses[content]["RHAP"])
    if content in insults:
        mood = mood - 0.25
        if mood <= 0:
            await client.send_message(message.channel, insults[content]["RANG"])
        if mood == 1:
            await client.send_message(message.channel, insults[content]["ANG"])
        if mood == 2:
            await client.send_message(message.channel, insults[content]["NEU"])
        if mood == 3:
            await client.send_message(message.channel, insults[content]["HAP"])
        if mood == 4:
            await client.send_message(message.channel, insults[content]["RHAP"])

1 个答案:

答案 0 :(得分:1)

您有奇怪的代码,因为您一直在第4行中设置mood=2

@client.event
async def on_message(message) :
    content = message.content.upper()

    mood = 2 # you hard-code your mood here!

    if content in responses: # might enter here

        if mood <= 0: # mood = 2, so won't enter here
            await client.send_message(message.channel, responses[content]["RANG"])

        if mood == 1: # mood = 2, so won't enter here
            await client.send_message(message.channel, responses[content]["ANG"])

        if mood == 2: # mood = 2, so ALWAYS here!
            await client.send_message(message.channel, responses[content]["NEU"])

        if mood == 3: # mood = 2, so won't enter here
            await client.send_message(message.channel, responses[content]["HAP"])

        if mood == 4: # mood = 2, so won't enter here
            await client.send_message(message.channel, responses[content]["RHAP"])

    if content in insults: # might enter here
        mood = mood - 0.25 # mood was 2, not it is 1.75

        if mood <= 0: # mood = 1.75, so won't enter here
            await client.send_message(message.channel, insults[content]["RANG"])

        if mood == 1: # mood = 1.75, so won't enter here
            await client.send_message(message.channel, insults[content]["ANG"])

        if mood == 2: # mood = 1.75, so won't enter here
            await client.send_message(message.channel, insults[content]["NEU"])

        if mood == 3: # mood = 1.75, so won't enter here
            await client.send_message(message.channel, insults[content]["HAP"])

        if mood == 4: # mood = 1.75, so won't enter here
            await client.send_message(message.channel, insults[content]["RHAP"])

您需要从某个地方提取mood。它来自哪里?

此外,您正在使用floats,但您的支票全部为== <some_int>。您可能需要将所有if mood == <int>更改为elif mood <= <int>

    if content in insults: # might enter here
        mood = mood - 0.25 # mood was 2, now it is 1.75

        if mood <= 0: # mood = 1.75, so won't enter here
            await client.send_message(message.channel, insults[content]["RANG"])

        elif mood <= 1: # mood = 1.75, so won't enter here
            await client.send_message(message.channel, insults[content]["ANG"])

        elif mood <= 2: # mood = 1.75, so ENTER HERE
            await client.send_message(message.channel, insults[content]["NEU"])

        elif mood <= 3: # elif, so skipped
            await client.send_message(message.channel, insults[content]["HAP"])

        elif mood <= 4: # elif, so skipped
            await client.send_message(message.channel, insults[content]["RHAP"])

使情绪更全球化

您的mood每次都会被重置。为避免这种情况,请尝试Access variables between commands with discord.py

mood = 2
@client.event
async def on_message(message) :
    content = message.content.upper()

    global mood

    if content in responses:
        .
        .
        .