【问题标题】:Python Discord ChatBot: Making a mood counterPython Discord ChatBot:制作情绪计数器
【发布时间】:2018-07-28 02:08:17
【问题描述】:

我想为我的不和谐聊天机器人制作一个情绪计数器。这个变量应该影响对用户的反应。到目前为止,我在 python 中制作了两本字典,一本叫做响应,一本叫做侮辱。响应很简单,如果用户发送了字典中的消息,机器人就会回复。每次您侮辱机器人时,侮辱字典都会从变量情绪中减去一定数量。我写了一个 if 语句,试图减少一点点情绪,但每当我尝试真正侮辱机器人时,机器人都不会对我说任何话。请帮忙。请记住,这并不是所有代码,我愿意就我遇到的问题提供更深入的解释。

@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"])

【问题讨论】:

    标签: python discord discord.py


    【解决方案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,但您的支票都是== &lt;some_int&gt;。您可能希望将所有 if mood == &lt;int&gt; 更改为 elif mood &lt;= &lt;int&gt;

        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:
            .
            .
            .
    

    【讨论】:

    • 我硬编码了从两点开始的心情,因为这是一个“中性”点。每当机器人受到侮辱时,我希望它减去 0.25 的情绪。因此,如果机器人受到足够的侮辱,情绪就会下降。感谢您向我展示如何处理情绪反应。
    • 好的,所以您可能希望使mood 变量更具全局性,并在每次收到新响应时更新它...现在,每次使用该功能时,您都会重置心情到2
    • 在我问这个问题之前,我尝试让变量更具全局性,但它似乎没有用。每当我测试它时,它会说在赋值之前引用了局部变量心情。我认为它认为我试图在函数内部声明变量,但事实并非如此。
    • 另外,现在每当我侮辱它时,它都会起作用并做出响应,但情绪不会下降,因为每当调用该函数时它都会重置。有什么建议吗?
    • 你能把mood = 2这一行放在函数之外(即@client.event这一行之前)吗?
    猜你喜欢
    • 2013-11-06
    • 1970-01-01
    • 2020-04-20
    • 2014-12-11
    • 2020-12-04
    • 2018-12-15
    • 1970-01-01
    • 2022-11-20
    • 1970-01-01
    相关资源
    最近更新 更多