Counter Discord机器人报告“ IndentationError”

时间:2020-06-28 17:49:28

标签: python discord

这是我的代码:

@client.event
async def on_message(message):
    global counter
    if message.author == client.user:
        counter += 1


    if message.content.startswith('Count'):
    await message.channel.send('Ya', counter)

并报错:

 C:\>Users\nasty\Desktop\discord\text.py
  File "C:\Users\nasty\Desktop\discord\text.py", line 13
    global counter
    ^
IndentationError: expected an indented block

2 个答案:

答案 0 :(得分:1)

如果应该将其作为if块的一部分,请在其右边留一个标签或4个空格

答案 1 :(得分:1)

您所需要做的就是修复缩进。

更改此

function newArray (obj1, obj2) {
  // Extract every item from obj1 into an Array, to make the next step easier
  const arr1 = obj1.reduce((res, group) => [ ...res, ...group.info], []);
 
  return obj2.map(e => {
    const match = arr1.find(x => x.id === e.id && x.code === e.code);
    return { ...e, nos: match ? match.nos : 0 };
  });
}

var obj1 =[
 { tot: 4, info: [{ id:1, code:"x1", nos:2 },{ id:2, code:"x2",nos:2 }] },
 { tot: 2, info: [{ id:3, code:"x3", nos:2 },{ id:5, code:"x5",nos:3 }] }
];

var obj2 = [
 { id:1, code: "x1", nos:1, name: "sample1" },
 { id:2, code: "x2", nos:2, name: "sample2" },
 { id:3, code:"x3", nos:3, name: "sample3" },
 { id:4, code: "x1", nos: 0 },
 { id:5, code: "x5", nos: 3, name: "sample5" }
];

const res = newArray(obj1, obj2);

console.log( JSON.stringify(res).replace(/\},\{/g, '},\n{') );
/*
[{"id":1,"code":"x1","nos":2,"name":"sample1"},
{"id":2,"code":"x2","nos":2,"name":"sample2"},
{"id":3,"code":"x3","nos":2,"name":"sample3"},
{"id":4,"code":"x1","nos":0},
{"id":5,"code":"x5","nos":3,"name":"sample5"}]
*/

进入

if message.content.startswith('Count'):
await message.channel.send('Ya', counter)